Search in sources :

Example 16 with NetworkInterfaceState

use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.

the class VSphereAdapterInstanceService method createUpdateIPOperationsForComputeAndNics.

private List<Operation> createUpdateIPOperationsForComputeAndNics(String computeLink, String ip, Map<String, List<String>> ipV4Addresses, ProvisionContext ctx) {
    List<Operation> updateIpAddressOperations = new ArrayList<>();
    if (ip != null) {
        ComputeState state = new ComputeState();
        state.address = ip;
        // update compute
        Operation updateIpAddress = Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getHost(), computeLink)).setBody(state);
        updateIpAddressOperations.add(updateIpAddress);
    }
    if (ipV4Addresses != null) {
        int sizeIpV4Addresses = ipV4Addresses.size();
        for (NetworkInterfaceStateWithDetails nic : ctx.nics) {
            String deviceKey = null;
            deviceKey = VmOverlay.getDeviceKey(nic);
            if (deviceKey == null && nic.deviceIndex < sizeIpV4Addresses) {
                deviceKey = Integer.toString(nic.deviceIndex);
            }
            if (deviceKey != null) {
                List<String> ipsV4 = ipV4Addresses.containsKey(deviceKey) ? ipV4Addresses.get(deviceKey) : Collections.emptyList();
                if (ipsV4.size() > 0) {
                    NetworkInterfaceState patchNic = new NetworkInterfaceState();
                    // if nic has multiple ip addresses for ipv4 only pick 1st ip address
                    patchNic.address = ipsV4.get(0);
                    Operation updateAddressNetWorkInterface = Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getHost(), nic.documentSelfLink)).setBody(patchNic);
                    updateIpAddressOperations.add(updateAddressNetWorkInterface);
                } else {
                    log(Level.WARNING, "Address is not going to be updated in network " + "interface state: [%], deviceKey: [%s] was not " + "found in " + "ipV4Addresses: " + "[%s]", nic.documentSelfLink, deviceKey, ipV4Addresses.keySet());
                }
            } else {
                log(Level.WARNING, "Address is not going to be updated in network interface " + "state: [%s] deviceKey is null", nic.documentSelfLink);
            }
        }
    }
    return updateIpAddressOperations;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) ArrayList(java.util.ArrayList) NetworkInterfaceStateWithDetails(com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails) Operation(com.vmware.xenon.common.Operation)

Example 17 with NetworkInterfaceState

use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method createNewVm.

