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 net.sf.json.JSONArray;
21 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.ResultRequestUtil;
22 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import net.sf.json.JSONException;
29 import net.sf.json.JSONObject;
31 import javax.print.attribute.standard.ReferenceUriSchemesSupported;
34 * create or terminate VNF to M
38 * @version NFVO 0.5 Aug 24, 2016
40 public class VnfMgrVnfm implements InterfaceVnfMgr {
42 private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
45 public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
46 LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
47 JSONObject restJson = new JSONObject();
48 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
49 String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
51 int scaleType = getScaleType(vnfObject.getString("type"));
52 //build request json object
53 JSONObject paramJson = new JSONObject();
54 JSONObject scaleInfo = new JSONObject();
55 JSONArray vduList = new JSONArray();
56 JSONObject vdu = new JSONObject();
57 vdu.put("vdu_type",this.getVduType(vnfmObject,vnfInstanceId));//TODO:set vdu_type
58 vdu.put("h_steps",vnfObject.get("numberOfSteps"));
60 scaleInfo.put("vnf_id",vnfInstanceId);
61 scaleInfo.put("scale_type",0);
62 scaleInfo.put("scale_action",scaleType);
63 scaleInfo.put("vdu_list",vduList);
64 if(scaleType == 0){//scale_in
65 JSONArray vmList = new JSONArray();
67 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
68 vmList = additionalParam.getJSONArray("vm_list");
69 }catch (JSONException e) {
70 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it",e);
72 scaleInfo.put("vm_list",vmList);
74 paramJson.put("scale_info",scaleInfo);
75 JSONObject queryResult = ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(),Constant.CERTIFICATE);
76 LOG.info("SCALE execute result:"+queryResult.toString());
78 int statusCode = queryResult.getInt(Constant.RETCODE);
80 if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
81 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
82 JSONObject resultObj = new JSONObject();
83 resultObj.put("jobId", vnfInstanceId + "_" + Constant.PUT);
84 restJson.put("data", resultObj);
86 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
89 } catch(JSONException e) {
90 LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
97 private String getVduType(JSONObject vnfmObject, String vnfInstanceId){
100 JSONObject queryResult = ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId), 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);
106 LOG.info("vdu_type="+vduType);
109 private int getScaleType(String type){
110 if("SCALE_OUT".equalsIgnoreCase(type)){
112 }else if("SCALE_IN".equalsIgnoreCase(type)){
118 public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
119 LOG.info("function=createVnf, msg=enter to create a vnf");
120 LOG.info("createVnf csm request body :"+subJsonObject);
121 JSONObject restJson = new JSONObject();
122 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
123 String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
125 JSONObject queryResult = ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(),Constant.CERTIFICATE);
126 LOG.info("createVnf csm response content:"+queryResult);
128 int statusCode = queryResult.getInt(Constant.RETCODE);
130 if(statusCode == Constant.HTTP_CREATED) {
131 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
132 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
133 JSONObject resultObj = new JSONObject();
134 resultObj.put("vnfInstanceId", appInfo.getString("id"));
135 resultObj.put("jobId", appInfo.getString("id") + "_" + Constant.POST);
136 restJson.put("data", resultObj);
138 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
141 } catch(JSONException e) {
142 LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
149 public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
150 LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
151 JSONObject restJson = new JSONObject();
152 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
154 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
155 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,Constant.CERTIFICATE);
157 int statusCode = queryResult.getInt(Constant.RETCODE);
159 if(statusCode == Constant.HTTP_NOCONTENT) {
160 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
161 JSONObject resultObj = new JSONObject();
162 resultObj.put("jobId", vnfId + "_" + Constant.DELETE);
163 restJson.put("data", resultObj);
165 LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
172 public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
173 LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
174 JSONObject restJson = new JSONObject();
175 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
177 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
178 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
179 null,Constant.CERTIFICATE);
181 int statusCode = queryResult.getInt("retCode");
183 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
184 if(null == (queryResult.get("data"))) {
185 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
188 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
189 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
191 LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
198 public JSONObject getJob(JSONObject vnfmObject, String jobId) {
199 LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
200 JSONObject restJson = new JSONObject();
201 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
203 String vnfId = jobId.split("_")[0];
204 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
205 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
206 null,Constant.CERTIFICATE);
208 int statusCode = queryResult.getInt("retCode");
210 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
212 if((queryResult.get("data")) == null) {
213 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
216 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
217 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
219 LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);