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