Modify database part in the deployment
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / msb / impl / MsbMgmrImpl.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.msb.impl;
18
19 import java.io.BufferedInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.onap.msb.sdk.discovery.common.RouteException;
31 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
32 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
33 import org.onap.msb.sdk.discovery.entity.RouteResult;
34 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.inf.IMsbMgmr;
36 import org.springframework.stereotype.Component;
37
38 import com.google.gson.Gson;
39
40 @Component
41 public class MsbMgmrImpl implements IMsbMgmr {
42         private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
43         
44         private Gson gson = new Gson();
45         
46         private String msb_ip;
47         
48         private int msb_port;
49         
50         @Override
51         public void register() {
52                 try {
53                         String msbInfoJsonStr = readMsbInfoFromJsonFile();
54                         JSONObject totalJsonObj = new JSONObject(msbInfoJsonStr);
55                         JSONObject serverJsonObj = totalJsonObj.getJSONObject("defaultServer");
56                         msb_ip = serverJsonObj.getString("host");
57                         msb_port = serverJsonObj.getInt("port");
58                         
59                         String vfcAdaptorInfoJsonStr = readVfcAdaptorInfoFromJsonFile();
60                         MicroServiceInfo msinfo = gson.fromJson(vfcAdaptorInfoJsonStr, MicroServiceInfo.class);
61                         
62                         MSBServiceClient msbClient = new MSBServiceClient(msb_ip, msb_port);
63                         MicroServiceFullInfo microServiceInfo = msbClient.registerMicroServiceInfo(msinfo);
64                         logger.info("Registered service response info is " + microServiceInfo.toString());
65                         
66                 } catch (IOException e) {
67                         logger.error("Failed to read vfcadaptor info! ", e);
68                 } catch (RouteException e) {
69                         logger.error("Failed to register nokia vnfm driver! ", e);
70                 } catch (JSONException e) {
71                         logger.error("Failed to retrieve json info! ", e);
72                 }
73                         
74         }
75         
76         private String readMsbInfoFromJsonFile() throws IOException {
77                 String filePath = "/etc/conf/restclient.json";
78                 String fileContent = getJsonStrFromFile(filePath);
79
80         return fileContent;
81         }
82
83         private String readVfcAdaptorInfoFromJsonFile() throws IOException {
84         String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
85                 String fileContent = getJsonStrFromFile(filePath);
86
87         return fileContent;
88     }
89
90         public String getJsonStrFromFile(String filePath) throws IOException {
91                 InputStream ins = null;
92         BufferedInputStream bins = null;
93         String fileContent = "";
94         String fileName = getAppRoot() + filePath;
95
96         try {
97             ins = new FileInputStream(fileName);
98             bins = new BufferedInputStream(ins);
99
100             byte[] contentByte = new byte[ins.available()];
101             int num = bins.read(contentByte);
102
103             if(num > 0) {
104                 fileContent = new String(contentByte);
105             }
106         } catch(FileNotFoundException e) {
107                 logger.error(fileName + "is not found!", e);
108         } finally {
109             if(ins != null) {
110                 ins.close();
111             }
112             if(bins != null) {
113                 bins.close();
114             }
115         }
116                 return fileContent;
117         }
118
119         @Override
120         public void unregister() {
121                 try {
122                         String jsonStr = readVfcAdaptorInfoFromJsonFile();
123                         MicroServiceInfo msinfo = gson.fromJson(jsonStr, MicroServiceInfo.class);
124                         
125                         MSBServiceClient msbClient = new MSBServiceClient(msb_ip, msb_port);
126                         RouteResult routeResult = msbClient.cancelMicroServiceInfo(msinfo.getServiceName(), msinfo.getVersion());
127                         logger.info("unregistered service response info is " + routeResult.toString());
128                         
129                 } catch (IOException e) {
130                         logger.error("Failed to read vfcadaptor info! ", e);
131                 } catch (RouteException e) {
132                         logger.error("Failed to register nokia vnfm driver! ", e);
133                 }
134         }
135         
136     public String getAppRoot() {
137         String appRoot = null;
138         appRoot = System.getProperty("catalina.base");
139         if(appRoot != null) {
140             appRoot = getCanonicalPath(appRoot);
141         }
142         return appRoot;
143     }
144
145     private String getCanonicalPath(final String inPath) {
146         String path = null;
147         try {
148             if(inPath != null) {
149                 final File file = new File(inPath);
150                 path = file.getCanonicalPath();
151             }
152         } catch(final IOException e) {
153             logger.error("file.getCanonicalPath() IOException:", e);
154         }
155         return path;
156     }
157
158 }