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.apache.commons.lang3.StringUtils;
23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.ResultRequestUtil;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl.AdapterResourceManager;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import net.sf.json.JSONArray;
33 import net.sf.json.JSONException;
34 import net.sf.json.JSONObject;
37 * create or terminate VNF to M
41 * @version VFC 1.0 Aug 24, 2016
43 public class VnfMgrVnfm implements InterfaceVnfMgr {
45 private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
47 private static final int PARAM_ZERO = 0;
49 private static final int PARAM_ONE = 1;
52 public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
53 LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
54 JSONObject restJson = new JSONObject();
55 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
56 String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
58 int scaleType = getScaleType(vnfObject.getString("type"));
59 // build request json object
60 JSONObject paramJson = new JSONObject();
61 JSONObject scaleInfo = new JSONObject();
62 JSONArray vduList = new JSONArray();
63 JSONObject vdu = new JSONObject();
64 vdu.put("vdu_type", this.getVduType(vnfmObject, vnfInstanceId));
65 vdu.put("h_steps", vnfObject.get("numberOfSteps"));
67 scaleInfo.put("vnf_id", vnfInstanceId);
68 scaleInfo.put("scale_pattern", "without_plan");
69 scaleInfo.put("scale_type", PARAM_ZERO);
70 scaleInfo.put("scale_action", scaleType);
71 scaleInfo.put("scale_step", PARAM_ZERO);
72 scaleInfo.put("scale_step_value", PARAM_ONE);
73 scaleInfo.put("scale_group", vdu.getString("vdu_type"));
74 scaleInfo.put("vdu_list", vduList);
75 if(scaleType == PARAM_ZERO) {
77 JSONArray vmList = new JSONArray();
79 // JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
80 // vmList = additionalParam.getJSONArray("vm_list");
81 vmList = AdapterResourceManager.readScaleInVmIdFromJson().getJSONArray("vm_list");
82 } catch(JSONException e) {
83 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
85 if(null != vmList && !vmList.isEmpty()) {
86 scaleInfo.put("vm_list", vmList);
89 paramJson.put("scale_info", scaleInfo);
90 JSONObject queryResult =
91 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
92 LOG.info("SCALE execute result:" + queryResult.toString());
94 int statusCode = queryResult.getInt(Constant.RETCODE);
96 if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
97 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
98 // restJson.put("data",
99 // queryResult.getJSONObject("data").getJSONObject("scale_info"));
100 JSONObject appInfo = new JSONObject();
102 appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("scale_info");
103 } catch(JSONException e) {
104 LOG.error("function=scaleVnf, msg=csm return obj is not a json, e={}.", e);
106 JSONObject resultObj = new JSONObject();
107 // resultObj.put(Constant.JOBID, vnfInstanceId + "_" + Constant.PUT);
108 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.PUT);
109 restJson.put("data", resultObj);
111 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
114 } catch(JSONException e) {
115 LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
121 private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
124 JSONObject queryResult =
125 ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
126 Constant.GET, null, Constant.CERTIFICATE);
127 LOG.info("getVduType result=" + queryResult);
128 vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
129 } catch(Exception e) {
130 LOG.error("get vdu_type failed.", e);
132 LOG.info("vdu_type=" + vduType);
136 private int getScaleType(String type) {
137 if("SCALE_OUT".equalsIgnoreCase(type)) {
139 } else if("SCALE_IN".equalsIgnoreCase(type)) {
146 public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
147 LOG.info("function=createVnf, msg=enter to create a vnf");
148 LOG.info("createVnf csm request body :" + subJsonObject);
149 JSONObject restJson = new JSONObject();
150 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
151 String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
153 JSONObject queryResult =
154 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
155 LOG.info("createVnf csm response content:" + queryResult);
157 int statusCode = queryResult.getInt(Constant.RETCODE);
159 if(statusCode == Constant.HTTP_CREATED) {
160 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
161 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
162 JSONObject resultObj = new JSONObject();
163 // resultObj.put("vnfInstanceId", appInfo.getString("id"));
164 // resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
165 String vnfInstanceId = appInfo.getString("id");
166 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.POST);
167 restJson.put("data", resultObj);
169 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
172 } catch(JSONException e) {
173 LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
179 private void handleResponse(JSONObject result, JSONObject returnObj, String vnfInstanceId, String type) {
181 if(returnObj.containsKey("job_id")) {
182 jobId = returnObj.getString("job_id") + ":job";
184 jobId = vnfInstanceId + "_" + type + ":no";
186 result.put("vnfInstanceId", vnfInstanceId);
187 result.put(Constant.JOBID, jobId);
191 public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
192 LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
193 JSONObject restJson = new JSONObject();
194 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
196 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
197 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
198 Constant.CERTIFICATE);
200 int statusCode = queryResult.getInt(Constant.RETCODE);
202 if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
203 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
204 // restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
205 JSONObject appInfo = new JSONObject();
206 if(queryResult.containsKey("data") && StringUtils.isNotEmpty(queryResult.getString("data")))
208 appInfo = JSONObject.fromObject(queryResult.getString("data"));
210 JSONObject resultObj = new JSONObject();
211 // resultObj.put(Constant.JOBID, vnfId + "_" + Constant.DELETE);
212 handleResponse(resultObj, appInfo, vnfId, Constant.DELETE);
213 restJson.put("data", resultObj);
215 LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
222 public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
223 LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
224 JSONObject restJson = new JSONObject();
225 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
227 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
228 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
230 int statusCode = queryResult.getInt(Constant.RETCODE);
232 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
233 if(null == (queryResult.get("data"))) {
234 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
237 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
238 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
240 LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
246 public JSONObject getIp(JSONObject vnfmObject, String vnfId) throws IOException {
247 LOG.warn("function=getIp, msg=enter to getIp: {}", vnfId);
248 JSONObject restJson = new JSONObject();
249 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
251 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
252 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
254 int statusCode = queryResult.getInt(Constant.RETCODE);
256 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
257 if(null == (queryResult.get("data"))) {
258 LOG.warn("function=getIp, msg=query is null {}", queryResult.get("data"));
261 JSONObject config = JSONObject.fromObject(queryResult.getString("data"));
262 LOG.info("function=getIp, query configuration result: {}", config);
263 JSONObject vnfInfo = config.getJSONArray("configuration").getJSONObject(0);
264 JSONObject result = new JSONObject();
265 result.put("vnf_id", vnfInfo.getString("vnf_id"));
266 result.put("vnf_type", vnfInfo.getString("vnf_type"));
267 JSONArray inputs = vnfInfo.getJSONArray("inputs");
269 ClassLoader classLoader = getClass().getClassLoader();
270 String ipConfig = IOUtils.toString(classLoader.getResourceAsStream("ipConfig.json"));
271 LOG.info("ipConfig: {}", ipConfig);
272 JSONObject ipCon = JSONObject.fromObject(ipConfig);
273 String vnfType = vnfInfo.getString("vnf_type");
274 if(ipCon.containsKey(vnfType)) {
275 String ipKey = ipCon.getString(vnfInfo.getString("vnf_type"));
276 LOG.info("ipKey: {}", ipKey);
278 for(int i = 0; i < inputs.size(); i++) {
279 JSONObject obj = inputs.getJSONObject(i);
280 if(obj.getString("key_name").equals(ipKey)) {
281 ip = obj.getString("value");
285 result.put("ip", ip);
286 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
287 restJson.put("data", result);
291 LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
298 public JSONObject getJob(JSONObject vnfmObject, String jobId) {
299 LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
300 JSONObject restJson = new JSONObject();
301 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
303 String vnfId = jobId.split("_")[0];
304 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
305 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
306 null, Constant.CERTIFICATE);
308 int statusCode = queryResult.getInt(Constant.RETCODE);
310 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
312 if((queryResult.get("data")) == null) {
313 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
316 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
317 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
319 LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
331 * @param vnfInstanceId
335 public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
336 LOG.info("healVnf request body :" + jsonObject);
337 JSONObject restJson = new JSONObject();
338 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
340 String action = jsonObject.getString("action");
341 JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
342 String vmId = affectedVm.getString("vmid");
343 String path = String.format(ParamConstants.HEAL_VNF, vmId);
345 JSONObject subJsonObject = new JSONObject();
346 subJsonObject.put("type", "hard");
347 subJsonObject.put("boot_mode", "");
348 if("vmReset".equals(action)) {
349 subJsonObject.put("action", "reset");
351 LOG.info("healVnf subJsonObject :" + subJsonObject);
352 JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
353 Constant.CERTIFICATE);
355 int statusCode = healResult.getInt(Constant.RETCODE);
356 if(statusCode == Constant.HTTP_OK) {
357 LOG.info("healResult:{}", healResult);
358 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
360 LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
366 public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
367 LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
369 JSONObject restJson = new JSONObject();
370 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
372 JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
373 Constant.GET, null, Constant.CERTIFICATE);
375 int statusCode = queryResult.getInt(Constant.RETCODE);
376 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
377 if((queryResult.get("data")) == null) {
378 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
381 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
382 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
384 LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);