2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.apihandlerinfra.tenantisolation.process;
26 import java.util.List;
27 import java.util.Optional;
28 import javax.ws.rs.core.Response;
29 import org.apache.http.HttpStatus;
30 import org.json.JSONObject;
31 import org.onap.aai.domain.yang.OperationalEnvironment;
32 import org.onap.so.apihandler.common.ErrorNumbers;
33 import org.onap.so.db.request.client.RequestsDbClient;
34 import org.onap.so.apihandlerinfra.exceptions.ApiException;
35 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
36 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
37 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
38 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
39 import org.onap.so.apihandlerinfra.tenantisolation.helpers.ActivateVnfDBHelper;
40 import org.onap.so.apihandlerinfra.tenantisolation.helpers.SDCClientHelper;
41 import org.onap.so.apihandlerinfra.tenantisolationbeans.Distribution;
42 import org.onap.so.apihandlerinfra.tenantisolationbeans.DistributionStatus;
43 import org.onap.so.client.aai.entities.AAIResultWrapper;
44 import org.onap.so.db.request.beans.OperationalEnvDistributionStatus;
45 import org.onap.so.db.request.beans.OperationalEnvServiceModelStatus;
46 import org.onap.so.logger.ErrorCode;
47 import org.onap.so.logger.MessageEnum;
48 import org.onap.so.requestsdb.RequestsDBHelper;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.stereotype.Component;
55 public class ActivateVnfStatusOperationalEnvironment {
57 private static Logger logger = LoggerFactory.getLogger(ActivateVnfStatusOperationalEnvironment.class);
58 private String origRequestId = "";
59 private String errorMessage = "";
60 private OperationalEnvDistributionStatus queryDistributionDbResponse = null;
61 private OperationalEnvServiceModelStatus queryServiceModelResponse = null;
62 private boolean isOverallSuccess = false;
64 private final int RETRY_COUNT_ZERO = 0;
65 private final String ERROR_REASON_ABORTED = "ABORTED";
66 private final String RECOVERY_ACTION_RETRY = "RETRY";
67 private final String RECOVERY_ACTION_ABORT = "ABORT";
68 private final String RECOVERY_ACTION_SKIP = "SKIP";
69 private final String DISTRIBUTION_STATUS_OK = DistributionStatus.DISTRIBUTION_COMPLETE_OK.toString();
70 private final String DISTRIBUTION_STATUS_ERROR = DistributionStatus.DISTRIBUTION_COMPLETE_ERROR.toString();
71 private final String DISTRIBUTION_STATUS_SENT = "SENT";
73 private final String MESSAGE_UNDEFINED_ID = "Undefined Error Message!";
76 private ActivateVnfDBHelper dbHelper;
78 private RequestsDBHelper requestDb;
80 private SDCClientHelper sdcClientHelper;
82 private RequestsDbClient client;
84 private AAIClientHelper aaiHelper;
87 * The Point-Of-Entry from APIH with activate status from SDC
89 * @param requestId - String
90 * @param request - CloudOrchestrationRequest - object
91 * @return void - nothing
93 public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException {
97 String operationalEnvironmentId = "";
99 String sdcDistributionId = request.getDistributionId();
100 Distribution sdcStatus = request.getDistribution();
102 // Distribution, Query for operationalEnvironmentId, serviceModelVersionId, origRequestId
103 this.queryDistributionDbResponse = client.getDistributionStatusById(sdcDistributionId);
104 operationalEnvironmentId = this.queryDistributionDbResponse.getOperationalEnvId();
105 this.origRequestId = this.queryDistributionDbResponse.getRequestId();
107 // ServiceModel, Query for recoveryAction, retryCountString
108 this.queryServiceModelResponse =
109 client.findOneByOperationalEnvIdAndServiceModelVersionIdAndRequestId(operationalEnvironmentId,
110 queryDistributionDbResponse.getServiceModelVersionId(), this.origRequestId);
112 processActivateSDCStatus(sdcDistributionId, sdcStatus, this.queryDistributionDbResponse,
113 this.queryServiceModelResponse);
115 // After EVERY status processed, need to query the status of all service modelId
116 // to determine the OVERALL status if "COMPLETE" or "FAILURE":
117 checkOrUpdateOverallStatus(operationalEnvironmentId, this.origRequestId);
119 // Update AAI to ACTIVE if Overall success
120 if (isOverallSuccess) {
121 String vnfOperationalEnvironmentId = this.queryServiceModelResponse.getVnfOperationalEnvId();
122 OperationalEnvironment aaiOpEnv = getAAIOperationalEnvironment(vnfOperationalEnvironmentId);
123 if (aaiOpEnv != null) {
124 aaiOpEnv.setOperationalEnvironmentStatus("ACTIVE");
125 aaiHelper.updateAaiOperationalEnvironment(vnfOperationalEnvironmentId, aaiOpEnv);
127 requestDb.updateInfraFailureCompletion("Unable to update ACTIVATE status in AAI. ",
128 this.origRequestId, this.queryServiceModelResponse.getVnfOperationalEnvId());
132 } catch (Exception e) {
133 requestDb.updateInfraFailureCompletion(e.getMessage(), this.origRequestId,
134 this.queryServiceModelResponse.getVnfOperationalEnvId());
140 * The Method to process the Activation Status from SDC
142 * @param sdcDistributionId - string
143 * @param sdcStatus - Distribution object
144 * @param queryDistributionDbResponse - OperationalEnvDistributionStatus object
145 * @param queryServiceModelResponse - OperationalEnvServiceModelStatus object
146 * @return void - nothing
148 public void processActivateSDCStatus(String sdcDistributionId, Distribution sdcStatus,
149 OperationalEnvDistributionStatus queryDistributionDbResponse,
150 OperationalEnvServiceModelStatus queryServiceModelResponse) throws ApiException {
152 String sdcStatusValue = sdcStatus.getStatus().toString();
153 String recoveryAction = queryServiceModelResponse.getRecoveryAction();
154 int retryCount = queryServiceModelResponse.getRetryCount();
156 // Validate/process status
157 if (sdcStatus.getStatus().toString().equals(DISTRIBUTION_STATUS_OK)) {
158 // should update 1 row, update status to "DISTRIBUTION_COMPLETE_OK"
159 OperationalEnvDistributionStatus updateDistStatusOk = dbHelper
160 .updateStatusInOperationalEnvDistributionStatus(queryDistributionDbResponse, sdcStatusValue, "");
161 client.save(updateDistStatusOk);
162 // should update 1 row, update status and retryCount = 0 (ie, serviceModelVersionId is DONE!)
163 OperationalEnvServiceModelStatus updateRetryCountZeroAndStatusOk =
164 dbHelper.updateRetryCountAndStatusInOperationalEnvServiceModelStatus(queryServiceModelResponse,
165 sdcStatusValue, RETRY_COUNT_ZERO);
166 client.save(updateRetryCountZeroAndStatusOk);
169 // "DISTRIBUTION_COMPLETE_ERROR", Check if recoveryAction is "RETRY"
170 if (recoveryAction.equals(RECOVERY_ACTION_RETRY) && retryCount > RETRY_COUNT_ZERO) {
172 // RESEND / RETRY serviceModelVersionId to SDC
174 JSONObject jsonResponse =
175 callSDClientForRetry(queryDistributionDbResponse, queryServiceModelResponse, sdcStatus);
177 } else { // either RETRY & Count = 0, or 'ABORT', or 'SKIP'
179 if (recoveryAction.equals(RECOVERY_ACTION_SKIP) || recoveryAction.equals(RECOVERY_ACTION_ABORT)) {
180 String modifiedStatus = "";
181 String errorReason = "";
182 if (recoveryAction.equals(RECOVERY_ACTION_SKIP)) { // considered SUCCESS
183 modifiedStatus = DISTRIBUTION_STATUS_OK;
185 if (recoveryAction.equals(RECOVERY_ACTION_ABORT)) {
186 modifiedStatus = DISTRIBUTION_STATUS_ERROR; // ABORT, error
187 errorReason = ERROR_REASON_ABORTED;
191 sdcStatusValue = modifiedStatus;
192 OperationalEnvServiceModelStatus updateRetryCountZeroAndStatus =
193 dbHelper.updateRetryCountAndStatusInOperationalEnvServiceModelStatus(
194 queryServiceModelResponse, modifiedStatus, RETRY_COUNT_ZERO);
195 client.save(updateRetryCountZeroAndStatus);
196 OperationalEnvDistributionStatus updateDistStatus =
197 dbHelper.updateStatusInOperationalEnvDistributionStatus(queryDistributionDbResponse,
198 modifiedStatus, errorReason);
199 client.save(updateDistStatus);
201 // RETRY & Count = 0 (do nothing!)
208 * The Method to call SDC for recoveryActioin RETRY
210 * @param queryDistributionDbResponse - OperationalEnvDistributionStatus object
211 * @param queryServiceModelResponse - OperationalEnvServiceModelStatus object
212 * @param sdcStatus - Distribution object
213 * @return JSONObject object
215 public JSONObject callSDClientForRetry(OperationalEnvDistributionStatus queryDistributionDbResponse,
216 OperationalEnvServiceModelStatus queryServiceModelResponse, Distribution sdcStatus) throws ApiException {
218 JSONObject jsonResponse = null;
220 String operEnvironmentId = queryDistributionDbResponse.getOperationalEnvId();
221 String serviceModelVersionId = queryDistributionDbResponse.getServiceModelVersionId();
222 String originalRequestId = queryServiceModelResponse.getRequestId();
223 int retryCount = queryServiceModelResponse.getRetryCount();
224 String workloadContext = queryServiceModelResponse.getWorkloadContext();
227 jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId, operEnvironmentId,
229 String statusCode = jsonResponse.get("statusCode").toString();
230 if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
231 String newDistributionId = jsonResponse.get("distributionId").toString();
232 // should insert 1 row, NEW distributionId for replacement of the serviceModelServiceId record
233 OperationalEnvDistributionStatus insertNewDistributionId =
234 dbHelper.insertRecordToOperationalEnvDistributionStatus(newDistributionId, operEnvironmentId,
235 serviceModelVersionId, originalRequestId, DISTRIBUTION_STATUS_SENT, "");
236 client.save(insertNewDistributionId);
238 // update retryCount (less 1) for the serviceModelServiceId
239 retryCount = retryCount - 1;
240 // should update 1 row, original insert
241 OperationalEnvServiceModelStatus updateRetryCountAndStatus =
242 dbHelper.updateRetryCountAndStatusInOperationalEnvServiceModelStatus(queryServiceModelResponse,
243 DISTRIBUTION_STATUS_SENT, retryCount);
244 client.save(updateRetryCountAndStatus);
246 // should update 1 row, OLD distributionId set to status error (ie, old distributionId is DONE!).
247 OperationalEnvDistributionStatus updateStatus = dbHelper.updateStatusInOperationalEnvDistributionStatus(
248 queryDistributionDbResponse, DISTRIBUTION_STATUS_ERROR, sdcStatus.getErrorReason());
249 client.save(updateStatus);
251 String dbErrorMessage = "Failure calling SDC: statusCode: " + statusCode + "; messageId: "
252 + jsonResponse.get("messageId") + "; message: " + jsonResponse.get("message");
253 ErrorLoggerInfo errorLoggerInfo =
254 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
256 ValidateException validateException =
257 new ValidateException.Builder(dbErrorMessage, HttpStatus.SC_BAD_REQUEST,
258 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
259 requestDb.updateInfraFailureCompletion(dbErrorMessage, this.origRequestId, operEnvironmentId);
260 throw validateException;
269 * The Method to check the overall status of the Activation for an operationalEnvironmentId
271 * @param operationalEnvironmentId - string
272 * @param origRequestId - string
273 * @return void - nothing
275 public void checkOrUpdateOverallStatus(String operationalEnvironmentId, String origRequestId) throws ApiException {
277 List<OperationalEnvServiceModelStatus> queryServiceModelResponseList =
278 client.getAllByOperationalEnvIdAndRequestId(operationalEnvironmentId, origRequestId);
280 String status = "Waiting";
282 // loop through the statuses of the service model
283 for (OperationalEnvServiceModelStatus querySrvModelResponse : queryServiceModelResponseList) {
284 status = querySrvModelResponse.getServiceModelVersionDistrStatus();
285 // all should be OK to be completed.
286 if ((status.equals(DistributionStatus.DISTRIBUTION_COMPLETE_OK.toString())
287 && (querySrvModelResponse.getRetryCount() == 0))) {
288 status = "Completed";
291 // one error with zero retry, means all are failures.
292 if ((status.equals(DistributionStatus.DISTRIBUTION_COMPLETE_ERROR.toString())
293 && (querySrvModelResponse.getRetryCount() == 0))) {
295 count = queryServiceModelResponseList.size();
300 if (status.equals("Completed") && queryServiceModelResponseList.size() == count) {
301 String messageStatus = "Overall Activation process is complete. " + status;
302 isOverallSuccess = true;
303 requestDb.updateInfraSuccessCompletion(messageStatus, origRequestId, operationalEnvironmentId);
305 if (status.equals("Failure") && queryServiceModelResponseList.size() == count) {
306 this.errorMessage = "Overall Activation process is a Failure. " + status;
307 ErrorLoggerInfo errorLoggerInfo =
308 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
310 ValidateException validateException =
311 new ValidateException.Builder(this.errorMessage, HttpStatus.SC_BAD_REQUEST,
312 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
313 requestDb.updateInfraFailureCompletion(this.errorMessage, origRequestId, operationalEnvironmentId);
314 throw validateException;
321 * Get OperationalEnvironment object
323 * @param operationalEnvironmentId - String
324 * @return operationalEnv - OperationalEnvironment object
326 private OperationalEnvironment getAAIOperationalEnvironment(String operationalEnvironmentId) {
327 AAIResultWrapper aaiResult = aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
328 Optional<OperationalEnvironment> operationalEnvironmentOpt = aaiResult.asBean(OperationalEnvironment.class);
329 return operationalEnvironmentOpt.isPresent() ? operationalEnvironmentOpt.get() : null;