2  * ============LICENSE_START=======================================================
 
   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
 
  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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.appc.provider.lcm.mock;
 
  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;
 
  33 import java.io.IOException;
 
  34 import java.nio.file.Files;
 
  35 import java.nio.file.Paths;
 
  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.
 
  42  * When backend is implemented, this file should be removed.
 
  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";
 
  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();
 
  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();
 
  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();
 
  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();
 
  76         File volumeDir = new File(volumeIdPath);
 
  77         boolean success = volumeDir.mkdir();
 
  79             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
 
  80             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
 
  81                 String.format("Failed to attach volume %s to VM %s", volumeId, vserverId));
 
  83         return  setOutputStatus();
 
  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();
 
  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();
 
 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();
 
 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();
 
 114         File volumeDir = new File(volumeIdPath);
 
 115         boolean success = volumeDir.delete();
 
 117             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
 
 118             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
 
 119                 String.format("Failed to attach volume %s to VM %s", volumeId, vserverId));
 
 121         return  setOutputStatus();
 
 124     private boolean mockConditionExists() {
 
 125         return isDirectoryExist(MOCK_VOLUME_DIR);
 
 128     private boolean isDirectoryExist(String path) {
 
 129         return Files.isDirectory(Paths.get(path));
 
 132     private Map<String, String> getJsonMap(String jsonString) {
 
 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);