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.so.apihandler.common.ErrorNumbers;
31 import org.onap.so.apihandlerinfra.exceptions.ApiException;
32 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
33 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
34 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
35 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
36 import org.onap.so.apihandlerinfra.tenantisolation.helpers.ActivateVnfDBHelper;
37 import org.onap.so.apihandlerinfra.tenantisolation.helpers.SDCClientHelper;
38 import org.onap.so.apihandlerinfra.tenantisolationbeans.ServiceModelList;
39 import org.onap.so.client.aai.entities.AAIResultWrapper;
40 import org.onap.so.client.aai.objects.AAIOperationalEnvironment;
41 import org.onap.so.db.request.beans.OperationalEnvDistributionStatus;
42 import org.onap.so.db.request.beans.OperationalEnvServiceModelStatus;
43 import org.onap.so.db.request.data.repository.OperationalEnvDistributionStatusRepository;
44 import org.onap.so.db.request.data.repository.OperationalEnvServiceModelStatusRepository;
45 import org.onap.so.logger.MessageEnum;
46 import org.onap.so.logger.MsoLogger;
47 import org.onap.so.requestsdb.RequestsDBHelper;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.beans.factory.annotation.Value;
50 import org.springframework.stereotype.Component;
54 public class ActivateVnfOperationalEnvironment {
56 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, ActivateVnfOperationalEnvironment.class);
57 private static final int DEFAULT_ACTIVATE_RETRY_COUNT = 3;
58 private static final String DISTRIBUTION_STATUS_SENT = "SENT";
61 private ActivateVnfDBHelper dbHelper;
63 private AAIClientHelper aaiHelper;
65 private RequestsDBHelper requestDb;
67 private SDCClientHelper sdcClientHelper;
69 @Value("${mso.tenant.isolation.retry.count}")
70 private String sdcRetryCount;
73 * The Point-Of-Entry from APIH with VID request to send activate request
74 * @param requestId - String
75 * @param request - CloudOrchestrationRequest object
76 * @param distributionStatusRepository - OperationalEnvDistributionStatusRepository object
77 * @param modelStatusRepository - OperationalEnvServiceModelStatusRepository object
78 * @return void - nothing
80 public void execute(String requestId, CloudOrchestrationRequest request, OperationalEnvDistributionStatusRepository distributionStatusRepository,
81 OperationalEnvServiceModelStatusRepository modelStatusRepository) throws ApiException{
82 String operationalEnvironmentId = request.getOperationalEnvironmentId();
84 String vidWorkloadContext = request.getRequestDetails().getRequestParameters().getWorkloadContext();
85 List<ServiceModelList> serviceModelVersionIdList = request.getRequestDetails().getRequestParameters().getManifest().getServiceModelList();
88 AAIOperationalEnvironment operationalEnv = getAAIOperationalEnvironment(operationalEnvironmentId);
89 String workloadContext = operationalEnv.getWorkloadContext();
90 msoLogger.debug(" aai workloadContext: " + workloadContext);
91 if (!vidWorkloadContext.equals(workloadContext)) {
94 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
95 throw new ValidateException.Builder(" The vid workloadContext did not match from aai record. " + " vid workloadContext:" + vidWorkloadContext + " aai workloadContext:" + workloadContext,
96 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
99 processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext, distributionStatusRepository, modelStatusRepository);
105 * The Method to send the Activation Requests to SDC
106 * @param requestId - String
107 * @param operationalEnvironmentId - String
108 * @param serviceModelVersionIdList - List<ServiceModelList> list
109 * @param workloadContext - String
110 * @param distributionStatusRepository - OperationalEnvDistributionStatusRepository object
111 * @param modelStatusRepository - OperationalEnvServiceModelStatusRepository object
112 * @return jsonResponse - JSONObject object
114 public void processActivateSDCRequest(String requestId, String operationalEnvironmentId, List<ServiceModelList> serviceModelVersionIdList,
115 String workloadContext, OperationalEnvDistributionStatusRepository distributionStatusRepository,
116 OperationalEnvServiceModelStatusRepository modelStatusRepository) throws ApiException {
118 JSONObject jsonResponse = null;
121 retryCount = Integer.parseInt(sdcRetryCount);
122 } catch (NumberFormatException e) {
123 retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
126 // loop through the serviceModelVersionId, and send request SDC
127 for(ServiceModelList serviceModelList : serviceModelVersionIdList){
128 String serviceModelVersionId = serviceModelList.getServiceModelVersionId();
129 String recoveryAction = serviceModelList.getRecoveryAction().toString().toUpperCase();
131 // should insert 1 row
132 OperationalEnvServiceModelStatus serviceModelStatus =
133 dbHelper.insertRecordToOperationalEnvServiceModelStatus(requestId,
134 operationalEnvironmentId,
135 serviceModelVersionId,
136 DISTRIBUTION_STATUS_SENT,
140 modelStatusRepository.save(serviceModelStatus);
142 String distributionId = "";
144 jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
146 String statusCode = jsonResponse.get("statusCode").toString();
147 if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
148 distributionId = jsonResponse.get("distributionId").toString();
149 // should insert 1 row
150 OperationalEnvDistributionStatus distStatus =
151 dbHelper.insertRecordToOperationalEnvDistributionStatus(distributionId,
152 operationalEnvironmentId,
153 serviceModelVersionId,
155 DISTRIBUTION_STATUS_SENT,
157 distributionStatusRepository.save(distStatus);
160 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
161 String dbErrorMessage = " Failure calling SDC: statusCode: " + statusCode +
162 "; messageId: " + jsonResponse.get("messageId") +
163 "; message: " + jsonResponse.get("message");
165 requestDb.updateInfraFailureCompletion(dbErrorMessage, requestId, operationalEnvironmentId);
166 throw new ValidateException.Builder(dbErrorMessage,
167 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
174 * Get AAIOperationalEnvironment object
175 * @param operationalEnvironmentId - String
176 * @return operationalEnv - AAIOperationalEnvironment object
178 public AAIOperationalEnvironment getAAIOperationalEnvironment(String operationalEnvironmentId) {
179 AAIResultWrapper aaiResult = aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
180 return aaiResult.asBean(AAIOperationalEnvironment.class).orElse(new AAIOperationalEnvironment());