7e756219cd66becded274d2027f0cb32049e178a
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / init / EnvVariablesInitialization.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
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.driver.vnfm.svnfm.init;
18
19 import java.io.IOException;
20
21 import org.apache.http.client.ClientProtocolException;
22 import org.json.JSONException;
23 import org.json.JSONObject;
24 import org.onap.msb.sdk.discovery.common.RouteException;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.inf.IMsbMgmr;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.boot.ApplicationArguments;
33 import org.springframework.boot.ApplicationRunner;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public class EnvVariablesInitialization implements ApplicationRunner {
38         private static final Logger logger = LoggerFactory.getLogger(EnvVariablesInitialization.class);
39
40         @Autowired
41         AdaptorEnv adaptorEnv;
42         
43         @Autowired
44         private IMsbMgmr msbMgmr;
45         
46         @Override
47         public void run(ApplicationArguments args){
48                 try {
49                         getMsbIpAndPort();
50                 } catch (Exception e) {
51                         logger.error("getMsbIpAndPort error", e);
52                         return;
53                 }
54                 
55                 try {
56                         handleAaiMsbServiceInfo();
57                 } catch (Exception e) {
58                         logger.error("handleAaiMsbServiceInfo error", e);
59                 }
60                 
61                 try {
62                         handLcmMsbServiceInfo();
63                 } catch (Exception e) {
64                         logger.error("handLcmMsbServiceInfo error", e);
65                 }
66                 
67                 try {
68                         handCatalogMsbServiceInfo();
69                 } catch (Exception e) {
70                         logger.error("handCatalogMsbServiceInfo error", e);
71                 }
72                 
73         }
74
75         private void handleAaiMsbServiceInfo() throws RouteException, ClientProtocolException, IOException {
76                 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getAaiServiceNameInMsb(), adaptorEnv.getAaiVersionInMsb());
77                 adaptorEnv.setAaiUrlInMsb(urlInMsb);
78                 adaptorEnv.setAaiApiUriFront(generateApiUriFront(urlInMsb));
79         }
80
81         private String generateApiUriFront(String urlInMsb) {
82                 return generateMsbApiUriFront() + urlInMsb;
83         }
84         
85         private String generateMsbApiUriFront()
86         {
87                 return CommonConstants.SCHEMA_HTTP + "://" + adaptorEnv.getMsbIp() + ":" + adaptorEnv.getMsbPort();
88         }
89         
90         private void handLcmMsbServiceInfo() throws RouteException, ClientProtocolException, IOException {
91                 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getLcmServiceNameInMsb(), adaptorEnv.getLcmVersionInMsb());
92                 adaptorEnv.setLcmUrlInMsb(urlInMsb);
93                 adaptorEnv.setLcmApiUriFront(generateApiUriFront(urlInMsb));
94         }
95         
96         private void handCatalogMsbServiceInfo() throws RouteException, ClientProtocolException, IOException {
97                 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getCatalogServiceNameInMsb(), adaptorEnv.getCatalogVersionInMsb());
98                 adaptorEnv.setCatalogUrlInMsb(urlInMsb);
99                 adaptorEnv.setCatalogApiUriFront(generateApiUriFront(urlInMsb));
100         }
101
102         private void getMsbIpAndPort() throws IOException, JSONException {
103                 String msbInfoJsonStr = readMsbInfoFromJsonFile();
104                 JSONObject totalJsonObj = new JSONObject(msbInfoJsonStr);
105                 JSONObject serverJsonObj = totalJsonObj.getJSONObject("defaultServer");
106                 String msb_ip = serverJsonObj.getString("host");
107                 int msb_port = serverJsonObj.getInt("port");
108                 
109                 adaptorEnv.setMsbIp(msb_ip);
110                 adaptorEnv.setMsbPort(msb_port);
111                 adaptorEnv.setMsbApiUriFront(generateMsbApiUriFront());
112         }
113         
114         private String readMsbInfoFromJsonFile() throws IOException {
115                 String filePath = "/etc/conf/restclient.json";
116                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
117
118         return fileContent;
119         }
120 }