use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState 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.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class TestAWSEnumerationDocumentCountInLongRun method storeDocumentLinksFromNetworkInterfaceStates.
/**
* Gets and stores network interface ids from network interface links and security group links, subnet
* links by querying network interface ids.
*/
private void storeDocumentLinksFromNetworkInterfaceStates() {
// If there are no network interface links, return.
if (this.networkInterfaceLinks.isEmpty()) {
return;
}
// Get network interface IDs from network interface links.
for (String s : this.networkInterfaceLinks) {
Operation op = Operation.createGet(UriUtils.buildUri(this.host.getUri(), s)).setReferer(this.host.getUri());
Operation response = this.host.waitForResponse(op);
Assert.assertTrue("Error retrieving network interface IDs", response.getStatusCode() == 200);
NetworkInterfaceState state = response.getBody(NetworkInterfaceState.class);
this.networkInterfaceIds.add(state.id);
}
// Query all network interface documents associated with list of network interface IDs.
QueryTask.Query networkInterfaceQuery = QueryTask.Query.Builder.create().addKindFieldClause(NetworkInterfaceState.class).addInClause(NetworkInterfaceState.FIELD_NAME_ID, this.networkInterfaceIds).build();
QueryTask q = QueryTask.Builder.createDirectTask().setQuery(networkInterfaceQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).build();
Operation queryNetworkInterface = QueryUtils.createQueryTaskOperation(this.host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
Operation queryResponse = this.host.waitForResponse(queryNetworkInterface);
Assert.assertTrue("Error retrieving network interface states", queryResponse.getStatusCode() == 200);
QueryTask qt = queryResponse.getBody(QueryTask.class);
// Store security group links and subnet links.
for (String documentLink : qt.results.documentLinks) {
NetworkInterfaceState nis = Utils.fromJson(qt.results.documents.get(documentLink), NetworkInterfaceState.class);
this.securityGroupLinks.addAll(nis.securityGroupLinks);
this.subnetLinks.add(nis.subnetLink);
}
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method doCreateUpdateNics.
/**
* Manages creating and updating Network Interfaces resources based on network interfaces
* associated with virtual machines.
*/
private DeferredResult<List<NetworkInterfaceState>> doCreateUpdateNics(EnumerationContext ctx, Map<String, String> subnetPerNicId, List<Pair<NetworkInterfaceInner, String>> remoteNics) {
Map<String, Pair<NetworkInterfaceInner, String>> remoteStates = remoteNics.stream().filter(p -> p.getLeft() != null).collect(Collectors.toMap(p -> p.getLeft().id(), p -> p));
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(NetworkInterfaceState.class).addInClause(NetworkInterfaceState.FIELD_NAME_ID, remoteStates.keySet());
QueryByPages<NetworkInterfaceState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), NetworkInterfaceState.class, ctx.parentCompute.tenantLinks, // endpointLink
null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return queryLocalStates.collectDocuments(Collectors.toList()).thenCompose(localNics -> requestCreateUpdateNic(localNics, remoteStates, ctx, subnetPerNicId, remoteNics));
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateNics.
/**
* Deletes stale network interface states that are deleted from the remote.
*/
private DeferredResult<List<Operation>> disassociateNics(EnumerationContext ctx, List<String> remoteNicIds, List<NetworkInterfaceState> allLocalNics) {
List<DeferredResult<Operation>> updateOps = new ArrayList<>();
allLocalNics.stream().filter(localNic -> !remoteNicIds.contains(localNic.id)).forEach(localNic -> {
Operation upOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, localNic);
if (upOp != null) {
CompletionHandler completionHandler = upOp.getCompletion();
upOp.setCompletion(null);
updateOps.add(sendWithDeferredResult(upOp).whenComplete(completionHandler::handle));
}
});
return DeferredResult.allOf(updateOps);
}
use of com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateNicHelper.
/**
* Helper for deleting stale network interfaces.
*/
private DeferredResult<List<Operation>> disassociateNicHelper(List<String> remoteNicIds, EnumerationContext ctx) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(NetworkInterfaceState.class);
QueryByPages<NetworkInterfaceState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), NetworkInterfaceState.class, ctx.parentCompute.tenantLinks, ctx.request.endpointLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return queryLocalStates.collectDocuments(Collectors.toList()).thenCompose(allLocalNics -> disassociateNics(ctx, remoteNicIds, allLocalNics));
}
Aggregations