2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.init;
19 import java.io.IOException;
21 import org.apache.http.client.ClientProtocolException;
22 import org.json.JSONException;
23 import org.json.JSONObject;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.inf.IMsbMgmr;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.boot.ApplicationArguments;
32 import org.springframework.boot.ApplicationRunner;
33 import org.springframework.stereotype.Component;
36 public class EnvVariablesInitialization implements ApplicationRunner {
37 private static final Logger logger = LoggerFactory.getLogger(EnvVariablesInitialization.class);
40 AdaptorEnv adaptorEnv;
43 private IMsbMgmr msbMgmr;
46 public void run(ApplicationArguments args){
49 } catch (Exception e) {
50 logger.error("getMsbIpAndPort error", e);
55 handleAaiMsbServiceInfo();
56 } catch (Exception e) {
57 logger.error("handleAaiMsbServiceInfo error", e);
61 handleLcmMsbServiceInfo();
62 } catch (Exception e) {
63 logger.error("handleLcmMsbServiceInfo error", e);
67 handleCatalogMsbServiceInfo();
68 } catch (Exception e) {
69 logger.error("handleCatalogMsbServiceInfo error", e);
74 private void handleAaiMsbServiceInfo() throws ClientProtocolException, IOException {
75 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getAaiServiceNameInMsb(), adaptorEnv.getAaiVersionInMsb());
76 adaptorEnv.setAaiUrlInMsb(urlInMsb);
77 adaptorEnv.setAaiApiUriFront(generateApiUriFront(urlInMsb));
80 private String generateApiUriFront(String urlInMsb) {
81 return generateMsbApiUriFront() + urlInMsb;
84 private String generateMsbApiUriFront()
86 return CommonConstants.SCHEMA_HTTP + "://" + adaptorEnv.getMsbIp() + ":" + adaptorEnv.getMsbPort();
89 private void handleLcmMsbServiceInfo() throws ClientProtocolException, IOException {
90 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getLcmServiceNameInMsb(), adaptorEnv.getLcmVersionInMsb());
91 adaptorEnv.setLcmUrlInMsb(urlInMsb);
92 adaptorEnv.setLcmApiUriFront(generateApiUriFront(urlInMsb));
95 private void handleCatalogMsbServiceInfo() throws ClientProtocolException, IOException {
96 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getCatalogServiceNameInMsb(), adaptorEnv.getCatalogVersionInMsb());
97 adaptorEnv.setCatalogUrlInMsb(urlInMsb);
98 adaptorEnv.setCatalogApiUriFront(generateApiUriFront(urlInMsb));
101 private void getMsbIpAndPort() throws IOException, JSONException {
102 String msbInfoJsonStr = readMsbInfoFromJsonFile();
103 JSONObject totalJsonObj = new JSONObject(msbInfoJsonStr);
104 JSONObject serverJsonObj = totalJsonObj.getJSONObject("defaultServer");
105 String msb_ip = serverJsonObj.getString("host");
106 int msb_port = serverJsonObj.getInt("port");
108 adaptorEnv.setMsbIp(msb_ip);
109 adaptorEnv.setMsbPort(msb_port);
110 adaptorEnv.setMsbApiUriFront(generateMsbApiUriFront());
113 private String readMsbInfoFromJsonFile() throws IOException {
114 String filePath = "/etc/conf/restclient.json";
115 String fileContent = CommonUtil.getJsonStrFromFile(filePath);