Update db process part
[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.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;
34
35 @Component
36 public class EnvVariablesInitialization implements ApplicationRunner {
37         private static final Logger logger = LoggerFactory.getLogger(EnvVariablesInitialization.class);
38
39         @Autowired
40         AdaptorEnv adaptorEnv;
41         
42         @Autowired
43         private IMsbMgmr msbMgmr;
44         
45         @Override
46         public void run(ApplicationArguments args){
47                 try {
48                         getMsbIpAndPort();
49                 } catch (Exception e) {
50                         logger.error("getMsbIpAndPort error", e);
51                         return;
52                 }
53                 
54                 adaptorEnv.setAaiApiUriFront(adaptorEnv.getMsbApiUriFront());
55                 adaptorEnv.setLcmApiUriFront(adaptorEnv.getMsbApiUriFront());
56                 adaptorEnv.setCatalogApiUriFront(adaptorEnv.getMsbApiUriFront());
57                 
58 //              try {
59 //                      handleAaiMsbServiceInfo();
60 //              } catch (Exception e) {
61 //                      logger.error("handleAaiMsbServiceInfo error", e);
62 //              }
63 //              
64 //              try {
65 //                      handleLcmMsbServiceInfo();
66 //              } catch (Exception e) {
67 //                      logger.error("handleLcmMsbServiceInfo error", e);
68 //              }
69 //              
70 //              try {
71 //                      handleCatalogMsbServiceInfo();
72 //              } catch (Exception e) {
73 //                      logger.error("handleCatalogMsbServiceInfo error", e);
74 //              }
75                 
76         }
77
78         private void handleAaiMsbServiceInfo() throws ClientProtocolException, IOException {
79                 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getAaiServiceNameInMsb(), adaptorEnv.getAaiVersionInMsb());
80                 adaptorEnv.setAaiUrlInMsb(urlInMsb);
81                 adaptorEnv.setAaiApiUriFront(generateApiUriFront(urlInMsb));
82         }
83
84         private String generateApiUriFront(String urlInMsb) {
85                 return generateMsbApiUriFront() + urlInMsb;
86         }
87         
88         private String generateMsbApiUriFront()
89         {
90                 return CommonConstants.SCHEMA_HTTP + "://" + adaptorEnv.getMsbIp() + ":" + adaptorEnv.getMsbPort();
91         }
92         
93         private void handleLcmMsbServiceInfo() throws ClientProtocolException, IOException {
94                 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getLcmServiceNameInMsb(), adaptorEnv.getLcmVersionInMsb());
95                 adaptorEnv.setLcmUrlInMsb(urlInMsb);
96                 adaptorEnv.setLcmApiUriFront(generateApiUriFront(urlInMsb));
97         }
98         
99         private void handleCatalogMsbServiceInfo() throws ClientProtocolException, IOException {
100                 String urlInMsb = msbMgmr.getServiceUrlInMsbBySeriveNameAndVersion(adaptorEnv.getCatalogServiceNameInMsb(), adaptorEnv.getCatalogVersionInMsb());
101                 adaptorEnv.setCatalogUrlInMsb(urlInMsb);
102                 adaptorEnv.setCatalogApiUriFront(generateApiUriFront(urlInMsb));
103         }
104
105         private void getMsbIpAndPort() throws IOException, JSONException {
106                 String msbInfoJsonStr = readMsbInfoFromJsonFile();
107                 JSONObject totalJsonObj = new JSONObject(msbInfoJsonStr);
108                 JSONObject serverJsonObj = totalJsonObj.getJSONObject("defaultServer");
109                 String msb_ip = serverJsonObj.getString("host");
110                 int msb_port = serverJsonObj.getInt("port");
111                 
112                 adaptorEnv.setMsbIp(msb_ip);
113                 adaptorEnv.setMsbPort(msb_port);
114                 adaptorEnv.setMsbApiUriFront(generateMsbApiUriFront());
115         }
116         
117         private String readMsbInfoFromJsonFile() throws IOException {
118                 String filePath = "/etc/conf/restclient.json";
119                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
120
121         return fileContent;
122         }
123 }