2 * Copyright 2016 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.multivimproxy.service.adapter.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.multivimproxy.service.adapter.inf.IMultivimProxyAdapterMgrService;
29 import org.onap.vfc.nfvo.multivimproxy.common.constant.Constant;
30 import org.onap.vfc.nfvo.multivimproxy.common.constant.HttpConstant;
31 import org.onap.vfc.nfvo.multivimproxy.common.constant.ParamConstant;
32 import org.onap.vfc.nfvo.multivimproxy.common.constant.UrlConstant;
33 import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.SystemEnvVariablesFactory;
34 import org.onap.vfc.nfvo.multivimproxy.service.adapter.inf.IMultivimProxyAdapter2MSBManager;
35 import org.onap.vfc.nfvo.multivimproxy.service.adapter.inf.IMultivimProxyAdapterMgrService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import net.sf.json.JSONObject;
47 * @version VFC 1.0 Sep 22, 2016
49 public class MultivimProxyAdapterMgrService implements IMultivimProxyAdapterMgrService {
51 private static final Logger LOG = LoggerFactory.getLogger(MultivimProxyAdapterMgrService.class);
53 public static final String RESMGRADAPTERINFO = "resmgradapterinfo.json";
56 public void register() {
57 // set BUS URL and mothedtype
58 Map<String, String> paramsMap = new HashMap<>();
59 paramsMap.put("url", UrlConstant.REST_MSB_REGISTER);
60 paramsMap.put("methodType", ParamConstant.PARAM_POST);
62 // get multivimproxy info and raise registration
64 String resmgrInfo = readVimAdapterInfoFromJson();
65 if(!"".equals(resmgrInfo)) {
66 JSONObject adapterObject = JSONObject.fromObject(resmgrInfo);
67 RegisterMultivimProxyThread resmgrThread = new RegisterMultivimProxyThread(paramsMap, adapterObject);
68 Executors.newSingleThreadExecutor().submit(resmgrThread);
70 LOG.error("Resmgr info is null,please check!");
73 } catch(IOException e) {
74 LOG.error("Failed to read Resmgr info! " + e.getMessage(), e);
80 * Retrieve VIM driver information.
85 public static String readVimAdapterInfoFromJson() throws IOException {
86 String fileContent = "";
88 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
89 + "etc" + System.getProperty("file.separator") + "adapterInfo" + System.getProperty("file.separator")
93 InputStream ins = new FileInputStream(fileName);
94 BufferedInputStream bins = new BufferedInputStream(ins)){
96 byte[] contentByte = new byte[ins.available()];
97 int num = bins.read(contentByte);
100 fileContent = new String(contentByte);
102 } catch(FileNotFoundException e) {
103 LOG.error(fileName + "is not found!", e);
109 private static class RegisterMultivimProxyThread implements Runnable {
111 // Thread lock Object
112 private final Object lockObject = new Object();
114 private IMultivimProxyAdapter2MSBManager adapter2MSBMgr = new MultivimProxyAdapter2MSBManager();
116 // url and mothedtype
117 private Map<String, String> paramsMap;
120 private JSONObject adapterInfo;
122 public RegisterMultivimProxyThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
123 this.paramsMap = paramsMap;
124 this.adapterInfo = adapterInfo;
129 LOG.info("start register resmgr", RegisterMultivimProxyThread.class);
131 if(paramsMap == null || adapterInfo == null) {
132 LOG.error("parameter is null,please check!", RegisterMultivimProxyThread.class);
136 // catch Runtime Exception
138 sendRequest(paramsMap, adapterInfo);
139 } catch(Exception e) {
140 LOG.error(e.getMessage(), e);
145 private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo)throws InterruptedException {
146 JSONObject resultObj = adapter2MSBMgr.registerProxy(paramsMap, driverInfo);
148 if(Integer.valueOf(resultObj.get("retCode").toString()) == HttpConstant.HTTP_CREATED) {
149 LOG.info("Resmgr has now Successfully Registered to the Microservice BUS!");
151 LOG.error("Resmgr failed to Register to the Microservice BUS! Reason:"
152 + resultObj.get("reason").toString() + " retCode:" + resultObj.get("retCode").toString());
154 // if registration fails,wait one minute and try again
155 synchronized(lockObject) {
156 while(Integer.valueOf(resultObj.get("retCode").toString()) != Constant.HTTP_CREATED){
157 lockObject.wait(Constant.REPEAT_REG_TIME);
158 resultObj = adapter2MSBMgr.registerProxy(this.paramsMap, this.adapterInfo);
161 LOG.info("Resmgr has now Successfully Registered to the Microservice BUS!");
169 public void unregister() {