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