use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.
the class AWSNetworkUtils method removeNetworkLinkAndDocument.
/**
* Remove a compute state's networkLink and delete the link's corresponding document
*
* @param service
* Service to issue the patch to.
* @param computeState
* The compute state to be updated.
* @param networkLink
* The network link need to be removed.
* @param enumerationOperations
* The operation list to store the operations.
* @return
*/
public static void removeNetworkLinkAndDocument(StatelessService service, ComputeState computeState, String networkLink, List<Operation> enumerationOperations) {
// create a PATCH to remove one ComputeState's networkLink
Map<String, Collection<Object>> collectionsMap = new HashMap<>();
Collection<Object> networkLinksToBeRemoved = new ArrayList<>(Arrays.asList(networkLink));
collectionsMap.put(ComputeState.FIELD_NAME_NETWORK_INTERFACE_LINKS, networkLinksToBeRemoved);
ServiceStateCollectionUpdateRequest collectionRemovalBody = ServiceStateCollectionUpdateRequest.create(null, collectionsMap);
Operation removeNetworkLinkOperation = Operation.createPatch(UriUtils.buildUri(service.getHost(), computeState.documentSelfLink)).setBody(collectionRemovalBody).setReferer(service.getUri());
enumerationOperations.add(removeNetworkLinkOperation);
// create a DELETE to remove that networkLink's corresponding document
Operation removeNetworkLinkDocumentOperation = Operation.createDelete(UriUtils.buildUri(service.getHost(), networkLink)).setReferer(service.getUri());
enumerationOperations.add(removeNetworkLinkDocumentOperation);
}
use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method populateUpdateOperations.
private void populateUpdateOperations(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
if (context.request.instancesToBeUpdated == null || context.request.instancesToBeUpdated.size() == 0) {
logFine(() -> "No local compute states to be updated");
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
} else {
logFine(() -> String.format("Need to update %d local compute states", context.request.instancesToBeUpdated.size()));
for (String instanceId : context.request.instancesToBeUpdated.keySet()) {
Instance instance = context.request.instancesToBeUpdated.get(instanceId);
ComputeState existingComputeState = context.request.computeStatesToBeUpdated.get(instanceId);
if (StringUtils.isEmpty(existingComputeState.endpointLink)) {
existingComputeState.endpointLink = context.request.endpointLink;
}
Set<String> endpointLinks = new HashSet<>();
if (existingComputeState.endpointLinks != null) {
endpointLinks.addAll(existingComputeState.endpointLinks);
}
endpointLinks.add(context.request.endpointLink);
// Calculate NICs delta - collection of NIC States to add, to update and to delete
Map<String, List<Integer>> deviceIndexesDelta = new HashMap<>();
Map<String, Map<String, Collection<Object>>> linksToNICSToAddOrRemove = new HashMap<>();
// The stopped or stopping instance does not have full network settings.
if (!AWSEnumerationUtils.instanceIsInStoppedState(instance)) {
// Get existing NetworkInterfaceStates for this ComputeState
List<NetworkInterfaceState> existingNicStates = context.request.nicStatesToBeUpdated.get(instanceId);
deviceIndexesDelta = calculateNICsDeviceIndexesDelta(instance, existingNicStates);
linksToNICSToAddOrRemove = addUpdateOrRemoveNICStates(context, instance, deviceIndexesDelta, existingComputeState.endpointLink, endpointLinks);
}
// Create dedicated PATCH operation for updating NIC Links {{
if (linksToNICSToAddOrRemove.get(ADD_NIC_STATES) != null || linksToNICSToAddOrRemove.get(REMOVE_NIC_STATES) != null) {
ServiceStateCollectionUpdateRequest updateComputeStateRequest = ServiceStateCollectionUpdateRequest.create(linksToNICSToAddOrRemove.get(ADD_NIC_STATES), linksToNICSToAddOrRemove.get(REMOVE_NIC_STATES));
Operation patchComputeStateNICLinks = Operation.createPatch(UriUtils.buildUri(this.getHost(), existingComputeState.documentSelfLink)).setBody(updateComputeStateRequest).setReferer(this.getUri());
context.enumerationOperations.add(patchComputeStateNICLinks);
}
// Update ComputeState
String zoneId = instance.getPlacement().getAvailabilityZone();
ZoneData zoneData = context.request.zones.get(zoneId);
ComputeState computeStateToBeUpdated = mapInstanceToComputeState(this.getHost(), instance, context.request.parentComputeLink, zoneData.computeLink, existingComputeState.resourcePoolLink != null ? existingComputeState.resourcePoolLink : context.request.resourcePoolLink, existingComputeState.endpointLink, endpointLinks, existingComputeState.descriptionLink, context.request.parentCDStatsAdapterReferences, context.internalTagLinksMap.get(ec2_instance.toString()), zoneData.regionId, zoneId, context.request.tenantLinks, null, false, null);
computeStateToBeUpdated.documentSelfLink = existingComputeState.documentSelfLink;
Operation patchComputeState = createPatchOperation(this, computeStateToBeUpdated, existingComputeState.documentSelfLink);
context.enumerationOperations.add(patchComputeState);
// Reconcile remote diskLinks with current diskLinks of existing computeState and
// update them with collection update request if needed.
List<String> remoteDiskLinks = context.diskLinksByInstances.get(instance);
List<String> currentDiskLinks = existingComputeState.diskLinks;
Collection<Object> linksToAdd = new ArrayList<>();
Collection<Object> linksToRemove = new ArrayList<>();
if (remoteDiskLinks == null && currentDiskLinks != null) {
linksToRemove.addAll(currentDiskLinks);
} else if (remoteDiskLinks != null && currentDiskLinks == null) {
linksToAdd.addAll(remoteDiskLinks);
} else if (remoteDiskLinks != null && currentDiskLinks != null) {
currentDiskLinks.stream().forEach(link -> {
if (!remoteDiskLinks.contains(link)) {
linksToRemove.add(link);
}
});
remoteDiskLinks.removeAll(currentDiskLinks);
linksToAdd.addAll(remoteDiskLinks);
}
// If existing computeState does not have an internal tag then we need to add it
// to tagLinks with collection update request patch.
String ec2ComputeInternalTagLink = context.internalTagLinksMap.get(ec2_instance.toString()).iterator().next();
// and send a combined collection update request for both of these.
if (existingComputeState.tagLinks == null || (existingComputeState.tagLinks != null && !existingComputeState.tagLinks.contains(ec2ComputeInternalTagLink)) || !linksToAdd.isEmpty() || !linksToRemove.isEmpty()) {
Map<String, Collection<Object>> itemsToAdd = new HashMap<>();
Map<String, Collection<Object>> itemsToRemove = new HashMap<>();
itemsToAdd.put(ComputeState.FIELD_NAME_DISK_LINKS, linksToAdd);
itemsToRemove.put(ComputeState.FIELD_NAME_DISK_LINKS, linksToRemove);
itemsToAdd.put(ComputeState.FIELD_NAME_TAG_LINKS, Collections.singletonList(ec2ComputeInternalTagLink));
ServiceStateCollectionUpdateRequest updateTagLinksAndDiskLinks = ServiceStateCollectionUpdateRequest.create(itemsToAdd, itemsToRemove);
context.enumerationOperations.add(Operation.createPatch(this.getHost(), existingComputeState.documentSelfLink).setBody(updateTagLinksAndDiskLinks).setReferer(this.getUri()));
}
}
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
}
}
use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method updateNICState.
/**
* For each NetworkInterfaceState, obtain the corresponding AWS NIC, and generate POST operation
* to update its private address
*/
private NetworkInterfaceState updateNICState(AWSComputeStateCreationContext context, Instance instance, NetworkInterfaceState existingNicState) {
InstanceNetworkInterface awsNic = instance.getNetworkInterfaces().stream().filter(currentAwsNic -> currentAwsNic.getAttachment().getDeviceIndex() == existingNicState.deviceIndex).findFirst().orElse(null);
// create a new NetworkInterfaceState for updating the address
NetworkInterfaceState updateNicState = new NetworkInterfaceState();
if (StringUtils.isEmpty(updateNicState.endpointLink)) {
updateNicState.endpointLink = context.request.endpointLink;
}
// endpoint link.
if (existingNicState.endpointLinks == null) {
updateNicState.endpointLinks = new HashSet<>();
} else {
updateNicState.endpointLinks = existingNicState.endpointLinks;
}
updateNicState.endpointLinks.add(context.request.endpointLink);
updateNicState.address = awsNic.getPrivateIpAddress();
if (context.request.enumeratedSecurityGroups != null) {
for (GroupIdentifier awsSG : awsNic.getGroups()) {
// we should have updated the list of SG Ids before this step and should have
// ensured that all the SGs exist locally
String securityGroupLink = context.request.enumeratedSecurityGroups.securityGroupStates.get(awsSG.getGroupId());
if (securityGroupLink == null || securityGroupLink.isEmpty()) {
continue;
}
if (updateNicState.securityGroupLinks == null) {
updateNicState.securityGroupLinks = new ArrayList<>();
}
updateNicState.securityGroupLinks.add(securityGroupLink);
}
}
// create update operation and add it for batch execution on the next stage
Operation updateNicOperation = createPatchOperation(this, updateNicState, existingNicState.documentSelfLink);
context.enumerationOperations.add(updateNicOperation);
// If existing network state does not have an internal tag then create dedicated
// patch to update the internal tag link.
String networkInterfaceInternalTagLink = context.internalTagLinksMap.get(ec2_net_interface.toString()).iterator().next();
if (existingNicState.tagLinks == null || (existingNicState.tagLinks != null && !existingNicState.tagLinks.contains(networkInterfaceInternalTagLink))) {
Map<String, Collection<Object>> collectionsToAddMap = Collections.singletonMap(NetworkInterfaceState.FIELD_NAME_TAG_LINKS, Collections.singletonList(networkInterfaceInternalTagLink));
Map<String, Collection<Object>> collectionsToRemoveMap = Collections.singletonMap(NetworkInterfaceState.FIELD_NAME_TAG_LINKS, Collections.emptyList());
ServiceStateCollectionUpdateRequest updateTagLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAddMap, collectionsToRemoveMap);
context.enumerationOperations.add(Operation.createPatch(this.getHost(), existingNicState.documentSelfLink).setReferer(this.getUri()).setBody(updateTagLinksRequest));
}
return updateNicState;
}
use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.
the class AWSS3StorageEnumerationAdapterService method createDiskStates.
/**
* Creates the disk states that represent the buckets received from AWS during
* enumeration. Fields currently being enumerated for S3 are all immutable on AWS side, hence we only create
* disks and don't patch to them in subsequent except for changes in tagLinks.
*/
private void createDiskStates(S3StorageEnumerationContext aws, S3StorageEnumerationSubStage next) {
// For all the disks to be created, we filter them based on whether we were able to find the correct
// region for the disk using getBucketTaggingConfiguration() call and then map them and create operations.
// Filtering is done to avoid creating disk states with null region (since we don't PATCH region field
// after creating the disk, we need to ensure that disk state is initially created with the correct region).
// kick off the operation using a JOIN
List<DiskState> diskStatesToBeCreated = new ArrayList<>();
aws.bucketsToBeCreated.stream().filter(bucket -> aws.regionsByBucketName.containsKey(bucket.getName())).forEach(bucket -> {
diskStatesToBeCreated.add(mapBucketToDiskState(bucket, aws));
});
diskStatesToBeCreated.forEach(diskState -> aws.enumerationOperations.add(createPostOperation(this, diskState, DiskService.FACTORY_LINK)));
this.logFine(() -> String.format("Creating %d S3 disks", aws.bucketsToBeCreated.size()));
// For those disk states which do not have the tagLink, add the tagLink by PATCHing those states.
if (aws.internalTypeTagSelfLink != null) {
aws.diskStatesToBeUpdatedByBucketName.entrySet().stream().filter(diskMap -> diskMap.getValue().tagLinks == null || !diskMap.getValue().tagLinks.contains(aws.internalTypeTagSelfLink)).forEach(diskMap -> {
Map<String, Collection<Object>> collectionsToAddMap = Collections.singletonMap(DiskState.FIELD_NAME_TAG_LINKS, Collections.singletonList(aws.internalTypeTagSelfLink));
Map<String, Collection<Object>> collectionsToRemoveMap = Collections.singletonMap(DiskState.FIELD_NAME_TAG_LINKS, Collections.emptyList());
ServiceStateCollectionUpdateRequest updateTagLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAddMap, collectionsToRemoveMap);
aws.enumerationOperations.add(Operation.createPatch(this.getHost(), diskMap.getValue().documentSelfLink).setReferer(aws.service.getUri()).setBody(updateTagLinksRequest));
});
}
// update endpointLinks
aws.diskStatesToBeUpdatedByBucketName.entrySet().stream().filter(diskMap -> diskMap.getValue().endpointLinks == null || !diskMap.getValue().endpointLinks.contains(aws.request.original.endpointLink)).forEach(diskMap -> {
Map<String, Collection<Object>> collectionsToAddMap = Collections.singletonMap(DiskState.FIELD_NAME_ENDPOINT_LINKS, Collections.singletonList(aws.request.original.endpointLink));
Map<String, Collection<Object>> collectionsToRemoveMap = Collections.singletonMap(DiskState.FIELD_NAME_ENDPOINT_LINKS, Collections.emptyList());
ServiceStateCollectionUpdateRequest updateEndpointLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAddMap, collectionsToRemoveMap);
aws.enumerationOperations.add(Operation.createPatch(this.getHost(), diskMap.getValue().documentSelfLink).setReferer(aws.service.getUri()).setBody(updateEndpointLinksRequest));
});
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error creating/updating disk %s", Utils.toString(exc)));
aws.subStage = S3StorageEnumerationSubStage.DELETE_DISKS;
handleReceivedEnumerationData(aws);
return;
}
ox.entrySet().stream().forEach(operationEntry -> {
aws.diskStatesEnumerated.add(operationEntry.getValue().getBody(DiskState.class));
});
this.logFine(() -> "Successfully created and updated all the disk states.");
aws.subStage = next;
handleReceivedEnumerationData(aws);
};
if (aws.enumerationOperations.isEmpty()) {
aws.subStage = next;
handleReceivedEnumerationData(aws);
return;
}
OperationJoin joinOp = OperationJoin.create(aws.enumerationOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
}
use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.
the class VSphereAdapterInstanceService method updateComputeStateDiskLinks.
/**
* Remove diskLink from ComputeState by sending a ServiceStateCollectionUpdateRequest.
*/
public void updateComputeStateDiskLinks(ProvisionContext ctx, ComputeState computeState) {
Map<String, Collection<Object>> collectionsToRemove = Collections.singletonMap(ComputeService.ComputeState.FIELD_NAME_DISK_LINKS, new ArrayList<>(computeState.diskLinks));
ServiceStateCollectionUpdateRequest updateDiskLinksRequest = ServiceStateCollectionUpdateRequest.create(null, collectionsToRemove);
Operation.createPatch(PhotonModelUriUtils.createInventoryUri(this.getHost(), computeState.documentSelfLink)).setBody(updateDiskLinksRequest).setReferer(this.getUri()).setCompletion((op, t) -> {
if (t != null) {
log(Level.INFO, "Not able to update the compute state with the detached " + "disks %s", t.getMessage());
}
ctx.mgr.finishTask();
}).sendWith(this.getHost());
}
Aggregations