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.openo.nfvo.vnfmadapter.service.csm.vnf;
19 import net.sf.json.JSONArray;
20 import org.openo.nfvo.vnfmadapter.common.ResultRequestUtil;
21 import org.openo.nfvo.vnfmadapter.service.constant.Constant;
22 import org.openo.nfvo.vnfmadapter.service.constant.ParamConstants;
23 import org.openo.nfvo.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import net.sf.json.JSONException;
28 import net.sf.json.JSONObject;
30 import javax.print.attribute.standard.ReferenceUriSchemesSupported;
33 * create or terminate VNF to M
37 * @version NFVO 0.5 Aug 24, 2016
39 public class VnfMgrVnfm implements InterfaceVnfMgr {
41 private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
44 public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
45 LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
46 JSONObject restJson = new JSONObject();
47 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
48 String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
50 int scaleType = getScaleType(vnfObject.getString("type"));
51 //build request json object
52 JSONObject paramJson = new JSONObject();
53 JSONObject scaleInfo = new JSONObject();
54 JSONArray vduList = new JSONArray();
55 JSONObject vdu = new JSONObject();
56 vdu.put("vdu_type",this.getVduType(vnfmObject,vnfInstanceId));//TODO:set vdu_type
57 vdu.put("h_steps",vnfObject.get("numberOfSteps"));
59 scaleInfo.put("vnf_id",vnfInstanceId);
60 scaleInfo.put("scale_type",0);
61 scaleInfo.put("scale_action",scaleType);
62 scaleInfo.put("vdu_list",vduList);
63 if(scaleType == 0){//scale_in
64 JSONArray vmList = new JSONArray();
66 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
67 vmList = additionalParam.getJSONArray("vm_list");
68 }catch (JSONException e) {
69 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it",e);
71 scaleInfo.put("vm_list",vmList);
73 paramJson.put("scale_info",scaleInfo);
74 JSONObject queryResult = ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(),Constant.CERTIFICATE);
75 LOG.info("SCALE execute result:"+queryResult.toString());
77 int statusCode = queryResult.getInt(Constant.RETCODE);
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("jobId", vnfInstanceId + "_" + Constant.PUT);
83 restJson.put("data", resultObj);
85 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
88 } catch(JSONException e) {
89 LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
96 private String getVduType(JSONObject vnfmObject, String vnfInstanceId){
99 JSONObject queryResult = ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId), 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);
108 private int getScaleType(String type){
109 if("SCALE_OUT".equalsIgnoreCase(type)){
111 }else if("SCALE_IN".equalsIgnoreCase(type)){
117 public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
118 LOG.info("function=createVnf, msg=enter to create a vnf");
119 LOG.info("createVnf csm request body :"+subJsonObject);
120 JSONObject restJson = new JSONObject();
121 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
122 String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
124 JSONObject queryResult = ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(),Constant.CERTIFICATE);
125 LOG.info("createVnf csm response content:"+queryResult);
127 int statusCode = queryResult.getInt(Constant.RETCODE);
129 if(statusCode == Constant.HTTP_CREATED) {
130 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
131 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
132 JSONObject resultObj = new JSONObject();
133 resultObj.put("vnfInstanceId", appInfo.getString("id"));
134 resultObj.put("jobId", appInfo.getString("id") + "_" + Constant.POST);
135 restJson.put("data", resultObj);
137 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
140 } catch(JSONException e) {
141 LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
148 public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
149 LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
150 JSONObject restJson = new JSONObject();
151 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
153 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
154 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,Constant.CERTIFICATE);
156 int statusCode = queryResult.getInt(Constant.RETCODE);
158 if(statusCode == Constant.HTTP_NOCONTENT) {
159 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
160 JSONObject resultObj = new JSONObject();
161 resultObj.put("jobId", vnfId + "_" + Constant.DELETE);
162 restJson.put("data", resultObj);
164 LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
171 public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
172 LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
173 JSONObject restJson = new JSONObject();
174 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
176 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
177 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
178 null,Constant.CERTIFICATE);
180 int statusCode = queryResult.getInt("retCode");
182 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
183 if(null == (queryResult.get("data"))) {
184 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
187 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
188 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
190 LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
197 public JSONObject getJob(JSONObject vnfmObject, String jobId) {
198 LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
199 JSONObject restJson = new JSONObject();
200 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
202 String vnfId = jobId.split("_")[0];
203 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
204 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
205 null,Constant.CERTIFICATE);
207 int statusCode = queryResult.getInt("retCode");
209 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
211 if((queryResult.get("data")) == null) {
212 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
215 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
216 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
218 LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);