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.entities.AAIResultWrapper;
30 import org.onap.aaiclient.client.aai.entities.Relationships;
31 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
33 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
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 = relationships.getRelatedUris(Types.OPERATIONAL_ENVIRONMENT);
98 if (!operationalEnvironments.isEmpty()) {
99 ecompOperationalEnvironmentId = operationalEnvironments.get(0).getURIKeys().get(
100 AAIFluentTypeBuilder.Types.OPERATIONAL_ENVIRONMENT.getUriParams().operationalEnvironmentId);
103 logger.debug(" vnfOperationalEnvironmentId : {}", vnfOperationalEnvironmentId);
104 logger.debug(" ecompOperationalEnvironmentId : {}", ecompOperationalEnvironmentId);
106 OperationalEnvironment operationalEnv = wrapper.asBean(OperationalEnvironment.class).get();
107 String workloadContext = operationalEnv.getWorkloadContext();
108 logger.debug(" aai workloadContext: {}", workloadContext);
109 if (!vidWorkloadContext.equals(workloadContext)) {
110 ErrorLoggerInfo errorLoggerInfo =
111 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError)
113 throw new ValidateException.Builder(
114 " The vid workloadContext did not match from aai record. " + " vid workloadContext:"
115 + vidWorkloadContext + " aai workloadContext:" + workloadContext,
116 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
119 if (ecompOperationalEnvironmentId == null) {
120 ErrorLoggerInfo errorLoggerInfo =
121 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError)
123 throw new ValidateException.Builder(
124 " The ECOMP OE was not in aai record; the value of relationship.relationship-data key: "
125 + AAIFluentTypeBuilder.Types.OPERATIONAL_ENVIRONMENT
126 .getUriParams().operationalEnvironmentId,
127 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
131 processActivateSDCRequest(requestId, ecompOperationalEnvironmentId, serviceModelVersionIdList, workloadContext,
132 vnfOperationalEnvironmentId);
138 * The Method to send the Activation Requests to SDC
140 * @param requestId - String
141 * @param operationalEnvironmentId - String
142 * @param serviceModelVersionIdList - List<ServiceModelList> list
143 * @param workloadContext - String
144 * @return jsonResponse - JSONObject object
146 public void processActivateSDCRequest(String requestId, String operationalEnvironmentId,
147 List<ServiceModelList> serviceModelVersionIdList, String workloadContext,
148 String vnfOperationalEnvironmentId) throws ApiException {
150 JSONObject jsonResponse = null;
153 retryCount = Integer.parseInt(sdcRetryCount);
154 } catch (NumberFormatException e) {
155 retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
158 // loop through the serviceModelVersionId, and send request SDC
159 for (ServiceModelList serviceModelList : serviceModelVersionIdList) {
160 String serviceModelVersionId = serviceModelList.getServiceModelVersionId();
161 String recoveryAction = serviceModelList.getRecoveryAction().toString().toUpperCase();
163 // should insert 1 row
164 OperationalEnvServiceModelStatus serviceModelStatus =
165 dbHelper.insertRecordToOperationalEnvServiceModelStatus(requestId, operationalEnvironmentId,
166 serviceModelVersionId, DISTRIBUTION_STATUS_SENT, recoveryAction, retryCount,
167 workloadContext, vnfOperationalEnvironmentId);
168 client.save(serviceModelStatus);
170 String distributionId = "";
172 jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId,
173 operationalEnvironmentId, workloadContext);
175 String statusCode = jsonResponse.get("statusCode").toString();
176 if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
177 distributionId = jsonResponse.get("distributionId").toString();
178 // should insert 1 row
179 OperationalEnvDistributionStatus distStatus = dbHelper.insertRecordToOperationalEnvDistributionStatus(
180 distributionId, operationalEnvironmentId, serviceModelVersionId, requestId,
181 DISTRIBUTION_STATUS_SENT, "");
182 client.save(distStatus);
185 ErrorLoggerInfo errorLoggerInfo =
186 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError)
188 String dbErrorMessage = " Failure calling SDC: statusCode: " + statusCode + "; messageId: "
189 + jsonResponse.get("messageId") + "; message: " + jsonResponse.get("message");
191 requestDb.updateInfraFailureCompletion(dbErrorMessage, requestId, operationalEnvironmentId);
192 throw new ValidateException.Builder(dbErrorMessage, HttpStatus.SC_BAD_REQUEST,
193 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
200 * Get AAIOperationalEnvironment object
202 * @param operationalEnvironmentId - String
203 * @return operationalEnv - AAIOperationalEnvironment object
205 public AAIResultWrapper getAAIOperationalEnvironment(String operationalEnvironmentId) {
206 return aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);