98171565d84f1400f4a1aabdd970597f43aaaf62
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / openo / nfvo / jujuvnfmadapter / common / VnfmUtil.java
1 /*
2  * Copyright 2016 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.openo.nfvo.jujuvnfmadapter.common;
18
19 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
20 import org.openo.nfvo.jujuvnfmadapter.common.servicetoken.JujuVnfmRestfulUtil;
21 import org.openo.nfvo.jujuvnfmadapter.common.servicetoken.VnfmRestfulUtil;
22 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
23 import org.openo.nfvo.jujuvnfmadapter.service.constant.UrlConstant;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import net.sf.json.JSONArray;
28 import net.sf.json.JSONObject;
29 /**
30  * Provide function of getting vnfmInfo
31  * <br/>
32  * <p>
33  * </p>
34  * 
35  * @author
36  * @version     NFVO 0.5  Aug 25, 2016
37  */
38 public final class VnfmUtil {
39
40     private static final Logger LOG = LoggerFactory.getLogger(VnfmUtil.class);
41     private VnfmUtil() {
42
43     }
44
45     /**
46      * Get vnfmInfo by ip
47      * <br/>
48      * 
49      * @param vnfmId
50      * @return
51      * @since  NFVO 0.5
52      */
53     public static JSONObject getVnfmById(String vnfmId) {
54         if(SwitchController.isDebugModel()){
55             JSONObject json = new JSONObject();
56             json.put("vnfmId", vnfmId);
57             json.put("vnfdId","testVnfdId");
58             json.put("vnfPackageId", "testPackageId");
59             json.put("version", "1");
60             json.put("url",JujuConfigUtil.getValue("jujuvnfm_server_url"));
61             return json;
62         }
63         RestfulResponse rsp = VnfmRestfulUtil.getRemoteResponse(String.format(UrlConstant.REST_ESRINFO_GET, vnfmId), JujuVnfmRestfulUtil.GET_TYPE, null);
64         if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
65             return null;
66         }
67         LOG.error("funtion=getVnfmById, status={}", rsp.getStatus());
68         return JSONObject.fromObject(rsp.getResponseContent());
69     }
70
71     /**
72      * Get vnfmInfo by id
73      * <br/>
74      * 
75      * @param ip
76      * @return
77      * @since  NFVO 0.5
78      */
79     public static String getVnfmIdByIp(String ip) {
80         RestfulResponse rsp =
81                 VnfmRestfulUtil.getRemoteResponse(UrlConstant.REST_ESRINFO_GET, VnfmRestfulUtil.TYPE_GET, null);
82         if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
83             return "";
84         }
85
86         JSONArray vnfmList = JSONArray.fromObject(rsp.getResponseContent());
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 }