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.msb.impl;
 
  19 import java.io.BufferedInputStream;
 
  21 import java.io.FileInputStream;
 
  22 import java.io.FileNotFoundException;
 
  23 import java.io.IOException;
 
  24 import java.io.InputStream;
 
  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;
 
  38 import com.google.gson.Gson;
 
  41 public class MsbMgmrImpl implements IMsbMgmr {
 
  42         private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
 
  44         private Gson gson = new Gson();
 
  46         private String msb_ip;
 
  51         public void register() {
 
  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");
 
  59                         String vfcAdaptorInfoJsonStr = readVfcAdaptorInfoFromJsonFile();
 
  60                         MicroServiceInfo msinfo = gson.fromJson(vfcAdaptorInfoJsonStr, MicroServiceInfo.class);
 
  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());
 
  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);
 
  76         private String readMsbInfoFromJsonFile() throws IOException {
 
  77                 String filePath = "/etc/conf/restclient.json";
 
  78                 String fileContent = getJsonStrFromFile(filePath);
 
  83         private String readVfcAdaptorInfoFromJsonFile() throws IOException {
 
  84         String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
 
  85                 String fileContent = getJsonStrFromFile(filePath);
 
  90         public String getJsonStrFromFile(String filePath) throws IOException {
 
  91                 InputStream ins = null;
 
  92         BufferedInputStream bins = null;
 
  93         String fileContent = "";
 
  94         String fileName = getAppRoot() + filePath;
 
  97             ins = new FileInputStream(fileName);
 
  98             bins = new BufferedInputStream(ins);
 
 100             byte[] contentByte = new byte[ins.available()];
 
 101             int num = bins.read(contentByte);
 
 104                 fileContent = new String(contentByte);
 
 106         } catch(FileNotFoundException e) {
 
 107                 logger.error(fileName + "is not found!", e);
 
 120         public void unregister() {
 
 122                         String jsonStr = readVfcAdaptorInfoFromJsonFile();
 
 123                         MicroServiceInfo msinfo = gson.fromJson(jsonStr, MicroServiceInfo.class);
 
 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());
 
 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);
 
 136     public String getAppRoot() {
 
 137         String appRoot = null;
 
 138         appRoot = System.getProperty("catalina.base");
 
 139         if(appRoot != null) {
 
 140             appRoot = getCanonicalPath(appRoot);
 
 145     private String getCanonicalPath(final String inPath) {
 
 149                 final File file = new File(inPath);
 
 150                 path = file.getCanonicalPath();
 
 152         } catch(final IOException e) {
 
 153             logger.error("file.getCanonicalPath() IOException:", e);