/
HypervisorUpdateAction.java
349 lines (299 loc) · 14.1 KB
/
HypervisorUpdateAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* Copyright (c) 2009 - 2012 Red Hat, Inc.
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/
package org.candlepin.service.impl;
import org.candlepin.auth.Principal;
import org.candlepin.dto.ModelTranslator;
import org.candlepin.dto.api.v1.HypervisorConsumerDTO;
import org.candlepin.dto.api.v1.HypervisorUpdateResultDTO;
import org.candlepin.model.Consumer;
import org.candlepin.model.ConsumerCurator;
import org.candlepin.model.ConsumerType;
import org.candlepin.model.ConsumerType.ConsumerTypeEnum;
import org.candlepin.model.ConsumerTypeCurator;
import org.candlepin.model.GuestId;
import org.candlepin.model.HypervisorId;
import org.candlepin.model.Owner;
import org.candlepin.model.VirtConsumerMap;
import org.candlepin.pinsetter.tasks.HypervisorUpdateJob;
import org.candlepin.resource.ConsumerResource;
import org.candlepin.resource.util.GuestMigration;
import org.candlepin.service.SubscriptionServiceAdapter;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Returns {@link Result} of hypervisor update containing the result of the
* update and a map of known hypervisor consumers.
*/
public class HypervisorUpdateAction {
private static Logger log = LoggerFactory.getLogger(HypervisorUpdateAction.class);
private ConsumerCurator consumerCurator;
private ConsumerResource consumerResource;
private ConsumerType hypervisorType;
private SubscriptionServiceAdapter subAdapter;
private ModelTranslator translator;
public static final String CREATE = "create";
protected static String prefix = "hypervisor_update_";
@Inject
public HypervisorUpdateAction(ConsumerCurator consumerCurator,
ConsumerTypeCurator consumerTypeCurator, ConsumerResource consumerResource,
SubscriptionServiceAdapter subAdapter, ModelTranslator translator) {
this.consumerCurator = consumerCurator;
this.consumerResource = consumerResource;
this.subAdapter = subAdapter;
this.translator = translator;
this.hypervisorType = consumerTypeCurator.getByLabel(ConsumerTypeEnum.HYPERVISOR.getLabel(), true);
}
@Transactional
public Result update(
final Owner owner,
final HypervisorUpdateJob.HypervisorList hypervisors,
final Boolean create,
final Principal principal,
final String jobReporterId) {
final String ownerKey = owner.getKey();
log.debug("Hypervisor consumers for create/update: {}", hypervisors.getHypervisors().size());
log.debug("Updating hypervisor consumers for org {}", ownerKey);
Set<String> hosts = new HashSet<>();
Set<String> guests = new HashSet<>();
Map<String, Consumer> incomingHosts = new HashMap<>();
parseHypervisorList(hypervisors, hosts, guests, incomingHosts);
// TODO Need to ensure that we retrieve existing guestIds from the DB before continuing.
// Maps virt hypervisor ID to registered consumer for that hypervisor, should one exist:
VirtConsumerMap hypervisorKnownConsumersMap = consumerCurator
.getHostConsumersMap(owner, hypervisors);
HypervisorUpdateResultDTO result = new HypervisorUpdateResultDTO();
Map<String, Consumer> systemUuidKnownConsumersMap = new HashMap<>();
for (Consumer consumer : hypervisorKnownConsumersMap.getConsumers()) {
if (consumer.hasFact(Consumer.Facts.SYSTEM_UUID)) {
systemUuidKnownConsumersMap.put(
consumer.getFact(Consumer.Facts.SYSTEM_UUID).toLowerCase(), consumer);
}
}
Map<String, GuestId> guestIds = consumerCurator.getGuestIdMap(guests, owner);
for (String hypervisorId : hosts) {
Consumer incoming = incomingHosts.get(hypervisorId);
Consumer knownHost = hypervisorKnownConsumersMap.get(hypervisorId);
// HypervisorId might be different in candlepin
if (knownHost == null && incoming.hasFact(Consumer.Facts.SYSTEM_UUID) &&
systemUuidKnownConsumersMap.get(
incoming.getFact(Consumer.Facts.SYSTEM_UUID).toLowerCase()) != null) {
knownHost = systemUuidKnownConsumersMap.get(incoming.getFact(
Consumer.Facts.SYSTEM_UUID).toLowerCase());
if (knownHost != null) {
log.debug("Found a known host by system uuid");
}
}
Consumer reportedOnConsumer = null;
if (knownHost == null) {
if (!create) {
result.addFailed(hypervisorId,
"Unable to find hypervisor with id " + hypervisorId + " in org " + ownerKey);
}
else {
log.debug("Registering new host consumer for hypervisor ID: {}", hypervisorId);
Consumer newHost = createConsumerForHypervisorId(hypervisorId, jobReporterId, owner,
principal, incoming);
// Since we just created this new consumer, we can migrate the guests immediately
GuestMigration guestMigration = new GuestMigration(consumerCurator)
.buildMigrationManifest(incoming, newHost);
// Now that we have the new consumer persisted, immediately migrate the guests to it
if (guestMigration.isMigrationPending()) {
guestMigration.migrate(false);
}
hypervisorKnownConsumersMap.add(hypervisorId, newHost);
result.addCreated(this.translator.translate(newHost, HypervisorConsumerDTO.class));
reportedOnConsumer = newHost;
}
}
else {
boolean hypervisorIdUpdated = false;
if (knownHost.getHypervisorId() != null && !hypervisorId.equalsIgnoreCase(knownHost
.getHypervisorId().getHypervisorId())) {
hypervisorIdUpdated = true;
log.debug("Changing hypervisor id to [" + hypervisorId + "]");
knownHost.getHypervisorId().setHypervisorId(hypervisorId);
}
reportedOnConsumer = knownHost;
if (jobReporterId != null && knownHost.getHypervisorId() != null &&
hypervisorId.equalsIgnoreCase(knownHost.getHypervisorId().getHypervisorId()) &&
knownHost.getHypervisorId().getReporterId() != null &&
!jobReporterId.equalsIgnoreCase(knownHost.getHypervisorId().getReporterId())) {
log.debug("Reporter changed for Hypervisor {} of Owner {} from {} to {}",
hypervisorId, ownerKey, knownHost.getHypervisorId().getReporterId(),
jobReporterId);
}
boolean typeUpdated = false;
if (!hypervisorType.getId().equals(knownHost.getTypeId())) {
typeUpdated = true;
knownHost.setType(hypervisorType);
}
final GuestMigration guestMigration = new GuestMigration(consumerCurator)
.buildMigrationManifest(incoming, knownHost);
final boolean factsUpdated = consumerResource.checkForFactsUpdate(knownHost, incoming);
if (factsUpdated || guestMigration.isMigrationPending() || typeUpdated ||
hypervisorIdUpdated) {
knownHost.setLastCheckin(new Date());
guestMigration.migrate(false);
result.addUpdated(this.translator.translate(knownHost, HypervisorConsumerDTO.class));
}
else {
result.addUnchanged(
this.translator.translate(knownHost, HypervisorConsumerDTO.class));
}
}
// update reporter id if it changed
if (jobReporterId != null && reportedOnConsumer != null &&
reportedOnConsumer.getHypervisorId() != null &&
(reportedOnConsumer.getHypervisorId().getReporterId() == null ||
!jobReporterId.contentEquals(reportedOnConsumer.getHypervisorId().getReporterId()))) {
reportedOnConsumer.getHypervisorId().setReporterId(jobReporterId);
}
else if (jobReporterId == null) {
log.debug("hypervisor checkin reported asynchronously without reporter id " +
"for hypervisor:{} of owner:{}", hypervisorId, ownerKey);
}
}
return new Result(result, hypervisorKnownConsumersMap);
}
private void parseHypervisorList(HypervisorUpdateJob.HypervisorList hypervisorList, Set<String> hosts,
Set<String> guests, Map<String, Consumer> incomingHosts) {
int emptyGuestIdCount = 0;
int emptyHypervisorIdCount = 0;
List<Consumer> l = hypervisorList.getHypervisors();
for (Iterator<Consumer> hypervisors = l.iterator(); hypervisors.hasNext();) {
Consumer hypervisor = hypervisors.next();
HypervisorId idWrapper = hypervisor.getHypervisorId();
if (idWrapper == null) {
continue;
}
String id = idWrapper.getHypervisorId();
if (id == null) {
continue;
}
if ("".equals(id)) {
hypervisors.remove();
emptyHypervisorIdCount++;
continue;
}
incomingHosts.put(id, hypervisor);
hosts.add(id);
List<GuestId> guestsIdList = hypervisor.getGuestIds();
if (guestsIdList == null || guestsIdList.isEmpty()) {
continue;
}
for (Iterator<GuestId> guestIds = guestsIdList.iterator(); guestIds.hasNext();) {
GuestId guestId = guestIds.next();
if (StringUtils.isEmpty(guestId.getGuestId())) {
guestIds.remove();
emptyGuestIdCount++;
}
else {
guests.add(guestId.getGuestId());
}
}
}
if (emptyHypervisorIdCount > 0) {
log.debug("Ignoring {} hypervisors with empty hypervisor IDs", emptyHypervisorIdCount);
}
if (emptyGuestIdCount > 0) {
log.debug("Ignoring {} empty/null guestId(s)", emptyGuestIdCount);
}
}
/*
* Create a new hypervisor type consumer to represent the incoming hypervisorId
*/
private Consumer createConsumerForHypervisorId(String incHypervisorId, String reporterId,
Owner owner, Principal principal, Consumer incoming) {
Consumer consumer = new Consumer();
if (incoming.getName() != null) {
consumer.setName(incoming.getName());
}
else {
consumer.setName(sanitizeHypervisorId(incHypervisorId));
}
consumer.setType(hypervisorType);
consumer.setFact("uname.machine", "x86_64");
consumer.setGuestIds(new ArrayList<>());
consumer.setLastCheckin(new Date());
consumer.setOwner(owner);
consumer.setAutoheal(true);
consumer.setCanActivate(subAdapter.canActivateSubscription(consumer));
if (owner.getDefaultServiceLevel() != null) {
consumer.setServiceLevel(owner.getDefaultServiceLevel());
}
else {
consumer.setServiceLevel("");
}
if (principal.getUsername() != null) {
consumer.setUsername(principal.getUsername());
}
consumer.setEntitlementCount(0L);
// TODO: Refactor this to not call resource methods directly
consumerResource.sanitizeConsumerFacts(consumer);
// Create HypervisorId
HypervisorId hypervisorId = new HypervisorId(consumer, owner, incHypervisorId);
hypervisorId.setReporterId(reporterId);
consumer.setHypervisorId(hypervisorId);
// TODO: Refactor this to not call resource methods directly
consumerResource.checkForFactsUpdate(consumer, incoming);
return consumer;
}
/*
* Make sure the HypervisorId is a valid consumer name.
*/
private String sanitizeHypervisorId(String incHypervisorId) {
// Same validation as consumerResource.checkConsumerName
if (incHypervisorId.indexOf('#') == 0) {
log.debug("Hypervisor id cannot begin with # character");
incHypervisorId = incHypervisorId.substring(1);
}
int max = Consumer.MAX_LENGTH_OF_CONSUMER_NAME;
if (incHypervisorId.length() > max) {
log.debug("Hypervisor id too long, truncating");
incHypervisorId = incHypervisorId.substring(0, max);
}
return incHypervisorId;
}
/**
* Result of hypervisor update operation
*/
public static class Result {
private final HypervisorUpdateResultDTO result;
private final VirtConsumerMap hypervisorKnownConsumersMap;
Result(
final HypervisorUpdateResultDTO result,
final VirtConsumerMap hypervisorKnownConsumersMap) {
this.result = result;
this.hypervisorKnownConsumersMap = hypervisorKnownConsumersMap;
}
public HypervisorUpdateResultDTO getResult() {
return result;
}
public VirtConsumerMap getKnownConsumers() {
return hypervisorKnownConsumersMap;
}
}
}