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 upgrade actions are not implemented.
40 * Hence this class is here to mock the handling response of LCM upgrade actions REST API.
42 * When backend is implemented, this file should be removed.
44 public class MockUpgradeHelper extends AbstractMockHelper {
45 private final String MOCK_UPGRADE_DIR = "/tmp/lcm/upgrade";
46 private final String UPGRADE = "upgrade";
48 public RequestHandlerOutput upgradePreCheck(RequestHandlerInput input) {
50 if (!mockConditionExists()) {
51 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
52 "The upgrade-pre-check command is not supported");
53 return setOutputStatus();
56 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
57 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
58 if (jsonMap == null) {
59 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
60 return setOutputStatus();
63 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s-%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
65 if (isDirectoryExist(upgradePath)) {
66 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
67 String.format("UpgradeCheck from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
68 return setOutputStatus();
71 File upgradeDir = new File(upgradePath);
72 boolean success = upgradeDir.mkdir();
74 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
75 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
76 String.format("Failed to upgradePreCheck for VNF %s", vnfId));
78 return setOutputStatus();
81 public RequestHandlerOutput upgradePostCheck(RequestHandlerInput input) {
83 if (!mockConditionExists()) {
84 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
85 "The upgrade-post-check command is not supported");
86 return setOutputStatus();
89 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
90 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
91 if (jsonMap == null) {
92 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
93 return setOutputStatus();
96 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
98 if (isDirectoryExist(upgradePath)) {
99 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
100 String.format("UpgradePostcheck from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
101 return setOutputStatus();
104 File upgradeDir = new File(upgradePath);
105 boolean success = upgradeDir.mkdir();
107 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
108 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
109 String.format("Failed to upgradePostCheck for %s ", vnfId));
111 return setOutputStatus();
114 public RequestHandlerOutput upgradeSoftware(RequestHandlerInput input) {
116 if (!mockConditionExists()) {
117 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
118 "The upgrade-software command is not supported");
119 return setOutputStatus();
122 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
123 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
124 if (jsonMap == null) {
125 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
126 return setOutputStatus();
129 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
131 if (isDirectoryExist(upgradePath)) {
132 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
133 String.format("UpgradeSoftware from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
134 return setOutputStatus();
137 File upgradeDir = new File(upgradePath);
138 boolean success = upgradeDir.mkdir();
140 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
141 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
142 String.format("Failed to upgradeSoftware for VNF %s", vnfId));
144 return setOutputStatus();
147 public RequestHandlerOutput upgradeBackup(RequestHandlerInput input) {
149 if (!mockConditionExists()) {
150 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
151 "The upgrade-backup command is not supported");
152 return setOutputStatus();
155 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
156 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
157 if (jsonMap == null) {
158 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
159 return setOutputStatus();
162 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
164 if (isDirectoryExist(upgradePath)) {
165 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
166 String.format("UpgradeBackup from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
167 return setOutputStatus();
170 File upgradeDir = new File(upgradePath);
171 boolean success = upgradeDir.mkdir();
173 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
174 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
175 String.format("Failed to upgradeBackup for VNF %s", vnfId));
177 return setOutputStatus();
180 public RequestHandlerOutput upgradeBackout(RequestHandlerInput input) {
182 if (!mockConditionExists()) {
183 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
184 "The upgrade-backout command is not supported");
185 return setOutputStatus();
188 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
189 Map<String, String> jsonMap = getJsonMap(input.getRequestContext().getPayload());
190 if (jsonMap == null) {
191 status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, "payload reading failed");
192 return setOutputStatus();
195 String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
197 if (isDirectoryExist(upgradePath)) {
198 status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
199 String.format("UpgradeBackout from %s to %s is already happened to VNF %s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"), vnfId));
200 return setOutputStatus();
203 File upgradeDir = new File(upgradePath);
204 boolean success = upgradeDir.mkdir();
206 buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
207 buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
208 String.format("Failed to upgradebackout for VNF %s", vnfId));
210 return setOutputStatus();
213 private boolean mockConditionExists() {
214 return isDirectoryExist(MOCK_UPGRADE_DIR);
217 private boolean isDirectoryExist(String path) {
218 return Files.isDirectory(Paths.get(path));
221 private Map<String, String> getJsonMap(String jsonString) {
223 return JsonUtil.convertJsonStringToFlatMap(jsonString);
224 } catch (IOException e) {
225 logger.error(String.format("MockUpgradeHelper got exception when convert json map for (%s)", jsonString), e);