use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class TestAWSEnumerationDocumentCountInLongRun method storeDocumentLinksFromComputeStates.
/**
* Gets and stores resource pool links and network interface links by querying
* given instance IDs.
* @param instanceIdList List of instance IDs provisioned by the test
*/
private void storeDocumentLinksFromComputeStates(List<String> instanceIdList) {
// Query to get all compute state documents associated with list of instance IDs.
QueryTask.Query computeStateQuery = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addInClause(ComputeState.FIELD_NAME_ID, instanceIdList).build();
QueryTask q = QueryTask.Builder.createDirectTask().setQuery(computeStateQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).build();
Operation queryComputeState = QueryUtils.createQueryTaskOperation(this.host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
Operation queryResponse = this.host.waitForResponse(queryComputeState);
Assert.assertTrue("Error retrieving compute states", queryResponse.getStatusCode() == 200);
QueryTask qt = queryResponse.getBody(QueryTask.class);
// Store all compute links
this.computeStateLinks.addAll(qt.results.documentLinks);
// Store resource pool links and network links from all compute states.
for (String documentLink : this.computeStateLinks) {
ComputeState cs = Utils.fromJson(qt.results.documents.get(documentLink), ComputeState.class);
this.resourcePoolLinks.add(cs.resourcePoolLink);
this.networkInterfaceLinks.addAll(cs.networkInterfaceLinks);
}
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method createTags.
/**
* POSTs all tags for newly discovered instances. Even if some tags already exist we rely on
* IDEMPOTENT_POST behaviour and POST them again. All tags that got created successfully are
* stored in createdExternalTags list.
*/
private void createTags(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
// Get all tags from the instances to be created
Set<Tag> create = context.request.instancesToBeCreated.stream().flatMap(i -> i.getTags().stream()).collect(Collectors.toSet());
// Put them in a set to remove the duplicates
Set<Tag> allTags = new HashSet<>();
allTags.addAll(create);
// POST each of the tags. If a tag exists it won't be created again. We don't want the name
// tags, so filter them out
List<Operation> operations = new ArrayList<>();
Map<Long, Tag> tagsCreationOperationIdsMap = new ConcurrentHashMap<>();
allTags.stream().filter(t -> !AWSConstants.AWS_TAG_NAME.equals(t.getKey())).forEach(t -> {
TagState tagState = newTagState(t.getKey(), t.getValue(), true, context.request.tenantLinks);
Operation op = Operation.createPost(this, TagService.FACTORY_LINK).setBody(tagState);
operations.add(op);
tagsCreationOperationIdsMap.put(op.getId(), t);
});
if (operations.isEmpty()) {
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
} else {
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null && !exs.isEmpty()) {
logSevere(() -> String.format("Error creating %s external tags for compute" + "states: %s", exs.size(), Utils.toString(exs.get(0))));
}
ops.values().stream().filter(operation -> operation.getStatusCode() == Operation.STATUS_CODE_OK || operation.getStatusCode() == Operation.STATUS_CODE_NOT_MODIFIED).forEach(operation -> {
if (tagsCreationOperationIdsMap.containsKey(operation.getId())) {
context.createdExternalTags.add(tagsCreationOperationIdsMap.get(operation.getId()));
}
});
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
}).sendWith(this);
}
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method addUpdateOrRemoveNICStates.
/**
* From the previously calculated NICs delta (based on whether the local state correspond to
* existing AWS object, the AWS object was deleted, or a new AWS object was added): 1) Create,
* update or delete NICState objects 2) Update the CS's references to the added or removed
* NetworkInterfaceStates
*/
private Map<String, Map<String, Collection<Object>>> addUpdateOrRemoveNICStates(AWSComputeStateCreationContext context, Instance instance, Map<String, List<Integer>> nicsDeviceIndexDeltaMap, String existingEndpointLink, Set<String> endpointLinks) {
List<NetworkInterfaceState> existingNicStates = context.request.nicStatesToBeUpdated.get(instance.getInstanceId());
// Generate operation for adding NIC state and description, and retain its link to add to
// the CS
List<Integer> deviceIndexesToAdd = nicsDeviceIndexDeltaMap.get(ADD_NIC_STATES);
Collection<Object> networkInterfaceLinksToBeAdded = instance.getNetworkInterfaces().stream().filter(awsNic -> deviceIndexesToAdd.contains(awsNic.getAttachment().getDeviceIndex()) && context.request.enumeratedNetworks != null && context.request.enumeratedNetworks.subnets != null && context.request.enumeratedNetworks.subnets.containsKey(awsNic.getSubnetId())).map(awsNic -> createNICStateAndDescription(context, awsNic, existingEndpointLink, endpointLinks)).map(addedNicState -> UriUtils.buildUriPath(NetworkInterfaceService.FACTORY_LINK, addedNicState.documentSelfLink)).collect(Collectors.toList());
// Generate operation for removing NIC states, and retain its link to remove from the CS {{
List<Integer> deviceIndexesToRemove = nicsDeviceIndexDeltaMap.get(REMOVE_NIC_STATES);
Collection<Object> networkInterfaceLinksToBeRemoved = deviceIndexesToRemove.stream().map(deviceIndexToRemove -> {
NetworkInterfaceState stateToDelete = existingNicStates.stream().filter(existNicState -> existNicState.deviceIndex == deviceIndexToRemove).findFirst().orElse(null);
return stateToDelete;
}).filter(existingNicState -> existingNicState != null).map(existingNicState -> deleteNICState(context, existingNicState)).map(existingNicState -> existingNicState.documentSelfLink).collect(Collectors.toList());
// Generate operation for updating NIC states, no links should be updated on CS in this case
List<Integer> deviceIndexesToUpdate = nicsDeviceIndexDeltaMap.get(UPDATE_NIC_STATES);
deviceIndexesToUpdate.stream().map(deviceIndexToUpdate -> existingNicStates.stream().filter(existNicState -> existNicState.deviceIndex == deviceIndexToUpdate).findFirst().orElse(null)).filter(existingNicState -> existingNicState != null).forEach(existingNicState -> updateNICState(context, instance, existingNicState));
if (context.request.nicStatesToBeDeleted.size() > 0 && context.request.nicStatesToBeDeleted.get(instance.getInstanceId()) != null) {
this.logInfo(() -> String.format("Compute %s failed to discover %d nics", instance.getInstanceId(), context.request.nicStatesToBeDeleted.get(instance.getInstanceId()).size()));
networkInterfaceLinksToBeRemoved.addAll(context.request.nicStatesToBeDeleted.get(instance.getInstanceId()));
}
Map<String, Map<String, Collection<Object>>> nicsDeltaMap = new HashMap<>();
// only add the collections to the delta map in case there is something to add/remove
if (!networkInterfaceLinksToBeRemoved.isEmpty()) {
Map<String, Collection<Object>> collectionsToRemoveMap = new HashMap<>();
collectionsToRemoveMap.put(ComputeState.FIELD_NAME_NETWORK_INTERFACE_LINKS, networkInterfaceLinksToBeRemoved);
nicsDeltaMap.put(REMOVE_NIC_STATES, collectionsToRemoveMap);
}
if (!networkInterfaceLinksToBeAdded.isEmpty()) {
Map<String, Collection<Object>> collectionsToAddMap = new HashMap<>();
collectionsToAddMap.put(ComputeState.FIELD_NAME_NETWORK_INTERFACE_LINKS, networkInterfaceLinksToBeAdded);
nicsDeltaMap.put(ADD_NIC_STATES, collectionsToAddMap);
}
return nicsDeltaMap;
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSEnumerationAdapterService method kickOffEnumerationWorkFlows.
/**
* Kicks off the enumeration flows for creation and deletion.
*/
public void kickOffEnumerationWorkFlows(EnumerationContext context, AWSEnumerationStages next) {
List<List<Operation>> enumOperations = new ArrayList<>();
if (!AWSUtils.isAwsClientMock()) {
ComputeEnumerateAdapterRequest awsS3EnumerationRequest = new ComputeEnumerateAdapterRequest(context.request, context.endpointAuth, context.parent, Regions.DEFAULT_REGION.getName());
Operation patchAWSS3StorageAdapterService = Operation.createPatch(this, AWSS3StorageEnumerationAdapterService.SELF_LINK).setBody(awsS3EnumerationRequest).setReferer(getHost().getUri());
enumOperations.add(Collections.singletonList(patchAWSS3StorageAdapterService));
}
for (String regionId : context.regions) {
List<Operation> enumOperationsForRegion = new ArrayList<>();
ComputeEnumerateAdapterRequest awsEnumerationRequest = new ComputeEnumerateAdapterRequest(context.request, context.endpointAuth, context.parent, regionId);
Operation patchAWSCreationAdapterService = Operation.createPatch(this, AWSEnumerationAndCreationAdapterService.SELF_LINK).setBody(awsEnumerationRequest).setReferer(this.getHost().getUri());
Operation patchAWSDeletionAdapterService = Operation.createPatch(this, AWSEnumerationAndDeletionAdapterService.SELF_LINK).setBody(awsEnumerationRequest).setReferer(getHost().getUri());
Operation patchAWSEBSStorageAdapterService = Operation.createPatch(this, AWSEBSStorageEnumerationAdapterService.SELF_LINK).setBody(awsEnumerationRequest).setReferer(getHost().getUri());
enumOperationsForRegion.add(patchAWSCreationAdapterService);
enumOperationsForRegion.add(patchAWSDeletionAdapterService);
enumOperationsForRegion.add(patchAWSEBSStorageAdapterService);
enumOperations.add(enumOperationsForRegion);
}
if (enumOperations.size() == 0) {
logFine(() -> "No enumeration tasks to run");
context.stage = next;
handleEnumerationRequest(context);
return;
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Error starting the enumeration workflows for AWS: %s", Utils.toString(exc)));
context.taskManager.patchTaskToFailure(exc.values().iterator().next());
return;
}
logInfo(() -> "Completed creation and deletion enumeration for compute and storage" + " states");
context.stage = next;
handleEnumerationRequest(context);
};
OperationSequence enumOp = OperationSequence.create(OperationJoin.create(enumOperations.get(0)));
for (int i = 1; i < enumOperations.size(); i++) {
enumOp = enumOp.next(OperationJoin.create(enumOperations.get(i)));
}
enumOp.setCompletion(joinCompletion);
enumOp.sendWith(getHost());
logFine(() -> "Started creation and deletion enumeration for AWS computes and storage");
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSEnumerationAdapterService method startHelperServices.
/**
* Starts the related services for the Enumeration Service
*/
public void startHelperServices(Operation startPost) {
Operation patchAWSEnumerationCreationService = Operation.createPatch(getHost(), AWSEnumerationAndCreationAdapterService.SELF_LINK).setReferer(getUri());
Operation patchAWSEnumerationDeletionService = Operation.createPatch(getHost(), AWSEnumerationAndDeletionAdapterService.SELF_LINK).setReferer(getUri());
Operation patchAWSEBSStorageEnumerationService = Operation.createPatch(getHost(), AWSEBSStorageEnumerationAdapterService.SELF_LINK).setReferer(getUri());
Operation patchAWSS3StorageEnumerationService = Operation.createPatch(getHost(), AWSS3StorageEnumerationAdapterService.SELF_LINK).setReferer(getUri());
getHost().startService(patchAWSEnumerationCreationService, new AWSEnumerationAndCreationAdapterService());
getHost().startService(patchAWSEnumerationDeletionService, new AWSEnumerationAndDeletionAdapterService());
getHost().startService(patchAWSEBSStorageEnumerationService, new AWSEBSStorageEnumerationAdapterService());
getHost().startService(patchAWSS3StorageEnumerationService, new AWSS3StorageEnumerationAdapterService());
getHost().startService(new AWSVolumeTypeDiscoveryService());
AdapterUtils.registerForServiceAvailability(getHost(), operation -> startPost.complete(), startPost::fail, LINKS);
}
Aggregations