2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.apihandlerinfra.tenantisolation.process;
23 import java.util.List;
24 import java.util.Optional;
26 import javax.ws.rs.core.Response;
28 import org.apache.http.HttpStatus;
29 import org.json.JSONObject;
30 import org.onap.aai.domain.yang.OperationalEnvironment;
31 import org.onap.aai.domain.yang.RelationshipList;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.onap.aai.domain.yang.RelationshipData;
34 import org.onap.so.apihandler.common.ErrorNumbers;
35 import org.onap.so.db.request.client.RequestsDbClient;
36 import org.onap.so.apihandlerinfra.exceptions.ApiException;
37 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
38 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
39 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
40 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
41 import org.onap.so.apihandlerinfra.tenantisolation.helpers.ActivateVnfDBHelper;
42 import org.onap.so.apihandlerinfra.tenantisolation.helpers.SDCClientHelper;
43 import org.onap.so.apihandlerinfra.tenantisolationbeans.ServiceModelList;
44 import org.onap.so.client.aai.AAIObjectType;
45 import org.onap.so.client.aai.entities.AAIResultWrapper;
46 import org.onap.so.client.aai.entities.Relationships;
47 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
48 import org.onap.so.db.request.beans.OperationalEnvDistributionStatus;
49 import org.onap.so.db.request.beans.OperationalEnvServiceModelStatus;
50 import org.onap.so.logger.MessageEnum;
51 import org.onap.so.logger.MsoLogger;
52 import org.onap.so.requestsdb.RequestsDBHelper;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.beans.factory.annotation.Value;
57 import org.springframework.stereotype.Component;
61 public class ActivateVnfOperationalEnvironment {
63 private static final Logger logger = LoggerFactory.getLogger(ActivateVnfOperationalEnvironment.class);
64 private static final int DEFAULT_ACTIVATE_RETRY_COUNT = 3;
65 private static final String DISTRIBUTION_STATUS_SENT = "SENT";
66 private static final String OPER_ENVIRONMENT_ID_KEY = "operational-environment-id";
69 private ActivateVnfDBHelper dbHelper;
71 private AAIClientHelper aaiHelper;
73 private RequestsDBHelper requestDb;
75 private SDCClientHelper sdcClientHelper;
77 @Value("${mso.tenant.isolation.retry.count}")
78 private String sdcRetryCount;
81 RequestsDbClient client;
84 * The Point-Of-Entry from APIH with VID request to send activate request
85 * @param requestId - String
86 * @param request - CloudOrchestrationRequest object
87 * @return void - nothing
89 public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException{
90 String vnfOperationalEnvironmentId = request.getOperationalEnvironmentId();
92 String vidWorkloadContext = request.getRequestDetails().getRequestParameters().getWorkloadContext();
93 List<ServiceModelList> serviceModelVersionIdList = request.getRequestDetails().getRequestParameters().getManifest().getServiceModelList();
95 String ecompOperationalEnvironmentId = null;
96 AAIResultWrapper wrapper = getAAIOperationalEnvironment(vnfOperationalEnvironmentId);
97 Optional<Relationships> optRelationships = wrapper.getRelationships();
98 if (optRelationships.isPresent()) {
99 Relationships relationships = optRelationships.get();
100 List<AAIResourceUri> operationalEnvironments = relationships.getRelatedAAIUris(AAIObjectType.OPERATIONAL_ENVIRONMENT);
101 if (!operationalEnvironments.isEmpty()) {
102 ecompOperationalEnvironmentId = operationalEnvironments.get(0).getURIKeys().get(OPER_ENVIRONMENT_ID_KEY);
105 logger.debug(" vnfOperationalEnvironmentId : {}", vnfOperationalEnvironmentId);
106 logger.debug(" ecompOperationalEnvironmentId : {}", ecompOperationalEnvironmentId);
108 OperationalEnvironment operationalEnv = wrapper.asBean(OperationalEnvironment.class).get();
109 String workloadContext = operationalEnv.getWorkloadContext();
110 logger.debug(" aai workloadContext: {}", workloadContext);
111 if (!vidWorkloadContext.equals(workloadContext)) {
112 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
113 throw new ValidateException.Builder(" The vid workloadContext did not match from aai record. " + " vid workloadContext:" + vidWorkloadContext + " aai workloadContext:" + workloadContext,
114 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
116 if (ecompOperationalEnvironmentId==null) {
117 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
118 throw new ValidateException.Builder(" The ECOMP OE was not in aai record; the value of relationship.relationship-data key: " + OPER_ENVIRONMENT_ID_KEY,
119 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
122 processActivateSDCRequest(requestId, ecompOperationalEnvironmentId, serviceModelVersionIdList, workloadContext);
128 * The Method to send the Activation Requests to SDC
129 * @param requestId - String
130 * @param operationalEnvironmentId - String
131 * @param serviceModelVersionIdList - List<ServiceModelList> list
132 * @param workloadContext - String
133 * @return jsonResponse - JSONObject object
135 public void processActivateSDCRequest(String requestId, String operationalEnvironmentId,
136 List<ServiceModelList> serviceModelVersionIdList,
137 String workloadContext) throws ApiException {
139 JSONObject jsonResponse = null;
142 retryCount = Integer.parseInt(sdcRetryCount);
143 } catch (NumberFormatException e) {
144 retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
147 // loop through the serviceModelVersionId, and send request SDC
148 for(ServiceModelList serviceModelList : serviceModelVersionIdList){
149 String serviceModelVersionId = serviceModelList.getServiceModelVersionId();
150 String recoveryAction = serviceModelList.getRecoveryAction().toString().toUpperCase();
152 // should insert 1 row
153 OperationalEnvServiceModelStatus serviceModelStatus =
154 dbHelper.insertRecordToOperationalEnvServiceModelStatus(requestId,
155 operationalEnvironmentId,
156 serviceModelVersionId,
157 DISTRIBUTION_STATUS_SENT,
161 client.save(serviceModelStatus);
163 String distributionId = "";
165 jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
167 String statusCode = jsonResponse.get("statusCode").toString();
168 if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
169 distributionId = jsonResponse.get("distributionId").toString();
170 // should insert 1 row
171 OperationalEnvDistributionStatus distStatus =
172 dbHelper.insertRecordToOperationalEnvDistributionStatus(distributionId,
173 operationalEnvironmentId,
174 serviceModelVersionId,
176 DISTRIBUTION_STATUS_SENT,
178 client.save(distStatus);
181 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
182 String dbErrorMessage = " Failure calling SDC: statusCode: " + statusCode +
183 "; messageId: " + jsonResponse.get("messageId") +
184 "; message: " + jsonResponse.get("message");
186 requestDb.updateInfraFailureCompletion(dbErrorMessage, requestId, operationalEnvironmentId);
187 throw new ValidateException.Builder(dbErrorMessage,
188 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
195 * Get AAIOperationalEnvironment object
196 * @param operationalEnvironmentId - String
197 * @return operationalEnv - AAIOperationalEnvironment object
199 public AAIResultWrapper getAAIOperationalEnvironment(String operationalEnvironmentId) {
200 return aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);