Update license header in appc-provider files
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / mock / MockResumeHelper.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 java.util.Map;
34
35 /**
36  * This class is here because LCM resume backend is not implemented.
37  * Hence this class is here to mock the handling response of LCM resume REST API.
38  *
39  * When backend is implemented, this file should be removed.
40  */
41 public class MockResumeHelper extends AbstractMockHelper {
42     private final String MOCK_RESUME_DIR = "/tmp/lcm/resumetraffic";
43     private final String RESUME = "resume";
44
45     public RequestHandlerOutput resumeTraffic(RequestHandlerInput input) {
46         if (!mockConditionExists()) {
47             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
48                 "The resume-traffic command is not supported");
49             return setOutputStatus();
50         }
51
52         String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
53         String vnfPath = String.format("%s/%s", MOCK_RESUME_DIR, vnfId);
54         if (!isDirectoryExist(vnfPath)) {
55             status = buildStatusForVnfId(LCMCommandStatus.VNF_NOT_FOUND, vnfId);
56             return setOutputStatus();
57         }
58
59         String resumePath = String.format("%s/%s", vnfPath, RESUME);
60         if (isDirectoryExist(resumePath)) {
61             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
62                 String.format("VNF %s is already resumed", vnfId));
63             return setOutputStatus();
64         }
65
66         File resumeDir = new File(resumePath);
67         boolean success = resumeDir.mkdir();
68         status = success ?
69             buildStatusWithoutParams(LCMCommandStatus.ACCEPTED) :
70             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
71                 String.format("Failed to resume traffic VNF %s", vnfId));
72
73         return setOutputStatus();
74     }
75
76     private boolean mockConditionExists() {
77         return isDirectoryExist(MOCK_RESUME_DIR);
78     }
79
80     private boolean isDirectoryExist(String path) {
81         return Files.isDirectory(Paths.get(path));
82     }
83 }