Removed MsoLogger class
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandlerinfra.tenantisolation.process;
24
25 import java.util.Optional;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.http.HttpStatus;
29 import org.onap.aai.domain.yang.OperationalEnvironment;
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.client.aai.entities.AAIResultWrapper;
37 import org.onap.so.logger.ErrorCode;
38 import org.onap.so.logger.MessageEnum;
39 import org.onap.so.requestsdb.RequestsDBHelper;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 @Component
46 public class DeactivateVnfOperationalEnvironment {
47
48         private static Logger logger = LoggerFactory.getLogger(DeactivateVnfOperationalEnvironment
49                 .class);
50         
51     @Autowired 
52     private AAIClientHelper aaiHelper;
53     @Autowired 
54     private RequestsDBHelper requestDb;
55         
56         public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException {
57                 String operationalEnvironmentId = request.getOperationalEnvironmentId();
58                 
59                 OperationalEnvironment aaiOpEnv = getAAIOperationalEnvironment(operationalEnvironmentId);
60                 if (aaiOpEnv != null) {
61                         String operationalEnvironmentStatus = aaiOpEnv.getOperationalEnvironmentStatus();
62         
63                         if(StringUtils.isBlank(operationalEnvironmentStatus)) {
64                     String error = "OperationalEnvironmentStatus is null on OperationalEnvironmentId: " + operationalEnvironmentId;
65                     ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
66                     throw new ValidateException.Builder(error, HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
67                                                                         .errorInfo(errorLoggerInfo)
68                                                                         .build();
69                         }
70                         
71                         if(operationalEnvironmentStatus.equalsIgnoreCase("ACTIVE")) {
72         
73                                 aaiOpEnv.setOperationalEnvironmentStatus("INACTIVE");
74                                 aaiHelper.updateAaiOperationalEnvironment(operationalEnvironmentId, aaiOpEnv);
75                                 
76                         } else if(!operationalEnvironmentStatus.equalsIgnoreCase("INACTIVE")) {
77                     String error = "Invalid OperationalEnvironmentStatus on OperationalEnvironmentId: " + operationalEnvironmentId;
78                     ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
79                     ValidateException validateException = new ValidateException.Builder(error,
80                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
81                     requestDb.updateInfraFailureCompletion(error, requestId, operationalEnvironmentId);
82                     throw validateException;
83                         }
84                         
85                         requestDb.updateInfraSuccessCompletion("SUCCESSFULLY Deactivated OperationalEnvironment", requestId, operationalEnvironmentId);
86                 }
87         }
88         
89         private OperationalEnvironment getAAIOperationalEnvironment(String operationalEnvironmentId) {
90                 AAIResultWrapper aaiResult = aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
91                 Optional<OperationalEnvironment> operationalEnvironmentOpt = aaiResult.asBean(OperationalEnvironment.class);
92                 return operationalEnvironmentOpt.isPresent() ? operationalEnvironmentOpt.get() : null;
93         }
94 }