fix scale jobid
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / csm / vnf / VnfMgrVnfm.java
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.vnf;
18
19 import java.io.IOException;
20
21 import org.apache.commons.io.IOUtils;
22 import org.apache.commons.lang3.StringUtils;
23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.ResultRequestUtil;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl.AdapterResourceManager;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import net.sf.json.JSONArray;
33 import net.sf.json.JSONException;
34 import net.sf.json.JSONObject;
35
36 /**
37  * create or terminate VNF to M
38  * <br/>
39  *
40  * @author
41  * @version VFC 1.0 Aug 24, 2016
42  */
43 public class VnfMgrVnfm implements InterfaceVnfMgr {
44
45     private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
46
47     private static final int PARAM_ZERO = 0;
48
49     private static final int PARAM_ONE = 1;
50
51     @Override
52     public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
53         LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
54         JSONObject restJson = new JSONObject();
55         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
56         String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
57
58         int scaleType = getScaleType(vnfObject.getString("type"));
59         // build request json object
60         JSONObject paramJson = new JSONObject();
61         JSONObject scaleInfo = new JSONObject();
62         JSONArray vduList = new JSONArray();
63         JSONObject vdu = new JSONObject();
64         vdu.put("vdu_type", this.getVduType(vnfmObject, vnfInstanceId));
65         vdu.put("h_steps", vnfObject.get("numberOfSteps"));
66         vduList.add(vdu);
67         scaleInfo.put("vnf_id", vnfInstanceId);
68         scaleInfo.put("scale_pattern", "without_plan");
69         scaleInfo.put("scale_type", PARAM_ZERO);
70         scaleInfo.put("scale_action", scaleType);
71         scaleInfo.put("scale_step", PARAM_ZERO);
72         scaleInfo.put("scale_step_value", PARAM_ONE);
73         scaleInfo.put("scale_group", vdu.getString("vdu_type"));
74         scaleInfo.put("vdu_list", vduList);
75         if(scaleType == PARAM_ZERO) {
76             // scale_in
77             JSONArray vmList = new JSONArray();
78             try {
79                 // JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
80                 // vmList = additionalParam.getJSONArray("vm_list");
81                 vmList = AdapterResourceManager.readScaleInVmIdFromJson().getJSONArray("vm_list");
82             } catch(JSONException e) {
83                 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
84             }
85             if(null != vmList && !vmList.isEmpty()) {
86                 scaleInfo.put("vm_list", vmList);
87             }
88         }
89         paramJson.put("scale_info", scaleInfo);
90         JSONObject queryResult =
91                 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
92         LOG.info("SCALE execute result:" + queryResult.toString());
93         try {
94             int statusCode = queryResult.getInt(Constant.RETCODE);
95
96             if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
97                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
98                 // restJson.put("data",
99                 // queryResult.getJSONObject("data").getJSONObject("scale_info"));
100                 JSONObject appInfo = new JSONObject();
101                 try {
102                     appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("scale_info");
103                 } catch(JSONException e) {
104                     LOG.error("function=scaleVnf, msg=csm return obj is not a json, e={}.", e);
105                 }
106                 JSONObject resultObj = new JSONObject();
107                 // resultObj.put(Constant.JOBID, vnfInstanceId + "_" + Constant.PUT);
108                 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.PUT);
109                 restJson.put("data", resultObj);
110             } else {
111                 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
112             }
113
114         } catch(JSONException e) {
115             LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
116         }
117
118         return restJson;
119     }
120
121     private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
122         String vduType = "";
123         try {
124             JSONObject queryResult =
125                     ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
126                             Constant.GET, null, Constant.CERTIFICATE);
127             LOG.info("getVduType result=" + queryResult);
128             vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
129         } catch(Exception e) {
130             LOG.error("get vdu_type failed.", e);
131         }
132         LOG.info("vdu_type=" + vduType);
133         return vduType;
134     }
135
136     private int getScaleType(String type) {
137         if("SCALE_OUT".equalsIgnoreCase(type)) {
138             return 1;
139         } else if("SCALE_IN".equalsIgnoreCase(type)) {
140             return 0;
141         }
142         return -1;
143     }
144
145     @Override
146     public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
147         LOG.info("function=createVnf, msg=enter to create a vnf");
148         LOG.info("createVnf csm request body :" + subJsonObject);
149         JSONObject restJson = new JSONObject();
150         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
151         String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
152
153         JSONObject queryResult =
154                 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
155         LOG.info("createVnf csm response content:" + queryResult);
156         try {
157             int statusCode = queryResult.getInt(Constant.RETCODE);
158
159             if(statusCode == Constant.HTTP_CREATED) {
160                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
161                 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
162                 JSONObject resultObj = new JSONObject();
163                 // resultObj.put("vnfInstanceId", appInfo.getString("id"));
164                 // resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
165                 String vnfInstanceId = appInfo.getString("id");
166                 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.POST);
167                 restJson.put("data", resultObj);
168             } else {
169                 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
170             }
171
172         } catch(JSONException e) {
173             LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
174         }
175
176         return restJson;
177     }
178
179     private void handleResponse(JSONObject result, JSONObject returnObj, String vnfInstanceId, String type) {
180         String jobId = "";
181         if(returnObj.containsKey("job_id")) {
182             jobId = returnObj.getString("job_id") + ":job";
183         } else {
184             jobId = vnfInstanceId + "_" + type + ":no";
185         }
186         result.put("vnfInstanceId", vnfInstanceId);
187         result.put(Constant.JOBID, jobId);
188     }
189
190     @Override
191     public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
192         LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
193         JSONObject restJson = new JSONObject();
194         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
195
196         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
197                 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
198                 Constant.CERTIFICATE);
199
200         int statusCode = queryResult.getInt(Constant.RETCODE);
201
202         if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
203             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
204             // restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
205             JSONObject appInfo = new JSONObject();
206             if(queryResult.containsKey("data") && StringUtils.isNotEmpty(queryResult.getString("data")))
207             {
208                 appInfo = JSONObject.fromObject(queryResult.getString("data"));
209             }
210             JSONObject resultObj = new JSONObject();
211             // resultObj.put(Constant.JOBID, vnfId + "_" + Constant.DELETE);
212             handleResponse(resultObj, appInfo, vnfId, Constant.DELETE);
213             restJson.put("data", resultObj);
214         } else {
215             LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
216         }
217
218         return restJson;
219     }
220
221     @Override
222     public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
223         LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
224         JSONObject restJson = new JSONObject();
225         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
226
227         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
228                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
229
230         int statusCode = queryResult.getInt(Constant.RETCODE);
231
232         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
233             if(null == (queryResult.get("data"))) {
234                 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
235                 return restJson;
236             }
237             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
238             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
239         } else {
240             LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
241         }
242
243         return restJson;
244     }
245
246     public JSONObject getIp(JSONObject vnfmObject, String vnfId) throws IOException {
247         LOG.warn("function=getIp, msg=enter to getIp: {}", vnfId);
248         JSONObject restJson = new JSONObject();
249         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
250
251         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
252                 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
253
254         int statusCode = queryResult.getInt(Constant.RETCODE);
255
256         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
257             if(null == (queryResult.get("data"))) {
258                 LOG.warn("function=getIp, msg=query is null {}", queryResult.get("data"));
259                 return restJson;
260             }
261             JSONObject config = JSONObject.fromObject(queryResult.getString("data"));
262             LOG.info("function=getIp, query configuration result: {}", config);
263             JSONObject vnfInfo = config.getJSONArray("configuration").getJSONObject(0);
264             JSONObject result = new JSONObject();
265             result.put("vnf_id", vnfInfo.getString("vnf_id"));
266             result.put("vnf_type", vnfInfo.getString("vnf_type"));
267             JSONArray inputs = vnfInfo.getJSONArray("inputs");
268
269             ClassLoader classLoader = getClass().getClassLoader();
270             String ipConfig = IOUtils.toString(classLoader.getResourceAsStream("ipConfig.json"));
271             LOG.info("ipConfig: {}", ipConfig);
272             JSONObject ipCon = JSONObject.fromObject(ipConfig);
273             String vnfType = vnfInfo.getString("vnf_type");
274             if(ipCon.containsKey(vnfType)) {
275                 String ipKey = ipCon.getString(vnfInfo.getString("vnf_type"));
276                 LOG.info("ipKey: {}", ipKey);
277                 String ip = "";
278                 for(int i = 0; i < inputs.size(); i++) {
279                     JSONObject obj = inputs.getJSONObject(i);
280                     if(obj.getString("key_name").equals(ipKey)) {
281                         ip = obj.getString("value");
282                         break;
283                     }
284                 }
285                 result.put("ip", ip);
286                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
287                 restJson.put("data", result);
288             }
289
290         } else {
291             LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
292         }
293
294         return restJson;
295     }
296
297     @Override
298     public JSONObject getJob(JSONObject vnfmObject, String jobId) {
299         LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
300         JSONObject restJson = new JSONObject();
301         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
302
303         String vnfId = jobId.split("_")[0];
304         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
305                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
306                 null, Constant.CERTIFICATE);
307
308         int statusCode = queryResult.getInt(Constant.RETCODE);
309
310         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
311
312             if((queryResult.get("data")) == null) {
313                 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
314                 return restJson;
315             }
316             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
317             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
318         } else {
319             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
320         }
321
322         return restJson;
323     }
324
325     /**
326      * <br>
327      * 
328      * @param jsonObject
329      * @param vnfmObjcet
330      * @param vnfmId
331      * @param vnfInstanceId
332      * @return
333      * @since VFC 1.0
334      */
335     public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
336         LOG.info("healVnf request body :" + jsonObject);
337         JSONObject restJson = new JSONObject();
338         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
339
340         String action = jsonObject.getString("action");
341         JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
342         String vmId = affectedVm.getString("vmid");
343         String path = String.format(ParamConstants.HEAL_VNF, vmId);
344
345         JSONObject subJsonObject = new JSONObject();
346         subJsonObject.put("type", "hard");
347         subJsonObject.put("boot_mode", "");
348         if("vmReset".equals(action)) {
349             subJsonObject.put("action", "reset");
350         }
351         LOG.info("healVnf subJsonObject :" + subJsonObject);
352         JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
353                 Constant.CERTIFICATE);
354
355         int statusCode = healResult.getInt(Constant.RETCODE);
356         if(statusCode == Constant.HTTP_OK) {
357             LOG.info("healResult:{}", healResult);
358             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
359         } else {
360             LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
361         }
362
363         return restJson;
364     }
365
366     public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
367         LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
368
369         JSONObject restJson = new JSONObject();
370         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
371
372         JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
373                 Constant.GET, null, Constant.CERTIFICATE);
374
375         int statusCode = queryResult.getInt(Constant.RETCODE);
376         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
377             if((queryResult.get("data")) == null) {
378                 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
379                 return restJson;
380             }
381             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
382             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
383         } else {
384             LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);
385         }
386
387         return restJson;
388     }
389
390 }