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;
}
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);
}
});
}
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);
}
}
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);
}
}
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;
}
Aggregations