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;
25 import javax.ws.rs.core.Response;
26 import org.apache.http.HttpStatus;
27 import org.json.JSONObject;
28 import org.onap.aai.domain.yang.OperationalEnvironment;
29 import org.onap.aaiclient.client.aai.AAIObjectType;
30 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
31 import org.onap.aaiclient.client.aai.entities.Relationships;
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
33 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
34 import org.onap.logging.filter.base.ErrorCode;
35 import org.onap.so.apihandler.common.ErrorNumbers;
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.db.request.beans.OperationalEnvDistributionStatus;
45 import org.onap.so.db.request.beans.OperationalEnvServiceModelStatus;
46 import org.onap.so.db.request.client.RequestsDbClient;
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.beans.factory.annotation.Value;
53 import org.springframework.stereotype.Component;
57 public class ActivateVnfOperationalEnvironment {
59 private static final Logger logger = LoggerFactory.getLogger(ActivateVnfOperationalEnvironment.class);
60 private static final int DEFAULT_ACTIVATE_RETRY_COUNT = 3;
61 private static final String DISTRIBUTION_STATUS_SENT = "SENT";
64 private ActivateVnfDBHelper dbHelper;
66 private AAIClientHelper aaiHelper;
68 private RequestsDBHelper requestDb;
70 private SDCClientHelper sdcClientHelper;
72 @Value("${mso.tenant.isolation.retry.count}")
73 private String sdcRetryCount;
76 RequestsDbClient client;
79 * The Point-Of-Entry from APIH with VID request to send activate request
81 * @param requestId - String
82 * @param request - CloudOrchestrationRequest object
83 * @return void - nothing
85 public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException {
86 String vnfOperationalEnvironmentId = request.getOperationalEnvironmentId();
88 String vidWorkloadContext = request.getRequestDetails().getRequestParameters().getWorkloadContext();
89 List<ServiceModelList> serviceModelVersionIdList =
90 request.getRequestDetails().getRequestParameters().getManifest().getServiceModelList();
92 String ecompOperationalEnvironmentId = null;
93 AAIResultWrapper wrapper = getAAIOperationalEnvironment(vnfOperationalEnvironmentId);
94 Optional<Relationships> optRelationships = wrapper.getRelationships();
95 if (optRelationships.isPresent()) {
96 Relationships relationships = optRelationships.get();
97 List<AAIResourceUri> operationalEnvironments =
98 relationships.getRelatedAAIUris(AAIObjectType.OPERATIONAL_ENVIRONMENT);
99 if (!operationalEnvironments.isEmpty()) {
100 ecompOperationalEnvironmentId = operationalEnvironments.get(0).getURIKeys().get(
101 AAIFluentTypeBuilder.Types.OPERATIONAL_ENVIRONMENT.getUriParams().operationalEnvironmentId);
104 logger.debug(" vnfOperationalEnvironmentId : {}", vnfOperationalEnvironmentId);
105 logger.debug(" ecompOperationalEnvironmentId : {}", ecompOperationalEnvironmentId);
107 OperationalEnvironment operationalEnv = wrapper.asBean(OperationalEnvironment.class).get();
108 String workloadContext = operationalEnv.getWorkloadContext();
109 logger.debug(" aai workloadContext: {}", workloadContext);
110 if (!vidWorkloadContext.equals(workloadContext)) {
111 ErrorLoggerInfo errorLoggerInfo =
112 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError)
114 throw new ValidateException.Builder(
115 " The vid workloadContext did not match from aai record. " + " vid workloadContext:"
116 + vidWorkloadContext + " aai workloadContext:" + workloadContext,
117 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
120 if (ecompOperationalEnvironmentId == null) {
121 ErrorLoggerInfo errorLoggerInfo =
122 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError)
124 throw new ValidateException.Builder(
125 " The ECOMP OE was not in aai record; the value of relationship.relationship-data key: "
126 + AAIFluentTypeBuilder.Types.OPERATIONAL_ENVIRONMENT
127 .getUriParams().operationalEnvironmentId,
128 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
132 processActivateSDCRequest(requestId, ecompOperationalEnvironmentId, serviceModelVersionIdList, workloadContext,
133 vnfOperationalEnvironmentId);
139 * The Method to send the Activation Requests to SDC
141 * @param requestId - String
142 * @param operationalEnvironmentId - String
143 * @param serviceModelVersionIdList - List<ServiceModelList> list
144 * @param workloadContext - String
145 * @return jsonResponse - JSONObject object
147 public void processActivateSDCRequest(String requestId, String operationalEnvironmentId,
148 List<ServiceModelList> serviceModelVersionIdList, String workloadContext,
149 String vnfOperationalEnvironmentId) throws ApiException {
151 JSONObject jsonResponse = null;
154 retryCount = Integer.parseInt(sdcRetryCount);
155 } catch (NumberFormatException e) {
156 retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
159 // loop through the serviceModelVersionId, and send request SDC
160 for (ServiceModelList serviceModelList : serviceModelVersionIdList) {
161 String serviceModelVersionId = serviceModelList.getServiceModelVersionId();
162 String recoveryAction = serviceModelList.getRecoveryAction().toString().toUpperCase();
164 // should insert 1 row
165 OperationalEnvServiceModelStatus serviceModelStatus =
166 dbHelper.insertRecordToOperationalEnvServiceModelStatus(requestId, operationalEnvironmentId,
167 serviceModelVersionId, DISTRIBUTION_STATUS_SENT, recoveryAction, retryCount,
168 workloadContext, vnfOperationalEnvironmentId);
169 client.save(serviceModelStatus);
171 String distributionId = "";
173 jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId,
174 operationalEnvironmentId, workloadContext);
176 String statusCode = jsonResponse.get("statusCode").toString();
177 if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
178 distributionId = jsonResponse.get("distributionId").toString();
179 // should insert 1 row
180 OperationalEnvDistributionStatus distStatus = dbHelper.insertRecordToOperationalEnvDistributionStatus(
181 distributionId, operationalEnvironmentId, serviceModelVersionId, requestId,
182 DISTRIBUTION_STATUS_SENT, "");
183 client.save(distStatus);
186 ErrorLoggerInfo errorLoggerInfo =
187 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError)
189 String dbErrorMessage = " Failure calling SDC: statusCode: " + statusCode + "; messageId: "
190 + jsonResponse.get("messageId") + "; message: " + jsonResponse.get("message");
192 requestDb.updateInfraFailureCompletion(dbErrorMessage, requestId, operationalEnvironmentId);
193 throw new ValidateException.Builder(dbErrorMessage, HttpStatus.SC_BAD_REQUEST,
194 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
201 * Get AAIOperationalEnvironment object
203 * @param operationalEnvironmentId - String
204 * @return operationalEnv - AAIOperationalEnvironment object
206 public AAIResultWrapper getAAIOperationalEnvironment(String operationalEnvironmentId) {
207 return aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);