1d2a445c40cda82f46f812e34b41749243b5c341
[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.aai.domain.yang.OperationalEnvironment;
31 import org.onap.aai.domain.yang.RelationshipList;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.onap.aai.domain.yang.RelationshipData;
34 import org.onap.so.apihandler.common.ErrorNumbers;
35 import org.onap.so.db.request.client.RequestsDbClient;
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.client.aai.AAIObjectType;
45 import org.onap.so.client.aai.entities.AAIResultWrapper;
46 import org.onap.so.client.aai.entities.Relationships;
47 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
48 import org.onap.so.db.request.beans.OperationalEnvDistributionStatus;
49 import org.onap.so.db.request.beans.OperationalEnvServiceModelStatus;
50 import org.onap.so.logger.MessageEnum;
51 import org.onap.so.logger.MsoLogger;
52 import org.onap.so.requestsdb.RequestsDBHelper;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.beans.factory.annotation.Value;
55 import org.springframework.stereotype.Component;
56
57
58 @Component
59 public class ActivateVnfOperationalEnvironment {
60
61         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, ActivateVnfOperationalEnvironment.class);
62         private static final int DEFAULT_ACTIVATE_RETRY_COUNT = 3;
63         private static final String DISTRIBUTION_STATUS_SENT = "SENT";  
64         private static final String OPER_ENVIRONMENT_ID_KEY = "operational-environment-id";
65         
66         @Autowired
67         private ActivateVnfDBHelper dbHelper;   
68         @Autowired
69         private AAIClientHelper aaiHelper;
70         @Autowired
71         private RequestsDBHelper requestDb;
72         @Autowired 
73         private SDCClientHelper sdcClientHelper;
74         
75         @Value("${mso.tenant.isolation.retry.count}")
76         private String sdcRetryCount;
77         
78         @Autowired
79         RequestsDbClient client;
80         
81         /**
82          * The Point-Of-Entry from APIH with VID request to send activate request
83          * @param requestId - String
84          * @param request - CloudOrchestrationRequest object
85          * @return void - nothing
86          */             
87         public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException{
88                 String vnfOperationalEnvironmentId = request.getOperationalEnvironmentId();
89
90                 String vidWorkloadContext = request.getRequestDetails().getRequestParameters().getWorkloadContext();
91                 List<ServiceModelList> serviceModelVersionIdList = request.getRequestDetails().getRequestParameters().getManifest().getServiceModelList();
92                 
93                 String ecompOperationalEnvironmentId = null;
94                 AAIResultWrapper wrapper = getAAIOperationalEnvironment(vnfOperationalEnvironmentId);
95                 Optional<Relationships> optRelationships = wrapper.getRelationships();
96                 if (optRelationships.isPresent()) {
97                    Relationships relationships = optRelationships.get();
98                    List<AAIResourceUri> operationalEnvironments = relationships.getRelatedAAIUris(AAIObjectType.OPERATIONAL_ENVIRONMENT);
99                    if (!operationalEnvironments.isEmpty()) {
100                            ecompOperationalEnvironmentId = operationalEnvironments.get(0).getURIKeys().get(OPER_ENVIRONMENT_ID_KEY);
101                    }
102                 }
103                 msoLogger.debug("  vnfOperationalEnvironmentId   : " + vnfOperationalEnvironmentId);            
104                 msoLogger.debug("  ecompOperationalEnvironmentId : " + ecompOperationalEnvironmentId);
105
106                 OperationalEnvironment operationalEnv = wrapper.asBean(OperationalEnvironment.class).get();
107                 String workloadContext = operationalEnv.getWorkloadContext();
108                 msoLogger.debug("  aai workloadContext: " + workloadContext);
109                 if (!vidWorkloadContext.equals(workloadContext)) {
110                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
111                         throw new ValidateException.Builder(" The vid workloadContext did not match from aai record. " + " vid workloadContext:" + vidWorkloadContext + " aai workloadContext:" + workloadContext,
112                                         HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
113                 }
114                 if (ecompOperationalEnvironmentId==null) {
115                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
116                         throw new ValidateException.Builder(" The ECOMP OE was not in aai record; the value of relationship.relationship-data key: " + OPER_ENVIRONMENT_ID_KEY,
117                                         HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
118                 }               
119
120                 processActivateSDCRequest(requestId, ecompOperationalEnvironmentId, serviceModelVersionIdList, workloadContext);
121
122         }       
123         
124         
125         /**
126          * The Method to send the Activation Requests to SDC
127          * @param requestId - String
128          * @param operationalEnvironmentId - String   
129          * @param serviceModelVersionIdList - List<ServiceModelList> list
130          * @param workloadContext - String        
131          * @return jsonResponse - JSONObject object  
132          */             
133         public void processActivateSDCRequest(String requestId, String operationalEnvironmentId, 
134                                                                                   List<ServiceModelList> serviceModelVersionIdList, 
135                                                                                   String workloadContext) throws ApiException {
136                 
137                 JSONObject jsonResponse = null;         
138                 int retryCount = 0;
139                 try {
140                         retryCount = Integer.parseInt(sdcRetryCount);
141                 } catch (NumberFormatException e) {
142                         retryCount = DEFAULT_ACTIVATE_RETRY_COUNT;
143                 }
144
145                 // loop through the serviceModelVersionId, and send request SDC
146                 for(ServiceModelList serviceModelList : serviceModelVersionIdList){
147                         String serviceModelVersionId = serviceModelList.getServiceModelVersionId();
148                         String recoveryAction = serviceModelList.getRecoveryAction().toString().toUpperCase();
149
150                         // should insert 1 row
151                         OperationalEnvServiceModelStatus serviceModelStatus = 
152                                 dbHelper.insertRecordToOperationalEnvServiceModelStatus(requestId,
153                                                                                                                                             operationalEnvironmentId,
154                                                                                                                                             serviceModelVersionId,
155                                                                                                                                             DISTRIBUTION_STATUS_SENT,
156                                                                                                                                             recoveryAction, 
157                                                                                                                                             retryCount,
158                                                                                                                                             workloadContext);                                   
159                         client.save(serviceModelStatus);
160                         
161                         String distributionId = "";
162
163                         jsonResponse = sdcClientHelper.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
164
165                         String statusCode = jsonResponse.get("statusCode").toString();
166                         if (statusCode.equals(String.valueOf(Response.Status.ACCEPTED.getStatusCode()))) {
167                                 distributionId = jsonResponse.get("distributionId").toString();
168                                 // should insert 1 row
169                                 OperationalEnvDistributionStatus distStatus =
170                                                 dbHelper.insertRecordToOperationalEnvDistributionStatus(distributionId,
171                                                                 operationalEnvironmentId,
172                                                                 serviceModelVersionId,
173                                                                 requestId,
174                                                                 DISTRIBUTION_STATUS_SENT,
175                                                                 "");
176                                 client.save(distStatus);
177
178                         } else {
179                                 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
180                 String dbErrorMessage = " Failure calling SDC: statusCode: " + statusCode +
181                         "; messageId: " + jsonResponse.get("messageId") +
182                         "; message: " + jsonResponse.get("message");
183
184                 requestDb.updateInfraFailureCompletion(dbErrorMessage, requestId, operationalEnvironmentId);
185                                 throw new ValidateException.Builder(dbErrorMessage,
186                                                 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
187                         }
188                 }
189
190         }
191
192         /**
193          * Get AAIOperationalEnvironment object
194          * @param  operationalEnvironmentId - String 
195          * @return operationalEnv - AAIOperationalEnvironment object
196          */
197         public AAIResultWrapper getAAIOperationalEnvironment(String operationalEnvironmentId) {
198                 return aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
199         }
200
201 }