2 * Copyright 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.service.adapter.impl.VnfmAdapter2DriverManager;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.inf.IVnfmAdapter2DriverManager;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.api.internalsvc.inf.IVnfmAdapter2DriverMgrService;
31 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
32 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
33 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import net.sf.json.JSONObject;
45 * @version NFVO 0.5 Jan 23, 2017
47 public class VnfmAdapter2DriverMgrService implements IVnfmAdapter2DriverMgrService {
49 private static final Logger LOG = LoggerFactory.getLogger(VnfmAdapter2DriverMgrService.class);
51 public static final String VNFMADAPTER2DRIVERMGR = "vnfmadapter2drivermgr.json";
54 public void register() {
55 // set URL and mothedtype
56 Map<String, String> paramsMap = new HashMap<>();
57 paramsMap.put("url", UrlConstant.REST_DRIVERMGR_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 RegisterVnfm2DriverMgrThread vnfmAdapterThread =
66 new RegisterVnfm2DriverMgrThread(paramsMap, adapterObject);
67 Executors.newSingleThreadExecutor().submit(vnfmAdapterThread);
69 LOG.error("vnfmadapter2drivermgr info is null,please check!");
72 } catch(IOException e) {
73 LOG.error("Failed to read vnfmadapter2drivermgr info! " + e.getMessage(), e);
79 * Retrieve VIM driver information.
84 public static String readVnfmAdapterInfoFromJson() throws IOException {
85 InputStream ins = null;
86 BufferedInputStream bins = null;
87 String fileContent = "";
89 String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
90 + "etc" + System.getProperty("file.separator") + "adapterInfo" + System.getProperty("file.separator")
91 + VNFMADAPTER2DRIVERMGR;
94 ins = new FileInputStream(fileName);
95 bins = new BufferedInputStream(ins);
97 byte[] contentByte = new byte[ins.available()];
98 int num = bins.read(contentByte);
101 fileContent = new String(contentByte);
103 } catch(FileNotFoundException e) {
104 LOG.error(fileName + "is not found!", e);
117 private static class RegisterVnfm2DriverMgrThread implements Runnable {
119 // Thread lock Object
120 private final Object lockObject = new Object();
122 private IVnfmAdapter2DriverManager adapter2DriverMgr = new VnfmAdapter2DriverManager();
124 // url and mothedtype
125 private Map<String, String> paramsMap;
128 private JSONObject adapterInfo;
130 public RegisterVnfm2DriverMgrThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
131 this.paramsMap = paramsMap;
132 this.adapterInfo = adapterInfo;
137 LOG.info("start register vnfmadapter to Driver Manager", RegisterVnfm2DriverMgrThread.class);
139 if(paramsMap == null || adapterInfo == null) {
140 LOG.error("parameter is null,please check!", RegisterVnfm2DriverMgrThread.class);
144 // catch Runtime Exception
146 sendRequest(paramsMap, adapterInfo);
147 } catch(RuntimeException e) {
148 LOG.error(e.getMessage(), e);
153 private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo) {
154 JSONObject resultObj = adapter2DriverMgr.registerDriver(paramsMap, driverInfo);
156 if(Integer.valueOf(resultObj.get("retCode").toString()) == Constant.HTTP_CREATED) {
157 LOG.info("Vnfmadapter has now Successfully Registered to the Driver Manager!");
159 LOG.error("Vnfmadapter failed to Register to the Driver Manager! Reason:"
160 + resultObj.get("reason").toString() + " retCode:" + resultObj.get("retCode").toString());
162 // if registration fails,wait one minute and try again
164 synchronized(lockObject) {
165 lockObject.wait(Constant.REPEAT_REG_TIME);
167 } catch(InterruptedException e) {
168 LOG.error(e.getMessage(), e);
171 sendRequest(this.paramsMap, this.adapterInfo);
179 public void unregister() {