2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.vnf;
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.csm.inf.InterfaceVnfMgr;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import net.sf.json.JSONArray;
27 import net.sf.json.JSONException;
28 import net.sf.json.JSONObject;
31 * create or terminate VNF to M
35 * @version VFC 1.0 Aug 24, 2016
37 public class VnfMgrVnfm implements InterfaceVnfMgr {
39 private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
42 public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
43 LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
44 JSONObject restJson = new JSONObject();
45 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
46 String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
48 int scaleType = getScaleType(vnfObject.getString("type"));
49 // build request json object
50 JSONObject paramJson = new JSONObject();
51 JSONObject scaleInfo = new JSONObject();
52 JSONArray vduList = new JSONArray();
53 JSONObject vdu = new JSONObject();
54 vdu.put("vdu_type", this.getVduType(vnfmObject, vnfInstanceId));// TODO:set vdu_type
55 vdu.put("h_steps", vnfObject.get("numberOfSteps"));
57 scaleInfo.put("vnf_id", vnfInstanceId);
58 scaleInfo.put("scale_type", 0);
59 scaleInfo.put("scale_action", scaleType);
60 scaleInfo.put("vdu_list", vduList);
61 if(scaleType == 0) {// scale_in
62 JSONArray vmList = new JSONArray();
64 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
65 vmList = additionalParam.getJSONArray("vm_list");
66 } catch(JSONException e) {
67 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
69 scaleInfo.put("vm_list", vmList);
71 paramJson.put("scale_info", scaleInfo);
72 JSONObject queryResult =
73 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
74 LOG.info("SCALE execute result:" + queryResult.toString());
76 int statusCode = queryResult.getInt(Constant.RETCODE);
78 if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
79 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
80 JSONObject resultObj = new JSONObject();
81 resultObj.put("jobId", vnfInstanceId + "_" + Constant.PUT);
82 restJson.put("data", resultObj);
84 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
87 } catch(JSONException e) {
88 LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
94 private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
97 JSONObject queryResult =
98 ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
99 Constant.GET, null, Constant.CERTIFICATE);
100 LOG.info("getVduType result=" + queryResult);
101 vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
102 } catch(Exception e) {
103 LOG.error("get vdu_type failed.", e);
105 LOG.info("vdu_type=" + vduType);
109 private int getScaleType(String type) {
110 if("SCALE_OUT".equalsIgnoreCase(type)) {
112 } else if("SCALE_IN".equalsIgnoreCase(type)) {
119 public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
120 LOG.info("function=createVnf, msg=enter to create a vnf");
121 LOG.info("createVnf csm request body :" + subJsonObject);
122 JSONObject restJson = new JSONObject();
123 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
124 String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
126 JSONObject queryResult =
127 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
128 LOG.info("createVnf csm response content:" + queryResult);
130 int statusCode = queryResult.getInt(Constant.RETCODE);
132 if(statusCode == Constant.HTTP_CREATED) {
133 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
134 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
135 JSONObject resultObj = new JSONObject();
136 resultObj.put("vnfInstanceId", appInfo.getString("id"));
137 resultObj.put("jobId", appInfo.getString("id") + "_" + Constant.POST);
138 restJson.put("data", resultObj);
140 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
143 } catch(JSONException e) {
144 LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
151 public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
152 LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
153 JSONObject restJson = new JSONObject();
154 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
156 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
157 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
158 Constant.CERTIFICATE);
160 int statusCode = queryResult.getInt(Constant.RETCODE);
162 if(statusCode == Constant.HTTP_NOCONTENT) {
163 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
164 JSONObject resultObj = new JSONObject();
165 resultObj.put("jobId", vnfId + "_" + Constant.DELETE);
166 restJson.put("data", resultObj);
168 LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
175 public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
176 LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
177 JSONObject restJson = new JSONObject();
178 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
180 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
181 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
182 null, Constant.CERTIFICATE);
184 int statusCode = queryResult.getInt("retCode");
186 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
187 if(null == (queryResult.get("data"))) {
188 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
191 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
192 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
194 LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
201 public JSONObject getJob(JSONObject vnfmObject, String jobId) {
202 LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
203 JSONObject restJson = new JSONObject();
204 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
206 String vnfId = jobId.split("_")[0];
207 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
208 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
209 null, Constant.CERTIFICATE);
211 int statusCode = queryResult.getInt("retCode");
213 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
215 if((queryResult.get("data")) == null) {
216 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
219 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
220 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
222 LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
234 * @param vnfInstanceId
238 public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
239 LOG.info("healVnf request body :" + jsonObject);
240 JSONObject restJson = new JSONObject();
241 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
243 String action = jsonObject.getString("action");
244 JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
245 String vmId = affectedVm.getString("vmid");
246 String path = String.format(ParamConstants.HEAL_VNF, vmId);
248 JSONObject subJsonObject = new JSONObject();
249 subJsonObject.put("type", "hard");
250 subJsonObject.put("boot_mode", "");
251 if("vmReset".equals(action)) {
252 subJsonObject.put("action", "reset");
254 LOG.info("healVnf subJsonObject :" + subJsonObject);
255 JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
256 Constant.CERTIFICATE);
258 int statusCode = healResult.getInt("retCode");
259 if(statusCode == Constant.HTTP_OK) {
261 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
263 LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);