update driver scaling process
[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 =
101                 // queryResult.getJSONObject("data").getJSONObject("scale_info");
102                 JSONObject appInfo = new JSONObject();
103                 JSONObject resultObj = new JSONObject();
104                 // resultObj.put(Constant.JOBID, vnfInstanceId + "_" + Constant.PUT);
105                 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.PUT);
106                 restJson.put("data", resultObj);
107             } else {
108                 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
109             }
110
111         } catch(JSONException e) {
112             LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
113         }
114
115         return restJson;
116     }
117
118     private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
119         String vduType = "";
120         try {
121             JSONObject queryResult =
122                     ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
123                             Constant.GET, null, Constant.CERTIFICATE);
124             LOG.info("getVduType result=" + queryResult);
125             vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
126         } catch(Exception e) {
127             LOG.error("get vdu_type failed.", e);
128         }
129         LOG.info("vdu_type=" + vduType);
130         return vduType;
131     }
132
133     private int getScaleType(String type) {
134         if("SCALE_OUT".equalsIgnoreCase(type)) {
135             return 1;
136         } else if("SCALE_IN".equalsIgnoreCase(type)) {
137             return 0;
138         }
139         return -1;
140     }
141
142     @Override
143     public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
144         LOG.info("function=createVnf, msg=enter to create a vnf");
145         LOG.info("createVnf csm request body :" + subJsonObject);
146         JSONObject restJson = new JSONObject();
147         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
148         String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
149
150         JSONObject queryResult =
151                 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
152         LOG.info("createVnf csm response content:" + queryResult);
153         try {
154             int statusCode = queryResult.getInt(Constant.RETCODE);
155
156             if(statusCode == Constant.HTTP_CREATED) {
157                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
158                 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
159                 JSONObject resultObj = new JSONObject();
160                 // resultObj.put("vnfInstanceId", appInfo.getString("id"));
161                 // resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
162                 String vnfInstanceId = appInfo.getString("id");
163                 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.POST);
164                 restJson.put("data", resultObj);
165             } else {
166                 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
167             }
168
169         } catch(JSONException e) {
170             LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
171         }
172
173         return restJson;
174     }
175
176     private void handleResponse(JSONObject result, JSONObject returnObj, String vnfInstanceId, String type) {
177         String jobId = "";
178         if(returnObj.containsKey("job_id")) {
179             jobId = returnObj.getString("job_id") + ":job";
180         } else {
181             jobId = vnfInstanceId + "_" + type + ":no";
182         }
183         result.put("vnfInstanceId", vnfInstanceId);
184         result.put(Constant.JOBID, jobId);
185     }
186
187     @Override
188     public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
189         LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
190         JSONObject restJson = new JSONObject();
191         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
192
193         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
194                 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
195                 Constant.CERTIFICATE);
196
197         int statusCode = queryResult.getInt(Constant.RETCODE);
198
199         if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
200             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
201             // restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
202             JSONObject appInfo = new JSONObject();
203             if(queryResult.containsKey("data") && StringUtils.isNotEmpty(queryResult.getString("data")))
204             {
205                 appInfo = JSONObject.fromObject(queryResult.getString("data"));
206             }
207             JSONObject resultObj = new JSONObject();
208             // resultObj.put(Constant.JOBID, vnfId + "_" + Constant.DELETE);
209             handleResponse(resultObj, appInfo, vnfId, Constant.DELETE);
210             restJson.put("data", resultObj);
211         } else {
212             LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
213         }
214
215         return restJson;
216     }
217
218     @Override
219     public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
220         LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
221         JSONObject restJson = new JSONObject();
222         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
223
224         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
225                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
226
227         int statusCode = queryResult.getInt(Constant.RETCODE);
228
229         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
230             if(null == (queryResult.get("data"))) {
231                 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
232                 return restJson;
233             }
234             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
235             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
236         } else {
237             LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
238         }
239
240         return restJson;
241     }
242
243     public JSONObject getIp(JSONObject vnfmObject, String vnfId) throws IOException {
244         LOG.warn("function=getIp, msg=enter to getIp: {}", vnfId);
245         JSONObject restJson = new JSONObject();
246         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
247
248         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
249                 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
250
251         int statusCode = queryResult.getInt(Constant.RETCODE);
252
253         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
254             if(null == (queryResult.get("data"))) {
255                 LOG.warn("function=getIp, msg=query is null {}", queryResult.get("data"));
256                 return restJson;
257             }
258             JSONObject config = JSONObject.fromObject(queryResult.getString("data"));
259             LOG.info("function=getIp, query configuration result: {}", config);
260             JSONObject vnfInfo = config.getJSONArray("configuration").getJSONObject(0);
261             JSONObject result = new JSONObject();
262             result.put("vnf_id", vnfInfo.getString("vnf_id"));
263             result.put("vnf_type", vnfInfo.getString("vnf_type"));
264             JSONArray inputs = vnfInfo.getJSONArray("inputs");
265
266             ClassLoader classLoader = getClass().getClassLoader();
267             String ipConfig = IOUtils.toString(classLoader.getResourceAsStream("ipConfig.json"));
268             LOG.info("ipConfig: {}", ipConfig);
269             JSONObject ipCon = JSONObject.fromObject(ipConfig);
270             String vnfType = vnfInfo.getString("vnf_type");
271             if(ipCon.containsKey(vnfType)) {
272                 String ipKey = ipCon.getString(vnfInfo.getString("vnf_type"));
273                 LOG.info("ipKey: {}", ipKey);
274                 String ip = "";
275                 for(int i = 0; i < inputs.size(); i++) {
276                     JSONObject obj = inputs.getJSONObject(i);
277                     if(obj.getString("key_name").equals(ipKey)) {
278                         ip = obj.getString("value");
279                         break;
280                     }
281                 }
282                 result.put("ip", ip);
283                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
284                 restJson.put("data", result);
285             }
286
287         } else {
288             LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
289         }
290
291         return restJson;
292     }
293
294     @Override
295     public JSONObject getJob(JSONObject vnfmObject, String jobId) {
296         LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
297         JSONObject restJson = new JSONObject();
298         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
299
300         String vnfId = jobId.split("_")[0];
301         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
302                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
303                 null, Constant.CERTIFICATE);
304
305         int statusCode = queryResult.getInt(Constant.RETCODE);
306
307         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
308
309             if((queryResult.get("data")) == null) {
310                 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
311                 return restJson;
312             }
313             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
314             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
315         } else {
316             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
317         }
318
319         return restJson;
320     }
321
322     /**
323      * <br>
324      * 
325      * @param jsonObject
326      * @param vnfmObjcet
327      * @param vnfmId
328      * @param vnfInstanceId
329      * @return
330      * @since VFC 1.0
331      */
332     public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
333         LOG.info("healVnf request body :" + jsonObject);
334         JSONObject restJson = new JSONObject();
335         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
336
337         String action = jsonObject.getString("action");
338         JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
339         String vmId = affectedVm.getString("vmid");
340         String path = String.format(ParamConstants.HEAL_VNF, vmId);
341
342         JSONObject subJsonObject = new JSONObject();
343         subJsonObject.put("type", "hard");
344         subJsonObject.put("boot_mode", "");
345         if("vmReset".equals(action)) {
346             subJsonObject.put("action", "reset");
347         }
348         LOG.info("healVnf subJsonObject :" + subJsonObject);
349         JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
350                 Constant.CERTIFICATE);
351
352         int statusCode = healResult.getInt(Constant.RETCODE);
353         if(statusCode == Constant.HTTP_OK) {
354             LOG.info("healResult:{}", healResult);
355             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
356         } else {
357             LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
358         }
359
360         return restJson;
361     }
362
363     public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
364         LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
365
366         JSONObject restJson = new JSONObject();
367         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
368
369         JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
370                 Constant.GET, null, Constant.CERTIFICATE);
371
372         int statusCode = queryResult.getInt(Constant.RETCODE);
373         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
374             if((queryResult.get("data")) == null) {
375                 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
376                 return restJson;
377             }
378             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
379             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
380         } else {
381             LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);
382         }
383
384         return restJson;
385     }
386
387 }