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 upgrade actions are not implemented.
39 * Hence this class is here to mock the handling response of LCM upgrade actions REST API.
41 * When backend is implemented, this file should be removed.
43 public class MockUpgradeHelper extends AbstractMockHelper {
44 private final String MOCK_UPGRADE_DIR = "/tmp/lcm/upgrade";
45 private final String UPGRADE = "upgrade";
47 public RequestHandlerOutput upgradePreCheck(RequestHandlerInput input) {
49 if (!mockConditionExists()) {
50 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
51 "The upgrade-pre-check command is not supported");
52 return setOutputStatus();
55 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
56 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
57 if (jsonMap == null) {
58 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
59 return setOutputStatus();
62 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s-%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
64 if (isDirectoryExist(upgradePath)) {
65 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
66 String.format("UpgradeCheck from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
67 return setOutputStatus();
70 File upgradeDir = new File(upgradePath);
71 boolean success = upgradeDir.mkdir();
73 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
74 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
75 String.format("Failed to upgradePreCheck for VNF %s", vnfId));
77 return setOutputStatus();
80 public RequestHandlerOutput upgradePostCheck(RequestHandlerInput input) {
82 if (!mockConditionExists()) {
83 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
84 "The upgrade-post-check command is not supported");
85 return setOutputStatus();
88 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
89 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
90 if (jsonMap == null) {
91 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
92 return setOutputStatus();
95 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
97 if (isDirectoryExist(upgradePath)) {
98 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
99 String.format("UpgradePostcheck from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
100 return setOutputStatus();
103 File upgradeDir = new File(upgradePath);
104 boolean success = upgradeDir.mkdir();
106 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
107 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
108 String.format("Failed to upgradePostCheck for %s ", vnfId));
110 return setOutputStatus();
113 public RequestHandlerOutput upgradeSoftware(RequestHandlerInput input) {
115 if (!mockConditionExists()) {
116 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
117 "The upgrade-software command is not supported");
118 return setOutputStatus();
121 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
122 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
123 if (jsonMap == null) {
124 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
125 return setOutputStatus();
128 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
130 if (isDirectoryExist(upgradePath)) {
131 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
132 String.format("UpgradeSoftware from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
133 return setOutputStatus();
136 File upgradeDir = new File(upgradePath);
137 boolean success = upgradeDir.mkdir();
139 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
140 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
141 String.format("Failed to upgradeSoftware for VNF %s", vnfId));
143 return setOutputStatus();
146 public RequestHandlerOutput upgradeBackup(RequestHandlerInput input) {
148 if (!mockConditionExists()) {
149 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
150 "The upgrade-backup command is not supported");
151 return setOutputStatus();
154 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
155 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
156 if (jsonMap == null) {
157 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
158 return setOutputStatus();
161 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
163 if (isDirectoryExist(upgradePath)) {
164 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
165 String.format("UpgradeBackup from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
166 return setOutputStatus();
169 File upgradeDir = new File(upgradePath);
170 boolean success = upgradeDir.mkdir();
172 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
173 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
174 String.format("Failed to upgradeBackup for VNF %s", vnfId));
176 return setOutputStatus();
179 public RequestHandlerOutput upgradeBackout(RequestHandlerInput input) {
181 if (!mockConditionExists()) {
182 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
183 "The upgrade-backout command is not supported");
184 return setOutputStatus();
187 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
188 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
189 if (jsonMap == null) {
190 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
191 return setOutputStatus();
194 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
196 if (isDirectoryExist(upgradePath)) {
197 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
198 String.format("UpgradeBackout from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
199 return setOutputStatus();
202 File upgradeDir = new File(upgradePath);
203 boolean success = upgradeDir.mkdir();
205 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
206 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
207 String.format("Failed to upgradebackout for VNF %s", vnfId));
209 return setOutputStatus();
212 private boolean mockConditionExists() {
213 return isDirectoryExist(MOCK_UPGRADE_DIR);
216 private boolean isDirectoryExist(String path) {
217 return Files.isDirectory(Paths.get(path));
220 private Map<String, String> getJsonMap(String jsonString) {
222 return JsonUtil.convertJsonStringToFlatMap(jsonString);
223 } catch (IOException e) {
224 logger.error(String.format("MockUpgradeHelper got exception when convert json map for (%s)", jsonString), e);