2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider.lcm.mock;
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;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
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.
41 * When backend is implemented, this file should be removed.
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";
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();
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();
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();
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();
75 File volumeDir = new File(volumeIdPath);
76 boolean success = volumeDir.mkdir();
78 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
79 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
80 String.format("Failed to attach volume %s to VM %s", volumeId, vserverId));
82 return setOutputStatus();
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();
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();
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();
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();
113 File volumeDir = new File(volumeIdPath);
114 boolean success = volumeDir.delete();
116 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
117 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
118 String.format("Failed to attach volume %s to VM %s", volumeId, vserverId));
120 return setOutputStatus();
123 private boolean mockConditionExists() {
124 return isDirectoryExist(MOCK_VOLUME_DIR);
127 private boolean isDirectoryExist(String path) {
128 return Files.isDirectory(Paths.get(path));
131 private Map<String, String> getJsonMap(String jsonString) {
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);