2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider.lcm.mock;
26 import org.onap.appc.executor.objects.LCMCommandStatus;
27 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
28 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import org.onap.appc.util.JsonUtil;
34 import java.io.IOException;
40 * This class is here because LCM quiesce backend is not implemented.
41 * Hence this class is here to mock the handling response of LCM quiesce REST API.
43 * When backend is implemented, this file should be removed.
45 public class MockQuiesceHelper extends AbstractMockHelper {
46 private final String MOCK_QUIESCE_DIR = "/tmp/lcm/quiescetraffic";
47 private final String PAUSE = "pause";
49 public RequestHandlerOutput quiesceTraffic(RequestHandlerInput input) {
50 if (!mockConditionExists()) {
51 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
52 "The quiesce-traffic command is not supported");
53 return setOutputStatus();
56 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
57 String vnfPath = String.format("%s/%s", MOCK_QUIESCE_DIR, vnfId);
58 if (!isDirectoryExist(vnfPath)) {
59 status = buildStatusForVnfId(LCMCommandStatus.VNF_NOT_FOUND, vnfId);
60 return setOutputStatus();
63 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
64 if (jsonMap == null) {
65 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
66 return setOutputStatus();
69 String pausePath = String.format("%s/%s", vnfPath, PAUSE);
70 if (isDirectoryExist(pausePath)) {
71 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
72 String.format("VNF %s is already quiesced", vnfId));
73 return setOutputStatus();
76 File pauseDir = new File(pausePath);
77 boolean success = pauseDir.mkdir();
79 buildStatusWithoutParams(LCMCommandStatus.ACCEPTED) :
80 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
81 String.format("Failed to quiesce traffic VNF %s", vnfId));
83 return setOutputStatus();
86 private boolean mockConditionExists() {
87 return isDirectoryExist(MOCK_QUIESCE_DIR);
90 private boolean isDirectoryExist(String path) {
91 return Files.isDirectory(Paths.get(path));
94 private Map<String, String> getJsonMap(String jsonString) {
96 return JsonUtil.convertJsonStringToFlatMap(jsonString);
97 } catch (IOException e) {
98 logger.error(String.format("MockQuiesceHelper got exception when convert json map for (%s)", jsonString), e);