c735e784f4168ffb8f487b3c0b806d685e229f02
[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         return JSONObject.fromObject(rsp.getResponseContent());
63     }
64
65     public static JSONObject mockForTest(String vnfmId) {
66         String vInfo =
67                 "{\"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\"}";
68         JSONObject json = JSONObject.fromObject(vInfo);
69         json.put("vnfmId", vnfmId);
70         return json;
71     }
72
73     /**
74      * Get vnfmInfo by id
75      * <br/>
76      *
77      * @param ip
78      * @return
79      * @since VFC 1.0
80      */
81     public static String getVnfmIdByIp(String ip) {
82         RestfulResponse rsp =
83                 VnfmRestfulUtil.getRemoteResponse(ParamConstants.ESR_GET_VNFMS_URL, VnfmRestfulUtil.TYPE_GET, null);
84         if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
85             return "";
86         }
87
88         JSONArray vnfmList = JSONArray.fromObject(rsp.getResponseContent());
89         LOGGER.info("vnfm ip: {}, vnfmList: {}", ip, vnfmList);
90         for(int i = 0; i < vnfmList.size(); i++) {
91             if(vnfmList.getJSONObject(i).getString("url").contains(ip)) {
92                 return vnfmList.getJSONObject(i).getString("vnfmId");
93             }
94         }
95
96         return "";
97     }
98 }