e89b5c8d42087cc835e6e338e5314bd5195dca14
[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
25 import javax.ws.rs.core.Response;
26
27 import org.apache.http.HttpStatus;
28 import org.json.JSONObject;
29 import org.onap.so.apihandler.common.ErrorNumbers;
30 import org.onap.so.requestsdb.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.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.logger.MessageEnum;
44 import org.onap.so.logger.MsoLogger;
45 import org.onap.so.requestsdb.RequestsDBHelper;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.stereotype.Component;
49
50
51 @Component
52 public class ActivateVnfOperationalEnvironment {
53
54         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, ActivateVnfOperationalEnvironment.class);
55         private static final int DEFAULT_ACTIVATE_RETRY_COUNT = 3;
56         private static final String DISTRIBUTION_STATUS_SENT = "SENT";  
57         
58         @Autowired
59         private ActivateVnfDBHelper dbHelper;   
60         @Autowired
61         private AAIClientHelper aaiHelper;
62         @Autowired
63         private RequestsDBHelper requestDb;
64         @Autowired 
65         private SDCClientHelper sdcClientHelper;
66         
67         @Value("${mso.tenant.isolation.retry.count}")
68         private String sdcRetryCount;
69         
70         @Autowired
71         RequestsDbClient client;
72         
73         /**
74          * The Point-Of-Entry from APIH with VID request to send activate request
75          * @param requestId - String
76          * @param request - CloudOrchestrationRequest object
77          * @return void - nothing
78          */             
79         public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException{
80                 String operationalEnvironmentId = request.getOperationalEnvironmentId();
81
82                 String vidWorkloadContext = request.getRequestDetails().getRequestParameters().getWorkloadContext();
83                 List<ServiceModelList> serviceModelVersionIdList = request.getRequestDetails().getRequestParameters().getManifest().getServiceModelList();
84                         
85
86                 AAIOperationalEnvironment operationalEnv = getAAIOperationalEnvironment(operationalEnvironmentId);
87                 String workloadContext = operationalEnv.getWorkloadContext();
88                 msoLogger.debug("  aai workloadContext: " + workloadContext);
89                 if (!vidWorkloadContext.equals(workloadContext)) {
90
91
92                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
93                         throw new ValidateException.Builder(" The vid workloadContext did not match from aai record. " + " vid workloadContext:" + vidWorkloadContext + " aai workloadContext:" + workloadContext,
94                                         HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
95                 }
96
97                         processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext);
98
99         }       
100         
101         
102         /**
103          * The Method to send the Activation Requests to SDC
104          * @param requestId - String
105          * @param operationalEnvironmentId - String   
106          * @param serviceModelVersionIdList - List<ServiceModelList> list
107          * @param workloadContext - String        
108          * @return jsonResponse - JSONObject object  
109          */             
110         public void processActivateSDCRequest(String requestId, String operationalEnvironmentId, 
111                                                                                   List<ServiceModelList> serviceModelVersionIdList, 
112                                                                                   String workloadContext) throws ApiException {
113                 
114                 JSONObject jsonResponse = null;         
115                 int retryCount = 0;
116                 try {
117                         retryCount = Integer.parseInt(sdcRetryCount);
118                 } catch (NumberFormatException e) {
119                         retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
120                 }
121
122                 // loop through the serviceModelVersionId, and send request SDC
123                 for(ServiceModelList serviceModelList : serviceModelVersionIdList){
124                         String serviceModelVersionId = serviceModelList.getServiceModelVersionId();
125                         String recoveryAction = serviceModelList.getRecoveryAction().toString().toUpperCase();
126
127                         // should insert 1 row
128                         OperationalEnvServiceModelStatus serviceModelStatus = 
129                                 dbHelper.insertRecordToOperationalEnvServiceModelStatus(requestId,
130                                                                                                                                             operationalEnvironmentId,
131                                                                                                                                             serviceModelVersionId,
132                                                                                                                                             DISTRIBUTION_STATUS_SENT,
133                                                                                                                                             recoveryAction, 
134                                                                                                                                             retryCount,
135                                                                                                                                             workloadContext);                                   
136                         client.save(serviceModelStatus);
137                         
138                         String distributionId = "";
139
140                         jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
141
142                         String statusCode = jsonResponse.get("statusCode").toString();
143                         if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
144                                 distributionId = jsonResponse.get("distributionId").toString();
145                                 // should insert 1 row
146                                 OperationalEnvDistributionStatus distStatus =
147                                                 dbHelper.insertRecordToOperationalEnvDistributionStatus(distributionId,
148                                                                 operationalEnvironmentId,
149                                                                 serviceModelVersionId,
150                                                                 requestId,
151                                                                 DISTRIBUTION_STATUS_SENT,
152                                                                 "");
153                                 client.save(distStatus);
154
155                         } else {
156                                 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
157                 String dbErrorMessage = " Failure calling SDC: statusCode: " + statusCode +
158                         "; messageId: " + jsonResponse.get("messageId") +
159                         "; message: " + jsonResponse.get("message");
160
161                 requestDb.updateInfraFailureCompletion(dbErrorMessage, requestId, operationalEnvironmentId);
162                                 throw new ValidateException.Builder(dbErrorMessage,
163                                                 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
164                         }
165                 }
166
167         }
168
169         /**
170          * Get AAIOperationalEnvironment object
171          * @param  operationalEnvironmentId - String 
172          * @return operationalEnv - AAIOperationalEnvironment object
173          */
174         public AAIOperationalEnvironment getAAIOperationalEnvironment(String operationalEnvironmentId) {
175                 AAIResultWrapper aaiResult = aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
176                 return aaiResult.asBean(AAIOperationalEnvironment.class).orElse(new AAIOperationalEnvironment());               
177         }
178
179 }