2  * ============LICENSE_START=======================================================
 
   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
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  23 package org.onap.so.apihandlerinfra.tenantisolation.process;
 
  25 import java.util.Optional;
 
  26 import org.apache.commons.lang3.StringUtils;
 
  27 import org.apache.http.HttpStatus;
 
  28 import org.onap.aai.domain.yang.OperationalEnvironment;
 
  29 import org.onap.so.apihandler.common.ErrorNumbers;
 
  30 import org.onap.so.apihandlerinfra.exceptions.ApiException;
 
  31 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
 
  32 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
 
  33 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
 
  34 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
 
  35 import org.onap.so.client.aai.entities.AAIResultWrapper;
 
  36 import org.onap.logging.filter.base.ErrorCode;
 
  37 import org.onap.so.logger.MessageEnum;
 
  38 import org.onap.so.requestsdb.RequestsDBHelper;
 
  39 import org.slf4j.Logger;
 
  40 import org.slf4j.LoggerFactory;
 
  41 import org.springframework.beans.factory.annotation.Autowired;
 
  42 import org.springframework.stereotype.Component;
 
  45 public class DeactivateVnfOperationalEnvironment {
 
  47     private static Logger logger = LoggerFactory.getLogger(DeactivateVnfOperationalEnvironment.class);
 
  50     private AAIClientHelper aaiHelper;
 
  52     private RequestsDBHelper requestDb;
 
  54     public void execute(String requestId, CloudOrchestrationRequest request) throws ApiException {
 
  55         String operationalEnvironmentId = request.getOperationalEnvironmentId();
 
  57         OperationalEnvironment aaiOpEnv = getAAIOperationalEnvironment(operationalEnvironmentId);
 
  58         if (aaiOpEnv != null) {
 
  59             String operationalEnvironmentStatus = aaiOpEnv.getOperationalEnvironmentStatus();
 
  61             if (StringUtils.isBlank(operationalEnvironmentStatus)) {
 
  63                         "OperationalEnvironmentStatus is null on OperationalEnvironmentId: " + operationalEnvironmentId;
 
  64                 ErrorLoggerInfo errorLoggerInfo =
 
  65                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
 
  66                 throw new ValidateException.Builder(error, HttpStatus.SC_BAD_REQUEST,
 
  67                         ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
 
  70             if (operationalEnvironmentStatus.equalsIgnoreCase("ACTIVE")) {
 
  72                 aaiOpEnv.setOperationalEnvironmentStatus("INACTIVE");
 
  73                 aaiHelper.updateAaiOperationalEnvironment(operationalEnvironmentId, aaiOpEnv);
 
  75             } else if (!operationalEnvironmentStatus.equalsIgnoreCase("INACTIVE")) {
 
  77                         "Invalid OperationalEnvironmentStatus on OperationalEnvironmentId: " + operationalEnvironmentId;
 
  78                 ErrorLoggerInfo errorLoggerInfo =
 
  79                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
 
  80                 ValidateException validateException = new ValidateException.Builder(error, HttpStatus.SC_BAD_REQUEST,
 
  81                         ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
 
  82                 requestDb.updateInfraFailureCompletion(error, requestId, operationalEnvironmentId);
 
  83                 throw validateException;
 
  86             requestDb.updateInfraSuccessCompletion("SUCCESSFULLY Deactivated OperationalEnvironment", requestId,
 
  87                     operationalEnvironmentId);
 
  91     private OperationalEnvironment getAAIOperationalEnvironment(String operationalEnvironmentId) {
 
  92         AAIResultWrapper aaiResult = aaiHelper.getAaiOperationalEnvironment(operationalEnvironmentId);
 
  93         Optional<OperationalEnvironment> operationalEnvironmentOpt = aaiResult.asBean(OperationalEnvironment.class);
 
  94         return operationalEnvironmentOpt.isPresent() ? operationalEnvironmentOpt.get() : null;