Add ip Info in query VNF result
[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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.ResultRequestUtil;
23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
33
34 /**
35  * create or terminate VNF to M
36  * <br/>
37  *
38  * @author
39  * @version VFC 1.0 Aug 24, 2016
40  */
41 public class VnfMgrVnfm implements InterfaceVnfMgr {
42
43     private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
44
45     private static final int PARAM_ZERO = 0;
46
47     private static final int PARAM_ONE = 1;
48
49     @Override
50     public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
51         LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
52         JSONObject restJson = new JSONObject();
53         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
54         String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
55
56         int scaleType = getScaleType(vnfObject.getString("type"));
57         // build request json object
58         JSONObject paramJson = new JSONObject();
59         JSONObject scaleInfo = new JSONObject();
60         JSONArray vduList = new JSONArray();
61         JSONObject vdu = new JSONObject();
62         vdu.put("vdu_type", this.getVduType(vnfmObject, vnfInstanceId));
63         vdu.put("h_steps", vnfObject.get("numberOfSteps"));
64         vduList.add(vdu);
65         scaleInfo.put("vnf_id", vnfInstanceId);
66         scaleInfo.put("scale_pattern", "without_plan");
67         scaleInfo.put("scale_type", PARAM_ZERO);
68         scaleInfo.put("scale_action", scaleType);
69         scaleInfo.put("scale_step", PARAM_ZERO);
70         scaleInfo.put("scale_step_value", PARAM_ONE);
71         scaleInfo.put("scale_group", vdu.getString("vdu_type"));
72         scaleInfo.put("vdu_list", vduList);
73         if(scaleType == PARAM_ZERO) {
74             // scale_in
75             JSONArray vmList = new JSONArray();
76             try {
77                 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
78                 vmList = additionalParam.getJSONArray("vm_list");
79             } catch(JSONException e) {
80                 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
81             }
82             scaleInfo.put("vm_list", vmList);
83         }
84         paramJson.put("scale_info", scaleInfo);
85         JSONObject queryResult =
86                 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
87         LOG.info("SCALE execute result:" + queryResult.toString());
88         try {
89             int statusCode = queryResult.getInt(Constant.RETCODE);
90
91             if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
92                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
93                 restJson.put("data", queryResult.getJSONObject("data").getJSONObject("scale_info"));
94             } else {
95                 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
96             }
97
98         } catch(JSONException e) {
99             LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
100         }
101
102         return restJson;
103     }
104
105     private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
106         String vduType = "";
107         try {
108             JSONObject queryResult =
109                     ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
110                             Constant.GET, null, Constant.CERTIFICATE);
111             LOG.info("getVduType result=" + queryResult);
112             vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
113         } catch(Exception e) {
114             LOG.error("get vdu_type failed.", e);
115         }
116         LOG.info("vdu_type=" + vduType);
117         return vduType;
118     }
119
120     private int getScaleType(String type) {
121         if("SCALE_OUT".equalsIgnoreCase(type)) {
122             return 1;
123         } else if("SCALE_IN".equalsIgnoreCase(type)) {
124             return 0;
125         }
126         return -1;
127     }
128
129     @Override
130     public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
131         LOG.info("function=createVnf, msg=enter to create a vnf");
132         LOG.info("createVnf csm request body :" + subJsonObject);
133         JSONObject restJson = new JSONObject();
134         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
135         String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
136
137         JSONObject queryResult =
138                 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
139         LOG.info("createVnf csm response content:" + queryResult);
140         try {
141             int statusCode = queryResult.getInt(Constant.RETCODE);
142
143             if(statusCode == Constant.HTTP_CREATED) {
144                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
145                 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
146                 JSONObject resultObj = new JSONObject();
147                 resultObj.put("vnfInstanceId", appInfo.getString("id"));
148                 resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
149                 restJson.put("data", resultObj);
150             } else {
151                 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
152             }
153
154         } catch(JSONException e) {
155             LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
156         }
157
158         return restJson;
159     }
160
161     @Override
162     public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
163         LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
164         JSONObject restJson = new JSONObject();
165         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
166
167         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
168                 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
169                 Constant.CERTIFICATE);
170
171         int statusCode = queryResult.getInt(Constant.RETCODE);
172
173         if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
174             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
175             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
176         } else {
177             LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
178         }
179
180         return restJson;
181     }
182
183     @Override
184     public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
185         LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
186         JSONObject restJson = new JSONObject();
187         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
188
189         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
190                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
191
192         int statusCode = queryResult.getInt(Constant.RETCODE);
193
194         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
195             if(null == (queryResult.get("data"))) {
196                 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
197                 return restJson;
198             }
199             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
200             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
201         } else {
202             LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
203         }
204
205         return restJson;
206     }
207
208     public JSONObject getIp(JSONObject vnfmObject, String vnfId) throws IOException {
209         LOG.warn("function=getIp, msg=enter to getIp: {}", vnfId);
210         JSONObject restJson = new JSONObject();
211         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
212
213         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
214                 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
215
216         int statusCode = queryResult.getInt(Constant.RETCODE);
217
218         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
219             if(null == (queryResult.get("data"))) {
220                 LOG.warn("function=getIp, msg=query is null {}", queryResult.get("data"));
221                 return restJson;
222             }
223             JSONObject config = JSONObject.fromObject(queryResult.getString("data"));
224             LOG.info("function=getIp, query configuration result: {}", config);
225             JSONObject vnfInfo = config.getJSONArray("configuration").getJSONObject(0);
226             JSONObject result = new JSONObject();
227             result.put("vnf_id", vnfInfo.getString("vnf_id"));
228             result.put("vnf_type", vnfInfo.getString("vnf_type"));
229             JSONArray inputs = vnfInfo.getJSONArray("inputs");
230
231             ClassLoader classLoader = getClass().getClassLoader();
232             String ipConfig = IOUtils.toString(classLoader.getResourceAsStream("ipConfig.json"));
233             LOG.info("ipConfig: {}", ipConfig);
234             JSONObject ipCon = JSONObject.fromObject(ipConfig);
235             String vnfType = vnfInfo.getString("vnf_type");
236             if(ipCon.containsKey(vnfType)) {
237                 String ipKey = ipCon.getString(vnfInfo.getString("vnf_type"));
238                 LOG.info("ipKey: {}", ipKey);
239                 String ip = "";
240                 for(int i = 0; i < inputs.size(); i++) {
241                     JSONObject obj = inputs.getJSONObject(i);
242                     if(obj.getString("key_name").equals(ipKey)) {
243                         ip = obj.getString("value");
244                         break;
245                     }
246                 }
247                 result.put("ip", ip);
248                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
249                 restJson.put("data", result);
250             }
251
252         } else {
253             LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
254         }
255
256         return restJson;
257     }
258
259     @Override
260     public JSONObject getJob(JSONObject vnfmObject, String jobId) {
261         LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
262         JSONObject restJson = new JSONObject();
263         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
264
265         String vnfId = jobId.split("_")[0];
266         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
267                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
268                 null, Constant.CERTIFICATE);
269
270         int statusCode = queryResult.getInt(Constant.RETCODE);
271
272         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
273
274             if((queryResult.get("data")) == null) {
275                 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
276                 return restJson;
277             }
278             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
279             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
280         } else {
281             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
282         }
283
284         return restJson;
285     }
286
287     /**
288      * <br>
289      * 
290      * @param jsonObject
291      * @param vnfmObjcet
292      * @param vnfmId
293      * @param vnfInstanceId
294      * @return
295      * @since VFC 1.0
296      */
297     public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
298         LOG.info("healVnf request body :" + jsonObject);
299         JSONObject restJson = new JSONObject();
300         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
301
302         String action = jsonObject.getString("action");
303         JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
304         String vmId = affectedVm.getString("vmid");
305         String path = String.format(ParamConstants.HEAL_VNF, vmId);
306
307         JSONObject subJsonObject = new JSONObject();
308         subJsonObject.put("type", "hard");
309         subJsonObject.put("boot_mode", "");
310         if("vmReset".equals(action)) {
311             subJsonObject.put("action", "reset");
312         }
313         LOG.info("healVnf subJsonObject :" + subJsonObject);
314         JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
315                 Constant.CERTIFICATE);
316
317         int statusCode = healResult.getInt(Constant.RETCODE);
318         if(statusCode == Constant.HTTP_OK) {
319             LOG.info("healResult:{}", healResult);
320             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
321         } else {
322             LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
323         }
324
325         return restJson;
326     }
327
328     public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
329         LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
330         JSONObject restJson = new JSONObject();
331         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
332
333         JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
334                 Constant.GET, null, Constant.CERTIFICATE);
335
336         int statusCode = queryResult.getInt(Constant.RETCODE);
337         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
338             if((queryResult.get("data")) == null) {
339                 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
340                 return restJson;
341             }
342             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
343             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
344         } else {
345             LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);
346         }
347
348         return restJson;
349     }
350 }