Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / tenantisolation / process / ActivateVnfOperationalEnvironment.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra.tenantisolation.process;
22
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.so.apihandler.common.ErrorNumbers;
30 import org.onap.so.db.request.client.RequestsDbClient;
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.AAIObjectType;
40 import org.onap.so.client.aai.entities.AAIResultWrapper;
41 import org.onap.so.client.aai.entities.Relationships;
42 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
43 import org.onap.so.db.request.beans.OperationalEnvDistributionStatus;
44 import org.onap.so.db.request.beans.OperationalEnvServiceModelStatus;
45 import org.onap.so.logger.ErrorCode;
46 import org.onap.so.logger.MessageEnum;
47 import org.onap.so.requestsdb.RequestsDBHelper;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.beans.factory.annotation.Value;
52 import org.springframework.stereotype.Component;
53
54
55 @Component
56 public class ActivateVnfOperationalEnvironment {
57
58     private static final Logger logger = LoggerFactory.getLogger(ActivateVnfOperationalEnvironment.class);
59     private static final int DEFAULT_ACTIVATE_RETRY_COUNT = 3;
60     private static final String DISTRIBUTION_STATUS_SENT = "SENT";
61     private static final String OPER_ENVIRONMENT_ID_KEY = "operational-environment-id";
62
63     @Autowired
64     private ActivateVnfDBHelper dbHelper;
65     @Autowired
66     private AAIClientHelper aaiHelper;
67     @Autowired
68     private RequestsDBHelper requestDb;
69     @Autowired
70     private SDCClientHelper sdcClientHelper;
71
72     @Value("${mso.tenant.isolation.retry.count}")
73     private String sdcRetryCount;
74
75     @Autowired
76     RequestsDbClient client;
77
78     /**
79      * The Point-Of-Entry from APIH with VID request to send activate request
80      * 
81      * @param requestId - String
82      * @param request - CloudOrchestrationRequest object
83      * @return void - nothing
84      */
85     public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException {
86         String vnfOperationalEnvironmentId = request.getOperationalEnvironmentId();
87
88         String vidWorkloadContext = request.getRequestDetails().getRequestParameters().getWorkloadContext();
89         List<ServiceModelList> serviceModelVersionIdList =
90                 request.getRequestDetails().getRequestParameters().getManifest().getServiceModelList();
91
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 =
101                         operationalEnvironments.get(0).getURIKeys().get(OPER_ENVIRONMENT_ID_KEY);
102             }
103         }
104         logger.debug("  vnfOperationalEnvironmentId   : {}", vnfOperationalEnvironmentId);
105         logger.debug("  ecompOperationalEnvironmentId : {}", ecompOperationalEnvironmentId);
106
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.BusinessProcesssError)
113                             .build();
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)
118                             .build();
119         }
120         if (ecompOperationalEnvironmentId == null) {
121             ErrorLoggerInfo errorLoggerInfo =
122                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
123                             .build();
124             throw new ValidateException.Builder(
125                     " The ECOMP OE was not in aai record; the value of relationship.relationship-data key: "
126                             + OPER_ENVIRONMENT_ID_KEY,
127                     HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
128                             .build();
129         }
130
131         processActivateSDCRequest(requestId, ecompOperationalEnvironmentId, serviceModelVersionIdList, workloadContext,
132                 vnfOperationalEnvironmentId);
133
134     }
135
136
137     /**
138      * The Method to send the Activation Requests to SDC
139      * 
140      * @param requestId - String
141      * @param operationalEnvironmentId - String
142      * @param serviceModelVersionIdList - List<ServiceModelList> list
143      * @param workloadContext - String
144      * @return jsonResponse - JSONObject object
145      */
146     public void processActivateSDCRequest(String requestId, String operationalEnvironmentId,
147             List<ServiceModelList> serviceModelVersionIdList, String workloadContext,
148             String vnfOperationalEnvironmentId) throws ApiException {
149
150         JSONObject jsonResponse = null;
151         int retryCount = 0;
152         try {
153             retryCount = Integer.parseInt(sdcRetryCount);
154         } catch (NumberFormatException e) {
155             retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
156         }
157
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();
162
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);
169
170             String distributionId = "";
171
172             jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId,
173                     operationalEnvironmentId, workloadContext);
174
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);
183
184             } else {
185                 ErrorLoggerInfo errorLoggerInfo =
186                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
187                                 .build();
188                 String dbErrorMessage = " Failure calling SDC: statusCode: " + statusCode + "; messageId: "
189                         + jsonResponse.get("messageId") + "; message: " + jsonResponse.get("message");
190
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();
194             }
195         }
196
197     }
198
199     /**
200      * Get AAIOperationalEnvironment object
201      * 
202      * @param operationalEnvironmentId - String
203      * @return operationalEnv - AAIOperationalEnvironment object
204      */
205     public AAIResultWrapper getAAIOperationalEnvironment(String operationalEnvironmentId) {
206         return aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
207     }
208
209 }