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