Update URL according to microservice name
[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.servicetoken.VnfmRestfulUtil;
20 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
21 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
22 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
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 = "{\"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\"}";
64         JSONObject json = JSONObject.fromObject(vInfo);
65         json.put("vnfmId",vnfmId);
66         return json;
67     }
68
69     /**
70      * Get vnfmInfo by id
71      * <br/>
72      *
73      * @param ip
74      * @return
75      * @since  NFVO 0.5
76      */
77     public static String getVnfmIdByIp(String ip) {
78         RestfulResponse rsp =
79                 VnfmRestfulUtil.getRemoteResponse(ParamConstants.ESR_GET_VNFMS_URL, VnfmRestfulUtil.TYPE_GET, null);
80         if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
81             return "";
82         }
83
84         JSONArray vnfmList = JSONArray.fromObject(rsp.getResponseContent());
85         LOGGER.info("vnfm ip: {}, vnfmList: {}", ip, vnfmList);
86         for(int i = 0; i < vnfmList.size(); i++) {
87             if(vnfmList.getJSONObject(i).getString("url").contains(ip)) {
88                 return vnfmList.getJSONObject(i).getString("vnfmId");
89             }
90         }
91
92         return "";
93     }
94 }