574910071b0d6b24a1c7bed85d23ace8b004f438
[vfc/nfvo/driver/vnfm/svnfm.git] /
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 NFVO 0.5 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      * Get vnfmInfo by ip
46      * <br/>
47      *
48      * @param vnfmId
49      * @return
50      * @since NFVO 0.5
51      */
52     public static JSONObject getVnfmById(String vnfmId) {
53         RestfulResponse rsp = VnfmRestfulUtil.getRemoteResponse(String.format(ParamConstants.ESR_GET_VNFM_URL, vnfmId),
54                 VnfmRestfulUtil.TYPE_GET, null);
55         if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
56             LOGGER.error("funtion=getVnfmById, status={}", rsp.getStatus());
57             return null;
58         }
59         return JSONObject.fromObject(rsp.getResponseContent());
60     }
61
62     public static JSONObject mockForTest(String vnfmId) {
63         String vInfo =
64                 "{\"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\"}";
65         JSONObject json = JSONObject.fromObject(vInfo);
66         json.put("vnfmId", vnfmId);
67         return json;
68     }
69
70     /**
71      * Get vnfmInfo by id
72      * <br/>
73      *
74      * @param ip
75      * @return
76      * @since NFVO 0.5
77      */
78     public static String getVnfmIdByIp(String ip) {
79         RestfulResponse rsp =
80                 VnfmRestfulUtil.getRemoteResponse(ParamConstants.ESR_GET_VNFMS_URL, VnfmRestfulUtil.TYPE_GET, null);
81         if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
82             return "";
83         }
84
85         JSONArray vnfmList = JSONArray.fromObject(rsp.getResponseContent());
86         LOGGER.info("vnfm ip: {}, vnfmList: {}", ip, vnfmList);
87         for(int i = 0; i < vnfmList.size(); i++) {
88             if(vnfmList.getJSONObject(i).getString("url").contains(ip)) {
89                 return vnfmList.getJSONObject(i).getString("vnfmId");
90             }
91         }
92
93         return "";
94     }
95 }