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