static void createNewVm(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, VmOverlay vm) {
    ComputeDescription desc = makeDescriptionForVm(service, enumerationProgress, vm);
    desc.tenantLinks = enumerationProgress.getTenantLinks();
    Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), ComputeDescriptionService.FACTORY_LINK)).setBody(desc).sendWith(service);
    ComputeState state = makeVmFromResults(enumerationProgress, vm);
    state.descriptionLink = desc.documentSelfLink;
    state.tenantLinks = enumerationProgress.getTenantLinks();
    List<Operation> operations = new ArrayList<>();
    VsphereEnumerationHelper.submitWorkToVSpherePool(service, () -> {
        VsphereEnumerationHelper.populateTags(service, enumerationProgress, vm, state);
        state.networkInterfaceLinks = new ArrayList<>();
        Map<String, List<String>> nicToIPv4Addresses = vm.getMapNic2IpV4Addresses();
        for (VirtualEthernetCard nic : vm.getNics()) {
            VirtualDeviceBackingInfo backing = nic.getBacking();
            if (backing instanceof VirtualEthernetCardNetworkBackingInfo) {
                VirtualEthernetCardNetworkBackingInfo veth = (VirtualEthernetCardNetworkBackingInfo) backing;
                NetworkInterfaceState iface = new NetworkInterfaceState();
                iface.networkLink = enumerationProgress.getNetworkTracker().getSelfLink(veth.getNetwork());
                iface.name = nic.getDeviceInfo().getLabel();
                iface.documentSelfLink = buildUriPath(NetworkInterfaceService.FACTORY_LINK, service.getHost().nextUUID());
                iface.address = getPrimaryIPv4Address(nic, nicToIPv4Addresses);
                CustomProperties.of(iface).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink());
                Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), NetworkInterfaceService.FACTORY_LINK)).setBody(iface).sendWith(service);
                state.networkInterfaceLinks.add(iface.documentSelfLink);
            } else if (backing instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
                VirtualEthernetCardDistributedVirtualPortBackingInfo veth = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backing;
                String portgroupKey = veth.getPort().getPortgroupKey();
                Operation op = addNewInterfaceState(service, enumerationProgress, state, portgroupKey, InterfaceStateMode.INTERFACE_STATE_WITH_DISTRIBUTED_VIRTUAL_PORT, SubnetState.class, SubnetState.class, getPrimaryIPv4Address(nic, nicToIPv4Addresses));
                if (op != null) {
                    operations.add(op);
                }
            } else if (backing instanceof VirtualEthernetCardOpaqueNetworkBackingInfo) {
                VirtualEthernetCardOpaqueNetworkBackingInfo veth = (VirtualEthernetCardOpaqueNetworkBackingInfo) backing;
                String opaqueNetworkId = veth.getOpaqueNetworkId();
                Operation op = addNewInterfaceState(service, enumerationProgress, state, opaqueNetworkId, InterfaceStateMode.INTERFACE_STATE_WITH_OPAQUE_NETWORK, NetworkState.class, NetworkState.class, getPrimaryIPv4Address(nic, nicToIPv4Addresses));
                if (op != null) {
                    operations.add(op);
                }
            } else {
                // TODO add support for DVS
                service.logFine(() -> String.format("Will not add nic of type %s", backing.getClass().getName()));
            }
        }
        // Process all the disks attached to the VM
        List<VirtualDevice> disks = vm.getDisks();
        if (CollectionUtils.isNotEmpty(disks)) {
            state.diskLinks = new ArrayList<>(disks.size());
            for (VirtualDevice device : disks) {
                Operation vdOp = processVirtualDevice(service, null, device, enumerationProgress, state.diskLinks, VimUtils.convertMoRefToString(vm.getId()), null);
                if (vdOp != null) {
                    operations.add(vdOp);
                }
            }
        }
        service.logFine(() -> String.format("Found new VM %s", vm.getInstanceUuid()));
        if (operations.isEmpty()) {
            Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), ComputeService.FACTORY_LINK)).setBody(state).setCompletion(trackVm(enumerationProgress)).sendWith(service);
        } else {
            OperationJoin.create(operations).setCompletion((operationMap, exception) -> {
                Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), ComputeService.FACTORY_LINK)).setBody(state).setCompletion(updateVirtualDisksAndTrackVm(service, enumerationProgress, operationMap)).sendWith(service);
            }).sendWith(service);
        }
    });
}
Also used : ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) VirtualDevice(com.vmware.vim25.VirtualDevice) NsxProperties(com.vmware.photon.controller.model.adapters.vsphere.network.NsxProperties) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DiskStateExpanded(com.vmware.photon.controller.model.resources.DiskService.DiskStateExpanded) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ClientUtils.handleVirtualDiskUpdate(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.handleVirtualDiskUpdate) List(java.util.List) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) ClientUtils.handleVirtualDeviceUpdate(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.handleVirtualDeviceUpdate) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) DiskService(com.vmware.photon.controller.model.resources.DiskService) VirtualFloppy(com.vmware.vim25.VirtualFloppy) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceCleanRequest(com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) ArrayList(java.util.ArrayList) VirtualCdrom(com.vmware.vim25.VirtualCdrom) ClientUtils.findMatchingDiskState(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.findMatchingDiskState) CollectionUtils(org.apache.commons.collections.CollectionUtils) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TYPE_PORTGROUP(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames.TYPE_PORTGROUP) VirtualDisk(com.vmware.vim25.VirtualDisk) UriUtils.buildUriPath(com.vmware.xenon.common.UriUtils.buildUriPath) InterfaceStateMode(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.InterfaceStateMode) ObjectUpdate(com.vmware.vim25.ObjectUpdate) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) ArrayList(java.util.ArrayList) VirtualDevice(com.vmware.vim25.VirtualDevice) Operation(com.vmware.xenon.common.Operation) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) List(java.util.List) ArrayList(java.util.ArrayList) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard)

Example 18 with NetworkInterfaceState

use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState 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 19 with NetworkInterfaceState

use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState 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 20 with NetworkInterfaceState

use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState 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;
}
Also used : NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) Collection(java.util.Collection) 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) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Aggregations

NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)32 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)17 Operation (com.vmware.xenon.common.Operation)15 ArrayList (java.util.ArrayList)14 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)13 URI (java.net.URI)10 HashSet (java.util.HashSet)10 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)9 DeferredResult (com.vmware.xenon.common.DeferredResult)9 QueryTask (com.vmware.xenon.services.common.QueryTask)9 List (java.util.List)9 Map (java.util.Map)9 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)8 HashMap (java.util.HashMap)8 Collectors (java.util.stream.Collectors)8 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)7 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)7 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)7 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)7 OperationJoin (com.vmware.xenon.common.OperationJoin)7