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 =
101 // queryResult.getJSONObject("data").getJSONObject("scale_info");
102 JSONObject appInfo = new JSONObject();
103 JSONObject resultObj = new JSONObject();
104 // resultObj.put(Constant.JOBID, vnfInstanceId + "_" + Constant.PUT);
105 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.PUT);
106 restJson.put("data", resultObj);
108 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
111 } catch(JSONException e) {
112 LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
118 private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
121 JSONObject queryResult =
122 ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
123 Constant.GET, null, Constant.CERTIFICATE);
124 LOG.info("getVduType result=" + queryResult);
125 vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
126 } catch(Exception e) {
127 LOG.error("get vdu_type failed.", e);
129 LOG.info("vdu_type=" + vduType);
133 private int getScaleType(String type) {
134 if("SCALE_OUT".equalsIgnoreCase(type)) {
136 } else if("SCALE_IN".equalsIgnoreCase(type)) {
143 public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
144 LOG.info("function=createVnf, msg=enter to create a vnf");
145 LOG.info("createVnf csm request body :" + subJsonObject);
146 JSONObject restJson = new JSONObject();
147 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
148 String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
150 JSONObject queryResult =
151 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
152 LOG.info("createVnf csm response content:" + queryResult);
154 int statusCode = queryResult.getInt(Constant.RETCODE);
156 if(statusCode == Constant.HTTP_CREATED) {
157 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
158 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
159 JSONObject resultObj = new JSONObject();
160 // resultObj.put("vnfInstanceId", appInfo.getString("id"));
161 // resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
162 String vnfInstanceId = appInfo.getString("id");
163 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.POST);
164 restJson.put("data", resultObj);
166 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
169 } catch(JSONException e) {
170 LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
176 private void handleResponse(JSONObject result, JSONObject returnObj, String vnfInstanceId, String type) {
178 if(returnObj.containsKey("job_id")) {
179 jobId = returnObj.getString("job_id") + ":job";
181 jobId = vnfInstanceId + "_" + type + ":no";
183 result.put("vnfInstanceId", vnfInstanceId);
184 result.put(Constant.JOBID, jobId);
188 public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
189 LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
190 JSONObject restJson = new JSONObject();
191 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
193 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
194 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
195 Constant.CERTIFICATE);
197 int statusCode = queryResult.getInt(Constant.RETCODE);
199 if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
200 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
201 // restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
202 JSONObject appInfo = new JSONObject();
203 if(queryResult.containsKey("data") && StringUtils.isNotEmpty(queryResult.getString("data")))
205 appInfo = JSONObject.fromObject(queryResult.getString("data"));
207 JSONObject resultObj = new JSONObject();
208 // resultObj.put(Constant.JOBID, vnfId + "_" + Constant.DELETE);
209 handleResponse(resultObj, appInfo, vnfId, Constant.DELETE);
210 restJson.put("data", resultObj);
212 LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
219 public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
220 LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
221 JSONObject restJson = new JSONObject();
222 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
224 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
225 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
227 int statusCode = queryResult.getInt(Constant.RETCODE);
229 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
230 if(null == (queryResult.get("data"))) {
231 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
234 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
235 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
237 LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
243 public JSONObject getIp(JSONObject vnfmObject, String vnfId) throws IOException {
244 LOG.warn("function=getIp, msg=enter to getIp: {}", vnfId);
245 JSONObject restJson = new JSONObject();
246 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
248 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
249 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
251 int statusCode = queryResult.getInt(Constant.RETCODE);
253 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
254 if(null == (queryResult.get("data"))) {
255 LOG.warn("function=getIp, msg=query is null {}", queryResult.get("data"));
258 JSONObject config = JSONObject.fromObject(queryResult.getString("data"));
259 LOG.info("function=getIp, query configuration result: {}", config);
260 JSONObject vnfInfo = config.getJSONArray("configuration").getJSONObject(0);
261 JSONObject result = new JSONObject();
262 result.put("vnf_id", vnfInfo.getString("vnf_id"));
263 result.put("vnf_type", vnfInfo.getString("vnf_type"));
264 JSONArray inputs = vnfInfo.getJSONArray("inputs");
266 ClassLoader classLoader = getClass().getClassLoader();
267 String ipConfig = IOUtils.toString(classLoader.getResourceAsStream("ipConfig.json"));
268 LOG.info("ipConfig: {}", ipConfig);
269 JSONObject ipCon = JSONObject.fromObject(ipConfig);
270 String vnfType = vnfInfo.getString("vnf_type");
271 if(ipCon.containsKey(vnfType)) {
272 String ipKey = ipCon.getString(vnfInfo.getString("vnf_type"));
273 LOG.info("ipKey: {}", ipKey);
275 for(int i = 0; i < inputs.size(); i++) {
276 JSONObject obj = inputs.getJSONObject(i);
277 if(obj.getString("key_name").equals(ipKey)) {
278 ip = obj.getString("value");
282 result.put("ip", ip);
283 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
284 restJson.put("data", result);
288 LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
295 public JSONObject getJob(JSONObject vnfmObject, String jobId) {
296 LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
297 JSONObject restJson = new JSONObject();
298 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
300 String vnfId = jobId.split("_")[0];
301 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
302 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
303 null, Constant.CERTIFICATE);
305 int statusCode = queryResult.getInt(Constant.RETCODE);
307 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
309 if((queryResult.get("data")) == null) {
310 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
313 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
314 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
316 LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
328 * @param vnfInstanceId
332 public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
333 LOG.info("healVnf request body :" + jsonObject);
334 JSONObject restJson = new JSONObject();
335 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
337 String action = jsonObject.getString("action");
338 JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
339 String vmId = affectedVm.getString("vmid");
340 String path = String.format(ParamConstants.HEAL_VNF, vmId);
342 JSONObject subJsonObject = new JSONObject();
343 subJsonObject.put("type", "hard");
344 subJsonObject.put("boot_mode", "");
345 if("vmReset".equals(action)) {
346 subJsonObject.put("action", "reset");
348 LOG.info("healVnf subJsonObject :" + subJsonObject);
349 JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
350 Constant.CERTIFICATE);
352 int statusCode = healResult.getInt(Constant.RETCODE);
353 if(statusCode == Constant.HTTP_OK) {
354 LOG.info("healResult:{}", healResult);
355 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
357 LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
363 public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
364 LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
366 JSONObject restJson = new JSONObject();
367 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
369 JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
370 Constant.GET, null, Constant.CERTIFICATE);
372 int statusCode = queryResult.getInt(Constant.RETCODE);
373 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
374 if((queryResult.get("data")) == null) {
375 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
378 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
379 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
381 LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);