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