0ebab2eefe956acba814edbcfbd00ae0b015444e
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / VnfmUtil.java
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common;
18
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;
25
26 import net.sf.json.JSONArray;
27 import net.sf.json.JSONObject;
28
29 /**
30  * Provide function of getting vnfmInfo
31  * <br/>
32  *
33  * @author
34  * @version VFC 1.0 Aug 25, 2016
35  */
36 public final class VnfmUtil {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(VnfmUtil.class);
39
40     private VnfmUtil() {
41
42     }
43
44     /**
45      * <br>
46      * 
47      * @param vnfmId
48      * @return
49      * @since VFC 1.0
50      */
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);
54         if(rsp == null) {
55             LOGGER.error("funtion=getVnfmById, response is null.");
56             return null;
57         }
58         if(rsp.getStatus() != Constant.HTTP_OK) {
59             LOGGER.error("funtion=getVnfmById, status={}", rsp.getStatus());
60             return null;
61         }
62         JSONObject esrVnfm = JSONObject.fromObject(rsp.getResponseContent());
63         LOGGER.info("esrVnfm: {}", esrVnfm);
64         JSONObject vnfmJson = parseEsrVnfm(esrVnfm);
65         LOGGER.info("vnfmJson: {}", esrVnfm);
66         return vnfmJson;
67     }
68
69     /**
70      * <br>
71      * 
72      * @param
73      *            esrVnfm
74      *            {
75      *            "vnfm-id": "",
76      *            "vim-id": "",
77      *            "certificate-url": "",
78      *            "resource-version": "",
79      *            "esr-system-info-list": [{
80      *            "esr-system-info-id": "",
81      *            "system-name": "",
82      *            "type": "",
83      *            "vendor": "",
84      *            "version": "",
85      *            "service-url": "",
86      *            "user-name": "",
87      *            "password": "",
88      *            "system-type": "",
89      *            "protocal": "",
90      *            "ssl-cacert": "",
91      *            "ssl-insecure": "",
92      *            "ip-address": "",
93      *            "port": "",
94      *            "cloud-domain": "",
95      *            "default-tenant": "",
96      *            "resource-version": "",
97      *            "relationship-list": [
98      *            ]
99      *            }
100      *            ],
101      *            "relationship-list": [{
102      *            "related-to": "",
103      *            "related-link": "",
104      *            "relationship-data": [],
105      *            "related-to-property": []
106      *            }
107      *            ]
108      *            }
109      * @return
110      *         vnfmJson
111      *         {
112      *         "vnfmId": "1234",
113      *         "name": "vnfm",
114      *         "type": "Tacker",
115      *         "vimId": "",
116      *         "vendor": "huawei",
117      *         "version": "v1.0",
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"
124      *         }
125      * @since VFC 1.0
126      */
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", "");
142         return vnfmObj;
143     }
144
145     public static JSONObject mockForTest(String vnfmId) {
146         String vInfo =
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);
150         return json;
151     }
152
153     /**
154      * Get vnfmInfo by ip
155      * <br/>
156      *
157      * @param ip
158      * @return
159      * @since VFC 1.0
160      */
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) {
165             return "";
166         }
167
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);
173             }
174         }
175
176         return "";
177     }
178 }