Update license header in appc-provider files
[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-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
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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.provider.lcm.mock;
25
26 import org.onap.appc.executor.objects.LCMCommandStatus;
27 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
28 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
29
30 import java.io.File;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import org.onap.appc.util.JsonUtil;
34 import java.io.IOException;
35
36 import java.util.Map;
37
38
39 /**
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.
42  *
43  * When backend is implemented, this file should be removed.
44  */
45 public class MockQuiesceHelper extends AbstractMockHelper {
46     private final String MOCK_QUIESCE_DIR = "/tmp/lcm/quiescetraffic";
47     private final String PAUSE = "pause";
48
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();
54         }
55
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();
61         }
62         
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();
67         }
68
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();
74         }
75
76         File pauseDir = new File(pausePath);
77         boolean success = pauseDir.mkdir();
78         status = success ?
79             buildStatusWithoutParams(LCMCommandStatus.ACCEPTED) :
80             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
81                 String.format("Failed to quiesce traffic VNF %s", vnfId));
82
83         return setOutputStatus();
84     }
85
86     private boolean mockConditionExists() {
87         return isDirectoryExist(MOCK_QUIESCE_DIR);
88     }
89
90     private boolean isDirectoryExist(String path) {
91         return Files.isDirectory(Paths.get(path));
92     }
93     
94     private Map<String, String> getJsonMap(String jsonString) {
95         try {
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);
99         }
100         return null;
101     }
102 }