Containerization feature of SO
[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
26 import javax.ws.rs.core.Response;
27
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;
51
52
53 @Component
54 public class ActivateVnfOperationalEnvironment {
55
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";  
59         
60         @Autowired
61         private ActivateVnfDBHelper dbHelper;   
62         @Autowired
63         private AAIClientHelper aaiHelper;
64         @Autowired
65         private RequestsDBHelper requestDb;
66         @Autowired 
67         private SDCClientHelper sdcClientHelper;
68         
69         @Value("${mso.tenant.isolation.retry.count}")
70         private String sdcRetryCount;
71         
72         /**
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
79          */             
80         public void execute(String requestId, CloudOrchestrationRequest request, OperationalEnvDistributionStatusRepository distributionStatusRepository, 
81                                                         OperationalEnvServiceModelStatusRepository modelStatusRepository) throws ApiException{
82                 String operationalEnvironmentId = request.getOperationalEnvironmentId();
83
84                 String vidWorkloadContext = request.getRequestDetails().getRequestParameters().getWorkloadContext();
85                 List<ServiceModelList> serviceModelVersionIdList = request.getRequestDetails().getRequestParameters().getManifest().getServiceModelList();
86                         
87
88                 AAIOperationalEnvironment operationalEnv = getAAIOperationalEnvironment(operationalEnvironmentId);
89                 String workloadContext = operationalEnv.getWorkloadContext();
90                 msoLogger.debug("  aai workloadContext: " + workloadContext);
91                 if (!vidWorkloadContext.equals(workloadContext)) {
92
93
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();
97                 }
98
99                         processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext, distributionStatusRepository, modelStatusRepository);
100
101         }       
102         
103         
104         /**
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  
113          */             
114         public void processActivateSDCRequest(String requestId, String operationalEnvironmentId, List<ServiceModelList> serviceModelVersionIdList, 
115                                                                                         String workloadContext, OperationalEnvDistributionStatusRepository distributionStatusRepository, 
116                                                                                         OperationalEnvServiceModelStatusRepository modelStatusRepository) throws ApiException {
117                 
118                 JSONObject jsonResponse = null;         
119                 int retryCount = 0;
120                 try {
121                         retryCount = Integer.parseInt(sdcRetryCount);
122                 } catch (NumberFormatException e) {
123                         retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
124                 }
125
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();
130
131                         // should insert 1 row
132                         OperationalEnvServiceModelStatus serviceModelStatus = 
133                                 dbHelper.insertRecordToOperationalEnvServiceModelStatus(requestId,
134                                                                                                                                             operationalEnvironmentId,
135                                                                                                                                             serviceModelVersionId,
136                                                                                                                                             DISTRIBUTION_STATUS_SENT,
137                                                                                                                                             recoveryAction, 
138                                                                                                                                             retryCount,
139                                                                                                                                             workloadContext);                                   
140                         modelStatusRepository.save(serviceModelStatus);
141                         
142                         String distributionId = "";
143
144                         jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
145
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,
154                                                                 requestId,
155                                                                 DISTRIBUTION_STATUS_SENT,
156                                                                 "");
157                                 distributionStatusRepository.save(distStatus);
158
159                         } else {
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");
164
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();
168                         }
169                 }
170
171         }
172
173         /**
174          * Get AAIOperationalEnvironment object
175          * @param  operationalEnvironmentId - String 
176          * @return operationalEnv - AAIOperationalEnvironment object
177          */
178         public AAIOperationalEnvironment getAAIOperationalEnvironment(String operationalEnvironmentId) {
179                 AAIResultWrapper aaiResult = aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
180                 return aaiResult.asBean(AAIOperationalEnvironment.class).orElse(new AAIOperationalEnvironment());               
181         }
182
183 }