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