2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   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.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.appc.provider.lcm.mock;
 
  27 import org.onap.appc.executor.objects.LCMCommandStatus;
 
  28 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
 
  29 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
 
  32 import java.nio.file.Files;
 
  33 import java.nio.file.Paths;
 
  34 import org.onap.appc.util.JsonUtil;
 
  35 import java.io.IOException;
 
  41  * This class is here because LCM quiesce backend is not implemented.
 
  42  * Hence this class is here to mock the handling response of LCM quiesce REST API.
 
  44  * When backend is implemented, this file should be removed.
 
  46 public class MockQuiesceHelper extends AbstractMockHelper {
 
  47     private final String MOCK_QUIESCE_DIR = "/tmp/lcm/quiescetraffic";
 
  48     private final String PAUSE = "pause";
 
  50     public RequestHandlerOutput quiesceTraffic(RequestHandlerInput input) {
 
  51         if (!mockConditionExists()) {
 
  52             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
 
  53                 "The quiesce-traffic command is not supported");
 
  54             return setOutputStatus();
 
  57         String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
 
  58         String vnfPath = String.format("%s/%s", MOCK_QUIESCE_DIR, vnfId);
 
  59         if (!isDirectoryExist(vnfPath)) {
 
  60             status = buildStatusForVnfId(LCMCommandStatus.VNF_NOT_FOUND, vnfId);
 
  61             return setOutputStatus();
 
  64         Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
 
  65         if (jsonMap == null) {
 
  66             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
 
  67             return setOutputStatus();
 
  70         String pausePath = String.format("%s/%s", vnfPath, PAUSE);
 
  71         if (isDirectoryExist(pausePath)) {
 
  72             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
 
  73                 String.format("VNF %s is already quiesced", vnfId));
 
  74             return setOutputStatus();
 
  77         File pauseDir = new File(pausePath);
 
  78         boolean success = pauseDir.mkdir();
 
  80             buildStatusWithoutParams(LCMCommandStatus.ACCEPTED) :
 
  81             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
 
  82                 String.format("Failed to quiesce traffic VNF %s", vnfId));
 
  84         return setOutputStatus();
 
  87     private boolean mockConditionExists() {
 
  88         return isDirectoryExist(MOCK_QUIESCE_DIR);
 
  91     private boolean isDirectoryExist(String path) {
 
  92         return Files.isDirectory(Paths.get(path));
 
  95     private Map<String, String> getJsonMap(String jsonString) {
 
  97             return JsonUtil.convertJsonStringToFlatMap(jsonString);
 
  98         } catch (IOException e) {
 
  99             logger.error(String.format("MockQuiesceHelper got exception when convert json map for (%s)", jsonString), e);