use of com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler in project photon-model by vmware.
the class AWSMissingResourcesEnumerationService method createLinkedComputeStates.
/**
* creates new linked account endpoint states of a primary account
* @param context
*/
private void createLinkedComputeStates(AwsMissingResourcesEnumContext context) {
AtomicInteger completionCounter = new AtomicInteger(context.request.missingLinkedAccountIds.size());
AtomicBoolean isSuccessful = new AtomicBoolean(true);
context.request.missingLinkedAccountIds.forEach(linkedAccountId -> {
ComputeDescription computeDescription = populateComputeDescription(context, linkedAccountId);
ComputeState computeState = populateComputeState(context, linkedAccountId);
computeState.descriptionLink = computeDescription.documentSelfLink;
Operation csOp = Operation.createPost(this, ComputeService.FACTORY_LINK).setBody(computeState);
Operation cdOp = Operation.createPost(this, ComputeDescriptionService.FACTORY_LINK).setBody(computeDescription);
JoinedCompletionHandler completionHandler = (ops, exs) -> {
if (exs != null && !exs.isEmpty()) {
logWarning(() -> String.format("Error creating missing resources for" + " account with ID %s.", linkedAccountId));
isSuccessful.set(false);
} else {
logInfo(() -> String.format("Created missing resources for account " + "with ID %s.", linkedAccountId));
}
if (completionCounter.decrementAndGet() == 0) {
if (isSuccessful.get()) {
context.requestOp.complete();
} else {
context.requestOp.fail(new Exception("Failed to create missing resources " + "for atleast one linked account."));
}
}
};
Operation.createGet(this, computeDescription.documentSelfLink).setReferer(this.getUri()).setCompletion((o, e) -> {
if (e != null && (e instanceof ServiceNotFoundException || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND)) {
OperationSequence.create(cdOp).next(csOp).setCompletion(completionHandler).sendWith(this);
} else {
OperationSequence.create(csOp).setCompletion(completionHandler).sendWith(this);
}
}).sendWith(this);
});
}
use of com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method createNetworkStateOperations.
/**
* Create the network state operations for all the VPCs that need to be created or updated in
* the system.
*/
private void createNetworkStateOperations(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
if (context.vpcs.isEmpty()) {
logFine(() -> "No new VPCs have been discovered.");
handleNetworkStateChanges(context, next);
return;
}
final List<Operation> networkOperations = new ArrayList<>();
for (String remoteVPCId : context.vpcs.keySet()) {
NetworkState networkState = context.vpcs.get(remoteVPCId);
final Operation networkStateOp;
if (context.localNetworkStateMap.containsKey(remoteVPCId)) {
// If the local network state already exists for the VPC -> Update it.
networkState.documentSelfLink = context.localNetworkStateMap.get(remoteVPCId).documentSelfLink;
// don't overwrite resourcePoolLink
networkState.resourcePoolLink = null;
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());
}
networkStateOp = createPatchOperation(this, networkState, networkState.documentSelfLink);
} else {
Vpc awsVpc = context.awsVpcs.get(remoteVPCId);
// Add both external and internal tags.
setResourceTags(networkState, awsVpc.getTags());
setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
networkStateOp = createPostOperation(this, networkState, NetworkService.FACTORY_LINK);
}
networkOperations.add(networkStateOp);
}
JoinedCompletionHandler joinCompletion = (ops, excs) -> {
if (excs != null) {
Entry<Long, Throwable> excEntry = excs.entrySet().iterator().next();
Throwable exc = excEntry.getValue();
Operation op = ops.get(excEntry.getKey());
logSevere(() -> String.format("Error %s-ing a Network state: %s", op.getAction(), Utils.toString(excs)));
finishWithFailure(context, exc);
return;
}
logFine(() -> "Created/updated all network states.");
ops.values().stream().filter(op -> op.getStatusCode() != Operation.STATUS_CODE_NOT_MODIFIED).forEach(op -> {
NetworkState networkState = op.getBody(NetworkState.class);
context.vpcs.put(networkState.id, networkState);
});
handleNetworkStateChanges(context, next);
};
OperationJoin.create(networkOperations).setCompletion(joinCompletion).sendWith(this);
}
use of com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method createSubnetStateOperations.
/**
* Create the subnet state operations for all the Subnets that need to be created or updated in
* the system.
*/
private void createSubnetStateOperations(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
if (context.subnets.isEmpty()) {
logFine(() -> "No new subnets found.");
handleNetworkStateChanges(context, next);
return;
}
final List<Operation> subnetOperations = new ArrayList<>();
for (String remoteSubnetId : context.subnets.keySet()) {
SubnetStateWithParentVpcId subnetStateWithParentVpcId = context.subnets.get(remoteSubnetId);
SubnetState subnetState = subnetStateWithParentVpcId.subnetState;
// Update networkLink with "latest" (either created or updated)
// NetworkState.documentSelfLink
subnetState.networkLink = context.vpcs.get(subnetStateWithParentVpcId.parentVpcId).documentSelfLink;
final Operation subnetStateOp;
if (context.localSubnetStateMap.containsKey(remoteSubnetId)) {
// If the local subnet state already exists for the Subnet -> Update it.
subnetState.documentSelfLink = context.localSubnetStateMap.get(remoteSubnetId).documentSelfLink;
// 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());
}
subnetStateOp = createPatchOperation(this, subnetState, subnetState.documentSelfLink);
} else {
// add tag links
Subnet awsSubnet = context.awsSubnets.get(remoteSubnetId);
setResourceTags(subnetState, awsSubnet.getTags());
setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
subnetStateOp = createPostOperation(this, subnetState, SubnetService.FACTORY_LINK);
}
subnetOperations.add(subnetStateOp);
}
JoinedCompletionHandler joinCompletion = (ops, excs) -> {
if (excs != null) {
Entry<Long, Throwable> excEntry = excs.entrySet().iterator().next();
Throwable exc = excEntry.getValue();
Operation op = ops.get(excEntry.getKey());
logSevere(() -> String.format("Error %s-ing a Subnet state: %s", op.getAction(), Utils.toString(excs)));
finishWithFailure(context, exc);
return;
}
logFine(() -> "Successfully created/updated all subnet states.");
ops.values().stream().filter(op -> op.getStatusCode() != Operation.STATUS_CODE_NOT_MODIFIED).forEach(op -> {
SubnetState subnetState = op.getBody(SubnetState.class);
context.subnets.get(subnetState.id).subnetState = subnetState;
});
handleNetworkStateChanges(context, next);
};
OperationJoin.create(subnetOperations).setCompletion(joinCompletion).sendWith(this);
}
Aggregations