Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / mock / MockVolumeHelper.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 import org.onap.appc.util.JsonUtil;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Map;
37
38 /**
39  * This class is here because LCM attachVolume and detachVolume backends are not implemented.
40  * Hence this class is here to mock the handling response of LCM attachVolume and detachVolume REST API.
41  *
42  * When backend is implemented, this file should be removed.
43  */
44 public class MockVolumeHelper extends AbstractMockHelper {
45     private final String MOCK_VOLUME_DIR = "/tmp/lcm/volume";
46     private final String VOLUME_ID_KEY = "volumeAttachment.volumeId";
47
48     public RequestHandlerOutput attachVolume(RequestHandlerInput input) {
49         if (!mockConditionExists()) {
50             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
51                 "The attach-volume command is not supported");
52             return setOutputStatus();
53         }
54
55         String vserverId = input.getRequestContext().getActionIdentifiers().getVserverId();
56         String vserverPath = String.format("%s/%s", MOCK_VOLUME_DIR, vserverId);
57         if (!isDirectoryExist(vserverPath)) {
58             status = buildStatusForId(LCMCommandStatus.VSERVER_NOT_FOUND, vserverId);
59             return setOutputStatus();
60         }
61
62         Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
63         if (jsonMap == null) {
64             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
65             return setOutputStatus();
66         }
67
68         String volumeId = jsonMap.get(VOLUME_ID_KEY);
69         String volumeIdPath = String.format("%s/%s", vserverPath, volumeId);
70         if (isDirectoryExist(volumeIdPath)) {
71             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
72                 String.format("Volume %s is already attached to VM %s", volumeId, vserverId));
73             return setOutputStatus();
74         }
75
76         File volumeDir = new File(volumeIdPath);
77         boolean success = volumeDir.mkdir();
78         status = success ?
79             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
80             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
81                 String.format("Failed to attach volume %s to VM %s", volumeId, vserverId));
82
83         return  setOutputStatus();
84     }
85
86     public RequestHandlerOutput detachVolume(RequestHandlerInput input) {
87         if (!mockConditionExists()) {
88             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
89                 "The detach-volume command is not supported");
90             return setOutputStatus();
91         }
92
93         String vserverId = input.getRequestContext().getActionIdentifiers().getVserverId();
94         String vserverPath = String.format("%s/%s", MOCK_VOLUME_DIR, vserverId);
95         if (!isDirectoryExist(vserverPath)) {
96             status = buildStatusForId(LCMCommandStatus.VSERVER_NOT_FOUND, vserverId);
97             return setOutputStatus();
98         }
99
100         Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
101         if (jsonMap == null) {
102             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
103             return setOutputStatus();
104         }
105
106         String volumeId = jsonMap.get(VOLUME_ID_KEY);
107         String volumeIdPath = String.format("%s/%s", vserverPath, volumeId);
108         if (!isDirectoryExist(volumeIdPath)) {
109             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
110                 String.format("Volume %s is not attached to VM %s", volumeId, vserverId));
111             return setOutputStatus();
112         }
113
114         File volumeDir = new File(volumeIdPath);
115         boolean success = volumeDir.delete();
116         status = success ?
117             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
118             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
119                 String.format("Failed to attach volume %s to VM %s", volumeId, vserverId));
120
121         return  setOutputStatus();
122     }
123
124     private boolean mockConditionExists() {
125         return isDirectoryExist(MOCK_VOLUME_DIR);
126     }
127
128     private boolean isDirectoryExist(String path) {
129         return Files.isDirectory(Paths.get(path));
130     }
131
132     private Map<String, String> getJsonMap(String jsonString) {
133         try {
134             return JsonUtil.convertJsonStringToFlatMap(jsonString);
135         } catch (IOException e) {
136             logger.error(String.format("MockVolumeHelper got exception when convert json map for (%s)", jsonString), e);
137         }
138         return null;
139     }
140 }