c04de54bcb23d5576dbc3e0708d9becebc83ce63
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / mock / MockUpgradeHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.provider.lcm.mock;
26
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;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Map;
37
38 /**
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.
41  *
42  * When backend is implemented, this file should be removed.
43  */
44 public class MockUpgradeHelper extends AbstractMockHelper {
45     private final String MOCK_UPGRADE_DIR = "/tmp/lcm/upgrade";
46     private final String UPGRADE = "upgrade";
47
48     public RequestHandlerOutput upgradePreCheck(RequestHandlerInput input) {
49         
50         if (!mockConditionExists()) {
51             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
52                 "The upgrade-pre-check command is not supported");
53             return setOutputStatus();
54         }
55
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();
61         }
62         
63         String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s-%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
64
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();
69         }
70
71         File upgradeDir = new File(upgradePath);
72         boolean success = upgradeDir.mkdir();
73         status = success ?
74             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
75             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
76                 String.format("Failed to upgradePreCheck for  VNF %s", vnfId));
77
78         return  setOutputStatus();
79     }
80     
81     public RequestHandlerOutput upgradePostCheck(RequestHandlerInput input) {
82         
83         if (!mockConditionExists()) {
84             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
85                 "The upgrade-post-check command is not supported");
86             return setOutputStatus();
87         }
88
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();
94         }
95         
96         String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
97
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();
102         }
103
104         File upgradeDir = new File(upgradePath);
105         boolean success = upgradeDir.mkdir();
106         status = success ?
107             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
108             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
109                 String.format("Failed to upgradePostCheck for %s ", vnfId));
110
111         return  setOutputStatus();
112     }
113     
114     public RequestHandlerOutput upgradeSoftware(RequestHandlerInput input) {
115         
116         if (!mockConditionExists()) {
117             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
118                 "The upgrade-software command is not supported");
119             return setOutputStatus();
120         }
121
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();
127         }
128         
129         String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
130
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();
135         }
136
137         File upgradeDir = new File(upgradePath);
138         boolean success = upgradeDir.mkdir();
139         status = success ?
140             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
141             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
142                 String.format("Failed to upgradeSoftware for  VNF %s", vnfId));
143
144         return  setOutputStatus();
145     }
146     
147     public RequestHandlerOutput upgradeBackup(RequestHandlerInput input) {
148         
149         if (!mockConditionExists()) {
150             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
151                 "The upgrade-backup command is not supported");
152             return setOutputStatus();
153         }
154
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();
160         }
161         
162         String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
163
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();
168         }
169
170         File upgradeDir = new File(upgradePath);
171         boolean success = upgradeDir.mkdir();
172         status = success ?
173             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
174             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
175                 String.format("Failed to upgradeBackup for VNF %s", vnfId));
176
177         return  setOutputStatus();
178     }
179     
180     public RequestHandlerOutput upgradeBackout(RequestHandlerInput input) {
181         
182         if (!mockConditionExists()) {
183             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
184                 "The upgrade-backout command is not supported");
185             return setOutputStatus();
186         }
187
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();
193         }
194         
195         String upgradePath = String.format(MOCK_UPGRADE_DIR+"/%s/%s", jsonMap.get("existing-software-version"),jsonMap.get("new-software-version"));
196
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();
201         }
202
203         File upgradeDir = new File(upgradePath);
204         boolean success = upgradeDir.mkdir();
205         status = success ?
206             buildStatusWithoutParams(LCMCommandStatus.SUCCESS) :
207             buildStatusForErrorMsg(LCMCommandStatus.REJECTED,
208                 String.format("Failed to upgradebackout for  VNF %s", vnfId));
209
210         return  setOutputStatus();
211     }
212
213     private boolean mockConditionExists() {
214         return isDirectoryExist(MOCK_UPGRADE_DIR);
215     }
216
217     private boolean isDirectoryExist(String path) {
218         return Files.isDirectory(Paths.get(path));
219     }
220
221     private Map<String, String> getJsonMap(String jsonString) {
222         try {
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);
226         }
227         return null;
228     }
229 }