2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
 
   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.vnfm.svnfm.vnfmadapter.service.api.internalsvc.impl;
 
  19 import java.io.BufferedInputStream;
 
  20 import java.io.FileInputStream;
 
  21 import java.io.FileNotFoundException;
 
  22 import java.io.IOException;
 
  23 import java.io.InputStream;
 
  24 import java.util.HashMap;
 
  26 import java.util.concurrent.Executors;
 
  28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.SystemEnvVariablesFactory;
 
  29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl.Driver2MSBManager;
 
  30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.inf.IDriver2MSBManager;
 
  31 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.api.internalsvc.inf.IVnfmAdapterMgrService;
 
  32 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
 
  33 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
 
  34 import org.slf4j.Logger;
 
  35 import org.slf4j.LoggerFactory;
 
  37 import net.sf.json.JSONObject;
 
  45  * @version VFC 1.0 Aug 31, 2016
 
  47 public class VnfmAdapterMgrService implements IVnfmAdapterMgrService {
 
  49     private static final Logger LOG = LoggerFactory.getLogger(VnfmAdapterMgrService.class);
 
  51     public static final String VNFMADAPTERINFO = "vnfmadapterinfo.json";
 
  54     public void register() {
 
  55         // set BUS URL and mothedtype
 
  56         Map<String, String> paramsMap = new HashMap<>();
 
  57         paramsMap.put("url", UrlConstant.REST_MSB_REGISTER);
 
  58         paramsMap.put("methodType", Constant.POST);
 
  60         // get vim adapter info and raise registration
 
  62             String adapterInfo = readVnfmAdapterInfoFromJson();
 
  63             if(!"".equals(adapterInfo)) {
 
  64                 JSONObject adapterObject = JSONObject.fromObject(adapterInfo);
 
  65                 RegisterVnfmAdapterThread vnfmAdapterThread = new RegisterVnfmAdapterThread(paramsMap, adapterObject);
 
  66                 Executors.newSingleThreadExecutor().submit(vnfmAdapterThread);
 
  68                 LOG.error("VnfmAdapter info is null,please check!");
 
  71         } catch(IOException e) {
 
  72             LOG.error("Failed to read VnfmAdapter info! " + e.getMessage(), e);
 
  78      * Retrieve VIM driver information.
 
  83     public String readVnfmAdapterInfoFromJson() throws IOException {
 
  84         String fileContent = "";
 
  86         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
 
  87                 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
 
  88                 + "adapterInfo" + System.getProperty(Constant.FILE_SEPARATOR) + VNFMADAPTERINFO;
 
  90         try (InputStream ins = new FileInputStream(fileName)) {
 
  91             try (BufferedInputStream bins = new BufferedInputStream(ins)){
 
  93                 byte[] contentByte = new byte[ins.available()];
 
  94                 int num = bins.read(contentByte);
 
  97                     fileContent = new String(contentByte);
 
 100         } catch(FileNotFoundException e) {
 
 101             LOG.error(fileName + "is not found!", e);
 
 107     private static class RegisterVnfmAdapterThread implements Runnable {
 
 109         // Thread lock Object
 
 110         private final Object lockObject = new Object();
 
 112         private IDriver2MSBManager adapter2MSBMgr = new Driver2MSBManager();
 
 114         // url and mothedtype
 
 115         private Map<String, String> paramsMap;
 
 118         private JSONObject adapterInfo;
 
 120         public RegisterVnfmAdapterThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
 
 121             this.paramsMap = paramsMap;
 
 122             this.adapterInfo = adapterInfo;
 
 127             LOG.info("start register vnfmadapter", RegisterVnfmAdapterThread.class);
 
 129             if(paramsMap == null || adapterInfo == null) {
 
 130                 LOG.error("parameter is null,please check!", RegisterVnfmAdapterThread.class);
 
 134             // catch Runtime Exception
 
 136                 sendRequest(paramsMap, adapterInfo);
 
 137             } catch(RuntimeException e) {
 
 138                 LOG.error(e.getMessage(), e);
 
 143         private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo) {
 
 144             JSONObject resultObj = adapter2MSBMgr.registerDriver(paramsMap, driverInfo);
 
 146             if(Integer.valueOf(resultObj.get(Constant.RETCODE).toString()) == Constant.HTTP_CREATED) {
 
 147                 LOG.info("Vnfmadapter has now Successfully Registered to the Microservice BUS!");
 
 149                 LOG.error("Vnfmadapter failed to  Register to the Microservice BUS! Reason:"
 
 150                         + resultObj.get(Constant.REASON).toString() + " retCode:"
 
 151                         + resultObj.get(Constant.RETCODE).toString());
 
 153                 // if registration fails,wait one minute and try again
 
 155                     synchronized(lockObject) {
 
 156                         lockObject.wait(Constant.REPEAT_REG_TIME);
 
 158                 } catch(InterruptedException e) {
 
 159                     LOG.error(e.getMessage(), e);
 
 160                     // Restore interrupted state...
 
 161                     Thread.currentThread().interrupt();
 
 164                 sendRequest(this.paramsMap, this.adapterInfo);
 
 172     public void unregister() {