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.common;
19 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
20 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
21 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
22 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import net.sf.json.JSONArray;
27 import net.sf.json.JSONObject;
30 * Provide function of getting vnfmInfo
34 * @version VFC 1.0 Aug 25, 2016
36 public final class VnfmUtil {
38 private static final Logger LOGGER = LoggerFactory.getLogger(VnfmUtil.class);
51 public static JSONObject getVnfmById(String vnfmId) {
52 RestfulResponse rsp = VnfmRestfulUtil.getRemoteResponse(String.format(ParamConstants.ESR_GET_VNFM_URL, vnfmId),
53 VnfmRestfulUtil.TYPE_GET, null);
55 LOGGER.error("funtion=getVnfmById, response is null.");
58 if(rsp.getStatus() != Constant.HTTP_OK) {
59 LOGGER.error("funtion=getVnfmById, status={}", rsp.getStatus());
62 JSONObject esrVnfm = JSONObject.fromObject(rsp.getResponseContent());
63 LOGGER.info("esrVnfm: {}", esrVnfm);
64 JSONObject vnfmJson = parseEsrVnfm(esrVnfm);
65 LOGGER.info("vnfmJson: {}", esrVnfm);
77 * "certificate-url": "",
78 * "resource-version": "",
79 * "esr-system-info-list": [{
80 * "esr-system-info-id": "",
95 * "default-tenant": "",
96 * "resource-version": "",
97 * "relationship-list": [
101 * "relationship-list": [{
103 * "related-link": "",
104 * "relationship-data": [],
105 * "related-to-property": []
116 * "vendor": "huawei",
118 * "description": "vnfm",
119 * "certificateUrl": "",
120 * "url": "https://192.168.44.126:30001",
121 * "userName": "manoadmin",
122 * "password": "User@12345",
123 * "createTime": "2016-07-06 15:33:18"
127 private static JSONObject parseEsrVnfm(JSONObject esrVnfm) {
128 JSONObject vnfmObj = new JSONObject();
129 JSONObject esrSysInfo = esrVnfm.getJSONArray("esr-system-info-list").getJSONObject(0);
130 vnfmObj.put(Constant.VNFMID, esrSysInfo.getString("esr-system-info-id"));
131 vnfmObj.put("name", esrSysInfo.getString("system-name"));
132 vnfmObj.put("type", esrSysInfo.getString("type"));
133 vnfmObj.put("vimId", esrVnfm.getString("vim-id"));
134 vnfmObj.put("vendor", esrSysInfo.getString("vendor"));
135 vnfmObj.put("version", esrSysInfo.getString("version"));
136 vnfmObj.put("description", "");
137 vnfmObj.put("certificateUrl", esrVnfm.getString("certificate-url"));
138 vnfmObj.put("url", esrSysInfo.getString("service-url"));
139 vnfmObj.put("userName", esrSysInfo.getString("user-name"));
140 vnfmObj.put("password", esrSysInfo.getString("password"));
141 vnfmObj.put("createTime", "");
145 public static JSONObject mockForTest(String vnfmId) {
147 "{\"vnfmId\":\"1234\", \"name\":\"vnfm\", \"type\":\"Tacker\", \"vimId\":\"\", \"vendor\":\"huawei\", \"version\":\"v1.0\", \"description\":\"vnfm\", \"certificateUrl\":\"\", \"url\":\"https://192.168.44.126:30001\", \"userName\":\"manoadmin\", \"password\":\"User@12345\", \"createTime\":\"2016-07-06 15:33:18\"}";
148 JSONObject json = JSONObject.fromObject(vInfo);
149 json.put(Constant.VNFMID, vnfmId);
161 public static String getVnfmIdByIp(String ip) {
162 RestfulResponse rsp =
163 VnfmRestfulUtil.getRemoteResponse(ParamConstants.ESR_GET_VNFMS_URL, VnfmRestfulUtil.TYPE_GET, null);
164 if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
168 JSONArray vnfmList = JSONArray.fromObject(rsp.getResponseContent());
169 LOGGER.info("vnfm ip: {}, vnfmList: {}", ip, vnfmList);
170 for(int i = 0; i < vnfmList.size(); i++) {
171 if(vnfmList.getJSONObject(i).getString("url").contains(ip)) {
172 return vnfmList.getJSONObject(i).getString(Constant.VNFMID);