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 java.io.IOException;
21 import org.apache.commons.io.IOUtils;
22 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.ResultRequestUtil;
23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
35 * create or terminate VNF to M
39 * @version VFC 1.0 Aug 24, 2016
41 public class VnfMgrVnfm implements InterfaceVnfMgr {
43 private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
45 private static final int PARAM_ZERO = 0;
47 private static final int PARAM_ONE = 1;
50 public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
51 LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
52 JSONObject restJson = new JSONObject();
53 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
54 String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
56 int scaleType = getScaleType(vnfObject.getString("type"));
57 // build request json object
58 JSONObject paramJson = new JSONObject();
59 JSONObject scaleInfo = new JSONObject();
60 JSONArray vduList = new JSONArray();
61 JSONObject vdu = new JSONObject();
62 vdu.put("vdu_type", this.getVduType(vnfmObject, vnfInstanceId));
63 vdu.put("h_steps", vnfObject.get("numberOfSteps"));
65 scaleInfo.put("vnf_id", vnfInstanceId);
66 scaleInfo.put("scale_pattern", "without_plan");
67 scaleInfo.put("scale_type", PARAM_ZERO);
68 scaleInfo.put("scale_action", scaleType);
69 scaleInfo.put("scale_step", PARAM_ZERO);
70 scaleInfo.put("scale_step_value", PARAM_ONE);
71 scaleInfo.put("scale_group", vdu.getString("vdu_type"));
72 scaleInfo.put("vdu_list", vduList);
73 if(scaleType == PARAM_ZERO) {
75 JSONArray vmList = new JSONArray();
77 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
78 vmList = additionalParam.getJSONArray("vm_list");
79 } catch(JSONException e) {
80 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
82 scaleInfo.put("vm_list", vmList);
84 paramJson.put("scale_info", scaleInfo);
85 JSONObject queryResult =
86 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
87 LOG.info("SCALE execute result:" + queryResult.toString());
89 int statusCode = queryResult.getInt(Constant.RETCODE);
91 if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
92 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
93 restJson.put("data", queryResult.getJSONObject("data").getJSONObject("scale_info"));
95 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
98 } catch(JSONException e) {
99 LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
105 private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
108 JSONObject queryResult =
109 ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
110 Constant.GET, null, Constant.CERTIFICATE);
111 LOG.info("getVduType result=" + queryResult);
112 vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
113 } catch(Exception e) {
114 LOG.error("get vdu_type failed.", e);
116 LOG.info("vdu_type=" + vduType);
120 private int getScaleType(String type) {
121 if("SCALE_OUT".equalsIgnoreCase(type)) {
123 } else if("SCALE_IN".equalsIgnoreCase(type)) {
130 public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
131 LOG.info("function=createVnf, msg=enter to create a vnf");
132 LOG.info("createVnf csm request body :" + subJsonObject);
133 JSONObject restJson = new JSONObject();
134 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
135 String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
137 JSONObject queryResult =
138 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
139 LOG.info("createVnf csm response content:" + queryResult);
141 int statusCode = queryResult.getInt(Constant.RETCODE);
143 if(statusCode == Constant.HTTP_CREATED) {
144 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
145 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
146 JSONObject resultObj = new JSONObject();
147 resultObj.put("vnfInstanceId", appInfo.getString("id"));
148 resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
149 restJson.put("data", resultObj);
151 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
154 } catch(JSONException e) {
155 LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
162 public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
163 LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
164 JSONObject restJson = new JSONObject();
165 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
167 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
168 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
169 Constant.CERTIFICATE);
171 int statusCode = queryResult.getInt(Constant.RETCODE);
173 if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
174 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
175 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
177 LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
184 public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
185 LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
186 JSONObject restJson = new JSONObject();
187 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
189 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
190 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
192 int statusCode = queryResult.getInt(Constant.RETCODE);
194 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
195 if(null == (queryResult.get("data"))) {
196 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
199 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
200 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
202 LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
208 public JSONObject getIp(JSONObject vnfmObject, String vnfId) throws IOException {
209 LOG.warn("function=getIp, msg=enter to getIp: {}", vnfId);
210 JSONObject restJson = new JSONObject();
211 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
213 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
214 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
216 int statusCode = queryResult.getInt(Constant.RETCODE);
218 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
219 if(null == (queryResult.get("data"))) {
220 LOG.warn("function=getIp, msg=query is null {}", queryResult.get("data"));
223 JSONObject config = JSONObject.fromObject(queryResult.getString("data"));
224 LOG.info("function=getIp, query configuration result: {}", config);
225 JSONObject vnfInfo = config.getJSONArray("configuration").getJSONObject(0);
226 JSONObject result = new JSONObject();
227 result.put("vnf_id", vnfInfo.getString("vnf_id"));
228 result.put("vnf_type", vnfInfo.getString("vnf_type"));
229 JSONArray inputs = vnfInfo.getJSONArray("inputs");
231 ClassLoader classLoader = getClass().getClassLoader();
232 String ipConfig = IOUtils.toString(classLoader.getResourceAsStream("ipConfig.json"));
233 LOG.info("ipConfig: {}", ipConfig);
234 JSONObject ipCon = JSONObject.fromObject(ipConfig);
235 String vnfType = vnfInfo.getString("vnf_type");
236 if(ipCon.containsKey(vnfType)) {
237 String ipKey = ipCon.getString(vnfInfo.getString("vnf_type"));
238 LOG.info("ipKey: {}", ipKey);
240 for(int i = 0; i < inputs.size(); i++) {
241 JSONObject obj = inputs.getJSONObject(i);
242 if(obj.getString("key_name").equals(ipKey)) {
243 ip = obj.getString("value");
247 result.put("ip", ip);
248 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
249 restJson.put("data", result);
253 LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
260 public JSONObject getJob(JSONObject vnfmObject, String jobId) {
261 LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
262 JSONObject restJson = new JSONObject();
263 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
265 String vnfId = jobId.split("_")[0];
266 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
267 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
268 null, Constant.CERTIFICATE);
270 int statusCode = queryResult.getInt(Constant.RETCODE);
272 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
274 if((queryResult.get("data")) == null) {
275 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
278 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
279 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
281 LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
293 * @param vnfInstanceId
297 public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
298 LOG.info("healVnf request body :" + jsonObject);
299 JSONObject restJson = new JSONObject();
300 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
302 String action = jsonObject.getString("action");
303 JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
304 String vmId = affectedVm.getString("vmid");
305 String path = String.format(ParamConstants.HEAL_VNF, vmId);
307 JSONObject subJsonObject = new JSONObject();
308 subJsonObject.put("type", "hard");
309 subJsonObject.put("boot_mode", "");
310 if("vmReset".equals(action)) {
311 subJsonObject.put("action", "reset");
313 LOG.info("healVnf subJsonObject :" + subJsonObject);
314 JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
315 Constant.CERTIFICATE);
317 int statusCode = healResult.getInt(Constant.RETCODE);
318 if(statusCode == Constant.HTTP_OK) {
319 LOG.info("healResult:{}", healResult);
320 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
322 LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
328 public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
329 LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
330 JSONObject restJson = new JSONObject();
331 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
333 JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
334 Constant.GET, null, Constant.CERTIFICATE);
336 int statusCode = queryResult.getInt(Constant.RETCODE);
337 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
338 if((queryResult.get("data")) == null) {
339 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
342 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
343 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
345 LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);