Containerization feature of SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / tenantisolation / process / DeactivateVnfOperationalEnvironment.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.Optional;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.http.HttpStatus;
27 import org.onap.so.apihandler.common.ErrorNumbers;
28 import org.onap.so.apihandlerinfra.exceptions.ApiException;
29 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
30 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
31 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
32 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
33 import org.onap.so.client.aai.entities.AAIResultWrapper;
34 import org.onap.so.client.aai.objects.AAIOperationalEnvironment;
35 import org.onap.so.logger.MessageEnum;
36 import org.onap.so.logger.MsoLogger;
37 import org.onap.so.requestsdb.RequestsDBHelper;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class DeactivateVnfOperationalEnvironment {
43
44         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, DeactivateVnfOperationalEnvironment.class);
45         
46     @Autowired 
47     private AAIClientHelper aaiHelper;
48     @Autowired 
49     private RequestsDBHelper requestDb;
50         
51         public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException {
52                 String operationalEnvironmentId = request.getOperationalEnvironmentId();
53                 
54                 AAIOperationalEnvironment aaiOpEnv = getAAIOperationalEnvironment(operationalEnvironmentId);
55                 if (aaiOpEnv != null) {
56                         String operationalEnvironmentStatus = aaiOpEnv.getOperationalEnvironmentStatus();
57         
58                         if(StringUtils.isBlank(operationalEnvironmentStatus)) {
59                     String error = "OperationalEnvironmentStatus is null on OperationalEnvironmentId: " + operationalEnvironmentId;
60                     ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
61                     throw new ValidateException.Builder(error, HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
62                                                                         .errorInfo(errorLoggerInfo)
63                                                                         .build();
64                         }
65                         
66                         if(operationalEnvironmentStatus.equalsIgnoreCase("ACTIVE")) {
67         
68                                 aaiOpEnv.setOperationalEnvironmentStatus("INACTIVE");
69                                 aaiHelper.updateAaiOperationalEnvironment(operationalEnvironmentId, aaiOpEnv);
70                                 
71                         } else if(!operationalEnvironmentStatus.equalsIgnoreCase("INACTIVE")) {
72                     String error = "Invalid OperationalEnvironmentStatus on OperationalEnvironmentId: " + operationalEnvironmentId;
73                     ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
74                     ValidateException validateException = new ValidateException.Builder(error,
75                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
76                     requestDb.updateInfraFailureCompletion(error, requestId, operationalEnvironmentId);
77                     throw validateException;
78                         }
79                         
80                         requestDb.updateInfraSuccessCompletion("SUCCESSFULLY Deactivated OperationalEnvironment", requestId, operationalEnvironmentId);
81                 }
82         }
83         
84         private AAIOperationalEnvironment getAAIOperationalEnvironment(String operationalEnvironmentId) {
85                 AAIResultWrapper aaiResult = aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
86                 Optional<AAIOperationalEnvironment> operationalEnvironmentOpt = aaiResult.asBean(AAIOperationalEnvironment.class);
87                 return operationalEnvironmentOpt.isPresent() ? operationalEnvironmentOpt.get() : null;
88         }
89 }