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.vnfm.gvnfm.jujuvnfmadapter.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.gvnfm.jujuvnfmadapter.service.adapter.impl.JujuAdapter2MSBManager;
29 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.adapter.inf.IJujuAdapter2MSBManager;
30 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.api.internalsvc.inf.IJujuAdapterMgrService;
31 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
32 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.UrlConstant;
33 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.restclient.SystemEnvVariablesFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import net.sf.json.JSONObject;
40 * Juju adapter manager service class.<br>
45 * @version NFVO 0.5 Sep 12, 2016
47 public class JujuAdapterMgrService implements IJujuAdapterMgrService {
49 private static final Logger LOG = LoggerFactory.getLogger(JujuAdapterMgrService.class);
52 public void register() {
53 // set BUS URL and mothedtype
54 Map<String, String> paramsMap = new HashMap<>();
55 paramsMap.put("url", UrlConstant.REST_MSB_REGISTER);
56 paramsMap.put("methodType", Constant.POST);
58 // get juju adapter info and raise registration
60 String adapterInfo = readJujuAdapterInfoFromJson();
61 if(!"".equals(adapterInfo)) {
62 JSONObject adapterObject = JSONObject.fromObject(adapterInfo);
63 RegisterJujuAdapterThread jujuAdapterThread = new RegisterJujuAdapterThread(paramsMap, adapterObject);
64 Executors.newSingleThreadExecutor().submit(jujuAdapterThread);
66 LOG.error("JujuVnfmAdapter info is null,please check!", JujuAdapterMgrService.class);
69 } catch(IOException e) {
70 LOG.error("Failed to read JujuVnfmAdapter info!" + e, JujuAdapterMgrService.class);
76 * Read juju adapter information from Json.<br>
82 public static String readJujuAdapterInfoFromJson() throws IOException {
83 String fileContent = "";
85 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
86 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
87 + "adapterInfo" + System.getProperty(Constant.FILE_SEPARATOR) + Constant.JUJUADAPTERINFO;
90 InputStream ins = new FileInputStream(fileName);
91 BufferedInputStream bins = new BufferedInputStream(ins)){
93 byte[] contentByte = new byte[ins.available()];
94 int num = bins.read(contentByte);
97 fileContent = new String(contentByte);
99 } catch(FileNotFoundException e) {
100 LOG.error(fileName + "is not found!", e, JujuAdapterMgrService.class);
106 private static class RegisterJujuAdapterThread implements Runnable {
108 // Thread lock Object
109 private final Object lockObject = new Object();
111 private IJujuAdapter2MSBManager adapter2MSBMgr = new JujuAdapter2MSBManager();
113 // url and mothedtype
114 private Map<String, String> paramsMap;
117 private JSONObject adapterInfo;
119 public RegisterJujuAdapterThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
120 this.paramsMap = paramsMap;
121 this.adapterInfo = adapterInfo;
126 LOG.info("start register jujuvnfmadapter", RegisterJujuAdapterThread.class);
128 if(paramsMap == null || adapterInfo == null) {
129 LOG.error("parameter is null,please check!", RegisterJujuAdapterThread.class);
133 // catch Runtime Exception
135 sendRequest(paramsMap, adapterInfo);
136 } catch(RuntimeException e) {
137 LOG.error(e.getMessage(), e);
142 private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo) {
143 JSONObject resultObj = adapter2MSBMgr.registerJujuAdapter(paramsMap, driverInfo);
145 if(Integer.valueOf(resultObj.get("retCode").toString()) == Constant.HTTP_CREATED) {
146 LOG.info("JujuVnfmAdapter has now Successfully Registered to the Microservice BUS!",
147 JujuAdapterMgrService.class);
149 LOG.error("JujuVnfmAdapter failed to Register to the Microservice BUS! Reason:"
150 + resultObj.get("reason").toString() + " retCode:" + resultObj.get("retCode").toString());
152 // if registration fails,wait one minute and try again
154 synchronized(lockObject) {
155 lockObject.wait(Constant.REPEAT_REG_TIME);
157 } catch(InterruptedException e) {
158 LOG.error("sendRequest error", e);
161 sendRequest(this.paramsMap, this.adapterInfo);
169 public void unregister() {
180 public static void main(String[] args) {
181 LOG.info(SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty(Constant.FILE_SEPARATOR)
182 + "etc" + System.getProperty(Constant.FILE_SEPARATOR) + "adapterInfo"
183 + System.getProperty(Constant.FILE_SEPARATOR) + Constant.JUJUADAPTERINFO);