Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / mock / MockQuiesceHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.provider.lcm.mock;
26
27 import org.onap.appc.executor.objects.LCMCommandStatus;
28 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
29 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
30
31 import java.io.File;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import org.onap.appc.util.JsonUtil;
35 import java.io.IOException;
36
37 import java.util.Map;
38
39
40 /**
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.
43  *
44  * When backend is implemented, this file should be removed.
45  */
46 public class MockQuiesceHelper extends AbstractMockHelper {
47     private final String MOCK_QUIESCE_DIR = "/tmp/lcm/quiescetraffic";
48     private final String PAUSE = "pause";
49
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();
55         }
56
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();
62         }
63         
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();
68         }
69
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();
75         }
76
77         File pauseDir = new File(pausePath);
78         boolean success = pauseDir.mkdir();
79         status = success ?
80             buildStatusWithoutParams(LCMCommandStatus.ACCEPTED) :
81             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
82                 String.format("Failed to quiesce traffic VNF %s", vnfId));
83
84         return setOutputStatus();
85     }
86
87     private boolean mockConditionExists() {
88         return isDirectoryExist(MOCK_QUIESCE_DIR);
89     }
90
91     private boolean isDirectoryExist(String path) {
92         return Files.isDirectory(Paths.get(path));
93     }
94     
95     private Map<String, String> getJsonMap(String jsonString) {
96         try {
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);
100         }
101         return null;
102     }
103 }