use of org.onap.so.client.RestClient in project so by onap.
the class AAISingleTransactionClient method execute.
/*
* (non-Javadoc)
*
* @see org.onap.aaiclient.client.aai.GraphInventoryTransactionClient#execute()
*/
@Override
public void execute() throws BulkProcessFailed {
try {
if (!this.request.getOperations().isEmpty()) {
RestClient client = aaiClient.createClient(AAIUriFactory.createResourceUri(AAIObjectType.SINGLE_TRANSACTION));
SingleTransactionResponse response = client.post(this.request, SingleTransactionResponse.class);
if (response != null) {
final Optional<String> errorMessage = this.locateErrorMessages(response);
if (errorMessage.isPresent()) {
throw new BulkProcessFailed("One or more transactions failed in A&AI. Check logs for payloads.\nMessages:\n" + errorMessage.get());
}
} else {
throw new BulkProcessFailed("Transactions acccepted by A&AI, but there was no response. Unsure of result.");
}
}
} finally {
this.request.getOperations().clear();
this.actionCount = 0;
}
}
use of org.onap.so.client.RestClient in project so by onap.
the class MsoMulticloudUtils method queryStack.
/**
* Query for a single stack (by ID) in a tenant. This call will always return a StackInfo object. If the stack does
* not exist, an "empty" StackInfo will be returned - containing only the stack name and a status of NOTFOUND.
*
* @param tenantId The Openstack ID of the tenant in which to query
* @param cloudSiteId The cloud identifier (may be a region) in which to query
* @param cloudOwner cloud owner of the cloud site in which to query
* @param stackId The ID of the stack to query
* @return A StackInfo object
* @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
*/
@Override
public StackInfo queryStack(String cloudSiteId, String cloudOwner, String tenantId, String instanceId) throws MsoException {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Query multicloud HEAT stack: %s in tenant %s", instanceId, tenantId));
}
String stackName = null;
String stackId = null;
boolean byName = false;
int offset = instanceId.indexOf('/');
if (offset > 0 && offset < (instanceId.length() - 1)) {
stackName = instanceId.substring(0, offset);
stackId = instanceId.substring(offset + 1);
} else {
stackName = instanceId;
stackId = instanceId;
byName = true;
}
StackInfo returnInfo = new StackInfo();
returnInfo.setName(stackName);
String multicloudEndpoint = getMulticloudEndpoint(cloudSiteId, cloudOwner, stackId, byName);
RestClient multicloudClient = getMulticloudClient(multicloudEndpoint, tenantId);
if (multicloudClient != null) {
Response response = multicloudClient.get();
if (logger.isDebugEnabled()) {
logger.debug(String.format("Multicloud GET Response: %s", response.toString()));
}
MulticloudQueryResponse responseBody = null;
if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) {
returnInfo.setStatus(HeatStatus.NOTFOUND);
returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
} else if (response.getStatus() == Response.Status.OK.getStatusCode() && response.hasEntity()) {
responseBody = getQueryBody((java.io.InputStream) response.getEntity());
if (responseBody != null) {
if (logger.isDebugEnabled()) {
logger.debug("Multicloud Create Response Body: {}", responseBody);
}
Stack workloadStack = getWorkloadStack(responseBody.getWorkloadStatusReason());
if (workloadStack != null && !responseBody.getWorkloadStatus().equals("GET_FAILED") && !responseBody.getWorkloadStatus().contains("UPDATE")) {
returnInfo = new StackInfoMapper(workloadStack).map();
} else {
returnInfo.setCanonicalName(stackName + "/" + responseBody.getWorkloadId());
returnInfo.setStatus(getHeatStatus(responseBody.getWorkloadStatus()));
returnInfo.setStatusMessage(responseBody.getWorkloadStatus());
}
} else {
returnInfo.setName(stackName);
if (!byName)
returnInfo.setCanonicalName(instanceId);
returnInfo.setStatus(HeatStatus.FAILED);
returnInfo.setStatusMessage(MULTICLOUD_QUERY_BODY_NULL);
}
} else {
returnInfo.setName(stackName);
if (!byName)
returnInfo.setCanonicalName(instanceId);
returnInfo.setStatus(HeatStatus.FAILED);
returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
}
}
return returnInfo;
}
use of org.onap.so.client.RestClient in project so by onap.
the class MsoMulticloudUtils method deleteStack.
public StackInfo deleteStack(String cloudSiteId, String cloudOwner, String tenantId, String instanceId) throws MsoException {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Delete multicloud HEAT stack: %s in tenant %s", instanceId, tenantId));
}
String stackName = null;
String stackId = null;
int offset = instanceId.indexOf('/');
if (offset > 0 && offset < (instanceId.length() - 1)) {
stackName = instanceId.substring(0, offset);
stackId = instanceId.substring(offset + 1);
} else {
stackName = instanceId;
stackId = instanceId;
}
StackInfo returnInfo = new StackInfo();
returnInfo.setName(stackName);
Response response = null;
String multicloudEndpoint = getMulticloudEndpoint(cloudSiteId, cloudOwner, stackId, false);
RestClient multicloudClient = getMulticloudClient(multicloudEndpoint, tenantId);
if (multicloudClient != null) {
response = multicloudClient.delete();
if (logger.isDebugEnabled()) {
logger.debug(String.format("Multicloud Delete response is: %s", response.getEntity().toString()));
}
if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) {
returnInfo.setStatus(HeatStatus.NOTFOUND);
returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
} else if (response.getStatus() == Response.Status.NO_CONTENT.getStatusCode()) {
return getStackStatus(cloudSiteId, cloudOwner, tenantId, instanceId);
} else {
returnInfo.setStatus(HeatStatus.FAILED);
returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
}
}
returnInfo.setStatus(mapResponseToHeatStatus(response));
return returnInfo;
}
use of org.onap.so.client.RestClient in project so by onap.
the class AAITransactionalClient method execute.
/*
* (non-Javadoc)
*
* @see org.onap.aaiclient.client.aai.GraphInventoryTransactionalClient#execute()
*/
@Override
public void execute() throws BulkProcessFailed {
try {
if (!this.transactions.getTransactions().isEmpty()) {
RestClient client = aaiClient.createClient(AAIUriFactory.createResourceUri(AAIObjectType.BULK_PROCESS));
Response response = client.put(this.transactions);
if (response.hasEntity()) {
final Optional<String> errorMessage = this.locateErrorMessages(response.readEntity(String.class));
if (errorMessage.isPresent()) {
throw new BulkProcessFailed("One or more transactions failed in A&AI. Check logs for payloads.\nMessages:\n" + errorMessage.get());
}
} else {
throw new BulkProcessFailed("Transactions acccepted by A&AI, but there was no response. Unsure of result.");
}
}
} finally {
this.transactions.getTransactions().clear();
this.currentTransaction = null;
this.actionCount = 0;
}
}
use of org.onap.so.client.RestClient in project so by onap.
the class MsoMulticloudUtils method createStack.
/**
****************************************************************************
*
* Methods (and associated utilities) to implement the VduPlugin interface
*
******************************************************************************
*/
/**
* Create a new Stack in the specified cloud location and tenant. The Heat template and parameter map are passed in
* as arguments, along with the cloud access credentials. It is expected that parameters have been validated and
* contain at minimum the required parameters for the given template with no extra (undefined) parameters..
*
* The Stack name supplied by the caller must be unique in the scope of this tenant. However, it should also be
* globally unique, as it will be the identifier for the resource going forward in Inventory. This latter is managed
* by the higher levels invoking this function.
*
* The caller may choose to let this function poll Openstack for completion of the stack creation, or may handle
* polling itself via separate calls to query the status. In either case, a StackInfo object will be returned
* containing the current status. When polling is enabled, a status of CREATED is expected. When not polling, a
* status of BUILDING is expected.
*
* An error will be thrown if the requested Stack already exists in the specified Tenant and Cloud.
*
* For 1510 - add "environment", "files" (nested templates), and "heatFiles" (get_files) as parameters for
* createStack. If environment is non-null, it will be added to the stack. The nested templates and get_file entries
* both end up being added to the "files" on the stack. We must combine them before we add them to the stack if
* they're both non-null.
*
* @param cloudSiteId The cloud (may be a region) in which to create the stack
* @param cloudOwner the cloud owner of the cloud site in which to create the stack
* @param tenantId The Openstack ID of the tenant in which to create the Stack
* @param stackName The name of the stack to create
* @param heatTemplate The Heat template
* @param stackInputs A map of key/value inputs
* @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
* @param environment An optional yaml-format string to specify environmental parameters
* @param files a Map<String, Object> that lists the child template IDs (file is the string, object is an int of
* Template id)
* @param heatFiles a Map<String, Object> that lists the get_file entries (fileName, fileBody)
* @param backout Do not delete stack on create Failure - defaulted to True
* @return A StackInfo object
* @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
*/
@SuppressWarnings("unchecked")
@Override
public StackInfo createStack(String cloudSiteId, String cloudOwner, String tenantId, String stackName, VduModelInfo vduModel, String heatTemplate, Map<String, ?> stackInputs, boolean pollForCompletion, int timeoutMinutes, String environment, Map<String, Object> files, Map<String, Object> heatFiles, boolean backout, boolean failIfExists) throws MsoException {
logger.trace("Started MsoMulticloudUtils.createStack");
// Get the directives, if present.
String oofDirectives = "{}";
String sdncDirectives = "{}";
String userDirectives = "{}";
String genericVnfId = "";
String vfModuleId = "";
String templateType = "";
for (String key : MULTICLOUD_INPUTS) {
if (!stackInputs.isEmpty() && stackInputs.containsKey(key)) {
if (OOF_DIRECTIVES.equals(key)) {
oofDirectives = (String) stackInputs.get(key);
}
if (SDNC_DIRECTIVES.equals(key)) {
sdncDirectives = (String) stackInputs.get(key);
}
if (USER_DIRECTIVES.equals(key)) {
userDirectives = (String) stackInputs.get(key);
}
if (TEMPLATE_TYPE.equals(key)) {
templateType = (String) stackInputs.get(key);
}
if (logger.isDebugEnabled()) {
logger.debug(String.format("Found %s: %s", key, stackInputs.get(key)));
}
stackInputs.remove(key);
}
}
if (!stackInputs.isEmpty() && stackInputs.containsKey(VF_MODULE_ID)) {
vfModuleId = (String) stackInputs.get(VF_MODULE_ID);
}
if (!stackInputs.isEmpty() && stackInputs.containsKey(VNF_ID)) {
genericVnfId = (String) stackInputs.get(VNF_ID);
}
// create the multicloud payload
CreateStackParam stack = createStackParam(stackName, heatTemplate, stackInputs, timeoutMinutes, environment, files, heatFiles);
MulticloudRequest multicloudRequest = new MulticloudRequest();
multicloudRequest.setGenericVnfId(genericVnfId);
multicloudRequest.setVfModuleId(vfModuleId);
multicloudRequest.setVfModuleModelInvariantId(vduModel.getModelInvariantUUID());
multicloudRequest.setVfModuleModelVersionId(vduModel.getModelUUID());
multicloudRequest.setVfModuleModelCustomizationId(vduModel.getModelCustomizationUUID());
multicloudRequest.setTemplateType(templateType);
multicloudRequest.setTemplateData(stack);
multicloudRequest.setOofDirectives(getDirectiveNode(oofDirectives));
multicloudRequest.setSdncDirectives(getDirectiveNode(sdncDirectives));
multicloudRequest.setUserDirectives(getDirectiveNode(userDirectives));
if (logger.isDebugEnabled()) {
logger.debug(String.format("Multicloud Request is: %s", multicloudRequest.toString()));
}
String multicloudEndpoint = getMulticloudEndpoint(cloudSiteId, cloudOwner, null, false);
RestClient multicloudClient = getMulticloudClient(multicloudEndpoint, tenantId);
if (multicloudClient == null) {
MsoOpenstackException me = new MsoOpenstackException(0, "", "Multicloud client could not be initialized");
me.addContext(CREATE_STACK);
throw me;
}
Response response = multicloudClient.post(multicloudRequest);
MulticloudCreateResponse multicloudResponseBody = null;
if (response.hasEntity()) {
multicloudResponseBody = getCreateBody((java.io.InputStream) response.getEntity());
}
if (response.getStatus() == Response.Status.CREATED.getStatusCode() && multicloudResponseBody != null) {
String canonicalName = stackName + "/";
if (multicloudResponseBody != null) {
canonicalName = canonicalName + multicloudResponseBody.getWorkloadId();
}
if (logger.isDebugEnabled()) {
logger.debug("Multicloud Create Response Body: {}", multicloudResponseBody);
}
StackInfo stackStatus = getStackStatus(cloudSiteId, cloudOwner, tenantId, canonicalName, pollForCompletion, timeoutMinutes, backout);
if (HeatStatus.CREATED.equals(stackStatus.getStatus())) {
multicloudAaiUpdate(cloudSiteId, cloudOwner, tenantId, genericVnfId, vfModuleId, canonicalName, pollForCompletion, timeoutMinutes);
}
return stackStatus;
}
StringBuilder stackErrorStatusReason = new StringBuilder(response.getStatusInfo().getReasonPhrase());
if (null != multicloudResponseBody) {
stackErrorStatusReason.append(multicloudResponseBody.toString());
}
MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
me.addContext(CREATE_STACK);
throw me;
}
Aggregations