use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method createUpdateNetworkStates.
/**
* Create new network states or update matching network states with the actual state in Azure.
*/
private void createUpdateNetworkStates(NetworkEnumContext context, NetworkEnumStages next) {
logFine(() -> "Create or update Network States with the actual state in Azure.");
if (context.virtualNetworks.size() == 0) {
logFine(() -> "No virtual networks found to create/update.");
handleSubStage(context, next);
return;
}
Stream<Operation> operations = context.virtualNetworks.values().stream().map(virtualNetwork -> {
NetworkState existingNetworkState = context.networkStates.get(virtualNetwork.id);
NetworkState networkState = buildNetworkState(context, virtualNetwork, existingNetworkState);
setTagLinksToResourceState(networkState, virtualNetwork.tags, true);
if (existingNetworkState == null) {
// set internal tags as tagLinks for networks to be newly created.
setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
} else {
// for already existing networks, add internal tags only if missing
if (networkState.tagLinks == null || networkState.tagLinks.isEmpty()) {
setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
} else {
context.networkInternalTagLinksSet.stream().filter(tagLink -> !networkState.tagLinks.contains(tagLink)).map(tagLink -> networkState.tagLinks.add(tagLink)).collect(Collectors.toSet());
}
}
CompletionHandler handler = (completedOp, failure) -> {
if (failure != null) {
// Process successful operations only.
logWarning(() -> String.format("Error: %s", failure.getMessage()));
return;
}
NetworkState result;
if (completedOp.getStatusCode() == Operation.STATUS_CODE_NOT_MODIFIED) {
// Use the original networkState as result
result = networkState;
} else {
result = completedOp.getBody(NetworkState.class);
}
context.networkStates.put(result.id, result);
};
return existingNetworkState != null ? // Update case.
Operation.createPatch(this, networkState.documentSelfLink).setBody(networkState).setCompletion(handler) : // Create case.
Operation.createPost(getHost(), NetworkService.FACTORY_LINK).setBody(networkState).setCompletion(handler);
});
OperationJoin.create(operations).setCompletion((ops, failures) -> {
// We don't want to fail the whole data collection if some of the so we don't care of
// any potential operation failures. They are already logged at individual operation
// level.
logFine(() -> "Finished updating network states.");
handleSubStage(context, next);
}).sendWith(this);
}
use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method buildNetworkState.
/**
* @param localNetworkState
* null to do a create, non null to update an existing network state
*/
private NetworkState buildNetworkState(NetworkEnumContext context, VirtualNetwork azureVirtualNetwork, NetworkState localNetworkState) {
NetworkState resultNetworkState = new NetworkState();
if (localNetworkState != null) {
resultNetworkState.id = localNetworkState.id;
resultNetworkState.authCredentialsLink = localNetworkState.authCredentialsLink;
resultNetworkState.documentSelfLink = localNetworkState.documentSelfLink;
resultNetworkState.groupLinks = localNetworkState.groupLinks;
resultNetworkState.resourcePoolLink = localNetworkState.resourcePoolLink;
resultNetworkState.tagLinks = localNetworkState.tagLinks;
resultNetworkState.endpointLinks = localNetworkState.endpointLinks;
} else {
resultNetworkState.id = azureVirtualNetwork.id;
resultNetworkState.authCredentialsLink = context.endpointAuth.documentSelfLink;
resultNetworkState.resourcePoolLink = context.request.resourcePoolLink;
}
resultNetworkState.name = azureVirtualNetwork.name;
resultNetworkState.regionId = azureVirtualNetwork.location;
if (StringUtils.isEmpty(resultNetworkState.endpointLink)) {
resultNetworkState.endpointLink = context.request.endpointLink;
}
resultNetworkState.computeHostLink = context.parentCompute.documentSelfLink;
AdapterUtils.addToEndpointLinks(resultNetworkState, context.request.endpointLink);
AddressSpace addressSpace = azureVirtualNetwork.properties.addressSpace;
if (addressSpace != null && addressSpace.addressPrefixes != null && addressSpace.addressPrefixes.size() > 0) {
// TODO: Get the first address prefix for now.
// Trim any whitespaces that might be presented (VSYM-4132).
resultNetworkState.subnetCIDR = addressSpace.addressPrefixes.get(0).trim();
}
// Add gateway as custom property in case gateway is defined
String gatewayId = AzureUtils.getVirtualNetworkGatewayId(azureVirtualNetwork);
if (gatewayId != null) {
resultNetworkState.customProperties = Collections.singletonMap(ComputeProperties.FIELD_VIRTUAL_GATEWAY, gatewayId);
logFine(() -> String.format("Added Gateway %s for Network State %s.", gatewayId, resultNetworkState.name));
}
// TODO: There is no Azure Network Adapter Service. Add a default reference since this is
// required field.
resultNetworkState.instanceAdapterReference = AdapterUriUtil.buildAdapterUri(getHost().getPort(), DEFAULT_INSTANCE_ADAPTER_REFERENCE);
String resourceGroupId = AzureUtils.getResourceGroupId(azureVirtualNetwork.id);
String resourceGroupStateLink = context.resourceGroupStates.get(resourceGroupId);
if (resourceGroupStateLink != null) {
if (resultNetworkState.groupLinks == null) {
resultNetworkState.groupLinks = new HashSet<>();
}
// Add if a resource group state with this name exists.
// If not then the resource group was not enumerated yet. The groupLink will be filled
// during the next enumeration cycle.
resultNetworkState.groupLinks.add(resourceGroupStateLink);
}
resultNetworkState.tenantLinks = context.parentCompute.tenantLinks;
return resultNetworkState;
}
use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method updateNetworkTagLinks.
private DeferredResult<NetworkEnumContext> updateNetworkTagLinks(NetworkEnumContext context) {
logFine(() -> "Create or update Network States' tags with the actual tags in Azure.");
if (context.virtualNetworks.size() == 0) {
logFine(() -> "No local networks to be updated so there are no tags to update.");
return DeferredResult.completed(context);
} else {
List<DeferredResult<Set<String>>> updateNetwLinksOps = new ArrayList<>();
// update tag links for the existing NetworkStates
for (String vnetId : context.networkStates.keySet()) {
if (!context.virtualNetworks.containsKey(vnetId)) {
// this is not a network to update
continue;
}
VirtualNetwork vNet = context.virtualNetworks.get(vnetId);
NetworkState existingNetworkState = context.networkStates.get(vnetId);
Map<String, String> remoteTags = new HashMap<>();
if (vNet.tags != null && !vNet.tags.isEmpty()) {
for (Entry<String, String> vNetTagEntry : vNet.tags.entrySet()) {
remoteTags.put(vNetTagEntry.getKey(), vNetTagEntry.getValue());
}
}
updateNetwLinksOps.add(updateLocalTagStates(this, existingNetworkState, remoteTags, null));
}
return DeferredResult.allOf(updateNetwLinksOps).thenApply(gnore -> context);
}
}
use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method createUpdateSubnetStates.
/**
* Create new subnet states or updates matching subnet states with the actual state in Azure.
*/
private void createUpdateSubnetStates(NetworkEnumContext context, NetworkEnumStages next) {
if (context.subnets.size() == 0) {
logFine(() -> "No network states available for update.");
handleSubStage(context, next);
return;
}
Stream<Operation> operations = context.subnets.keySet().stream().map(subnetId -> {
SubnetStateWithParentVNetId subnetStateWithParentVNetId = context.subnets.get(subnetId);
SubnetState subnetState = subnetStateWithParentVNetId.subnetState;
if (!context.subnetStates.containsKey(subnetId)) {
// set internal tags as tagLinks for subnets to be newly created.
setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
} else {
// for already existing subnets, add internal tags only if missing
if (subnetState.tagLinks == null || subnetState.tagLinks.isEmpty()) {
setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
} else {
context.subnetInternalTagLinksSet.stream().filter(tagLink -> !subnetState.tagLinks.contains(tagLink)).map(tagLink -> subnetState.tagLinks.add(tagLink)).collect(Collectors.toSet());
}
}
// Update networkLink with "latest" (either created or updated)
// NetworkState.documentSelfLink
NetworkState networkState = context.networkStates.get(subnetStateWithParentVNetId.parentVNetId);
if (networkState != null) {
subnetState.networkLink = networkState.documentSelfLink;
} else {
logWarning(() -> String.format("Network state corresponding to subnet with" + " name [%s] was not found. Network Link is left empty.", subnetState.name));
}
if (StringUtils.isEmpty(subnetState.endpointLink)) {
subnetState.endpointLink = context.request.endpointLink;
}
subnetState.computeHostLink = context.parentCompute.documentSelfLink;
AdapterUtils.addToEndpointLinks(subnetState, context.request.endpointLink);
return context.subnetStates.containsKey(subnetId) ? // Update case
Operation.createPatch(this, context.subnetStates.get(subnetId)).setBody(subnetState) : // Create case.
Operation.createPost(getHost(), SubnetService.FACTORY_LINK).setBody(subnetState);
});
OperationJoin.create(operations).setCompletion((ops, failures) -> {
if (failures != null) {
// We don't want to fail the whole data collection if some of the
// operation fails.
failures.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
// Process successful operations.
ops.values().stream().filter(operation -> failures != null && !failures.containsKey(operation.getId())).filter(operation -> operation.getStatusCode() != Operation.STATUS_CODE_NOT_MODIFIED).forEach(operation -> {
SubnetState subnetState = operation.getBody(SubnetState.class);
context.subnets.get(subnetState.id).subnetState = subnetState;
});
if (context.enumNextPageLink != null) {
logFine(() -> "Fetch next page of Virtual Networks from Azure.");
handleSubStage(context, NetworkEnumStages.GET_VNETS);
return;
}
logFine(() -> "Finished updating network states");
handleSubStage(context, next);
}).sendWith(this);
}
use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method disassociateNetworkStates.
/**
* Delete local network states that no longer exist in Azure.
* <p>
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle.
*/
private void disassociateNetworkStates(NetworkEnumContext context, NetworkEnumStages next) {
Builder qBuilder = Query.Builder.create().addKindFieldClause(NetworkState.class).addRangeClause(NetworkState.FIELD_NAME_UPDATE_TIME_MICROS, NumericRange.createLessThanRange(context.enumerationStartTimeInMicros));
QueryByPages<NetworkState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), NetworkState.class, context.parentCompute.tenantLinks, null, /* endpoint */
context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
disassociateResourceStates(queryLocalStates, context, next);
}
Aggregations