Search in sources :

Example 1 with ZoneData

use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData 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);
    }
}
Also used : AWSEnumerationUtils.mapInstanceToComputeState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Instance(com.amazonaws.services.ec2.model.Instance) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) ArrayList(java.util.ArrayList) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) AWSEnumerationUtils.getRepresentativeListOfCDsFromInstanceList(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getRepresentativeListOfCDsFromInstanceList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 2 with ZoneData

use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData in project photon-model by vmware.

the class AWSComputeStateCreationAdapterService method populateCreateOperations.

/**
 * Method to create Compute States associated with the instances received from the AWS host.
 */
private void populateCreateOperations(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
    if (context.request.instancesToBeCreated == null || context.request.instancesToBeCreated.size() == 0) {
        logFine(() -> "No local compute states to be created");
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
    } else {
        logFine(() -> String.format("Need to create %d local compute states", context.request.instancesToBeCreated.size()));
        for (int i = 0; i < context.request.instancesToBeCreated.size(); i++) {
            Instance instance = context.request.instancesToBeCreated.get(i);
            String zoneId = instance.getPlacement().getAvailabilityZone();
            ZoneData zoneData = context.request.zones.get(zoneId);
            String regionId = zoneData.regionId;
            InstanceDescKey descKey = InstanceDescKey.build(regionId, zoneId, instance.getInstanceType());
            Set<String> endpointLinks = new HashSet<>();
            endpointLinks.add(context.request.endpointLink);
            ComputeState computeStateToBeCreated = mapInstanceToComputeState(this.getHost(), instance, context.request.parentComputeLink, zoneData.computeLink, context.request.resourcePoolLink, null, endpointLinks, context.computeDescriptionMap.get(descKey), context.request.parentCDStatsAdapterReferences, context.internalTagLinksMap.get(ec2_instance.toString()), regionId, zoneId, context.request.tenantLinks, context.createdExternalTags, true, context.diskLinksByInstances.get(instance));
            computeStateToBeCreated.networkInterfaceLinks = new ArrayList<>();
            if (!AWSEnumerationUtils.instanceIsInStoppedState(instance)) {
                // ComputeState to be created to the NIC State
                for (InstanceNetworkInterface awsNic : instance.getNetworkInterfaces()) {
                    if (context.request.enumeratedNetworks != null && context.request.enumeratedNetworks.subnets != null && context.request.enumeratedNetworks.subnets.containsKey(awsNic.getSubnetId())) {
                        NetworkInterfaceState nicState = createNICStateAndDescription(context, awsNic, null, endpointLinks);
                        computeStateToBeCreated.networkInterfaceLinks.add(UriUtils.buildUriPath(NetworkInterfaceService.FACTORY_LINK, nicState.documentSelfLink));
                    }
                }
            }
            Operation postComputeState = createPostOperation(this, computeStateToBeCreated, ComputeService.FACTORY_LINK);
            context.enumerationOperations.add(postComputeState);
        }
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
    }
}
Also used : AWSEnumerationUtils.mapInstanceToComputeState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) Instance(com.amazonaws.services.ec2.model.Instance) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) InstanceDescKey(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey) AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) HashSet(java.util.HashSet)

Example 3 with ZoneData

use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData in project photon-model by vmware.

the class TestAWSEnumerationUtils method testGetComputeDescriptionKeyFromAWSInstance.

@Test
public void testGetComputeDescriptionKeyFromAWSInstance() throws Throwable {
    Map<String, ZoneData> zones = new HashMap<>();
    zones.put(AWS_ZONE_ID, ZoneData.build(AWS_REGION_ID, AWS_ZONE_ID, ""));
    Instance awsInstance = new Instance();
    awsInstance.setInstanceId(AWS_INSTANCE_ID);
    Placement placement = new Placement();
    placement.setAvailabilityZone(AWS_ZONE_ID);
    awsInstance.setPlacement(placement);
    String regionId = getRegionId(awsInstance);
    awsInstance.setInstanceType(AWS_INSTANCE_TYPE);
    awsInstance.setVpcId(AWS_VPC_ID);
    assertEquals(AWS_REGION_ID, regionId);
    InstanceDescKey computeDescriptionKey = getKeyForComputeDescriptionFromInstance(awsInstance, zones);
    assertEquals(AWS_COMPUTE_DESCRIPTION_KEY, computeDescriptionKey);
}
Also used : ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) HashMap(java.util.HashMap) AWSEnumerationUtils.getKeyForComputeDescriptionFromInstance(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getKeyForComputeDescriptionFromInstance) Instance(com.amazonaws.services.ec2.model.Instance) Placement(com.amazonaws.services.ec2.model.Placement) InstanceDescKey(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey) Test(org.junit.Test)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)3 ZoneData (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData)3 InstanceDescKey (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey)2 AWSEnumerationUtils.mapInstanceToComputeState (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState)2 AdapterUtils.createDeleteOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation)2 AdapterUtils.createPatchOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation)2 AdapterUtils.createPostOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation)2 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)2 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)2 Operation (com.vmware.xenon.common.Operation)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 InstanceNetworkInterface (com.amazonaws.services.ec2.model.InstanceNetworkInterface)1 Placement (com.amazonaws.services.ec2.model.Placement)1 AWSEnumerationUtils.getKeyForComputeDescriptionFromInstance (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getKeyForComputeDescriptionFromInstance)1 AWSEnumerationUtils.getRepresentativeListOfCDsFromInstanceList (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getRepresentativeListOfCDsFromInstanceList)1 ServiceStateCollectionUpdateRequest (com.vmware.xenon.common.ServiceStateCollectionUpdateRequest)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1