bb4ec3a15137cd8bf8249e740f84618506258598
[vfc/nfvo/multivimproxy.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.multivimproxy.service.adapter.impl;
18
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;
25 import java.util.Map;
26 import java.util.concurrent.Executors;
27
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;
38
39 import net.sf.json.JSONObject;
40
41 /**
42  * <br>
43  * <p>
44  * </p>
45  * 
46  * @author
47  * @version VFC 1.0 Sep 22, 2016
48  */
49 public class MultivimProxyAdapterMgrService implements IMultivimProxyAdapterMgrService {
50
51     private static final Logger LOG = LoggerFactory.getLogger(MultivimProxyAdapterMgrService.class);
52
53     public static final String RESMGRADAPTERINFO = "resmgradapterinfo.json";
54
55     @Override
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);
61
62         // get multivimproxy info and raise registration
63         try {
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);
69             } else {
70                 LOG.error("Resmgr info is null,please check!");
71             }
72
73         } catch(IOException e) {
74             LOG.error("Failed to read Resmgr info! " + e.getMessage(), e);
75         }
76
77     }
78
79     /**
80      * Retrieve VIM driver information.
81      * 
82      * @return
83      * @throws IOException
84      */
85     public static String readVimAdapterInfoFromJson() throws IOException {
86         InputStream ins = null;
87         BufferedInputStream bins = null;
88         String fileContent = "";
89
90         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
91                 + "etc" + System.getProperty("file.separator") + "adapterInfo" + System.getProperty("file.separator")
92                 + RESMGRADAPTERINFO;
93
94         try {
95             ins = new FileInputStream(fileName);
96             bins = new BufferedInputStream(ins);
97
98             byte[] contentByte = new byte[ins.available()];
99             int num = bins.read(contentByte);
100
101             if(num > 0) {
102                 fileContent = new String(contentByte);
103             }
104         } catch(FileNotFoundException e) {
105             LOG.error(fileName + "is not found!", e);
106         } finally {
107             if(ins != null) {
108                 ins.close();
109             }
110             if(bins != null) {
111                 bins.close();
112             }
113         }
114
115         return fileContent;
116     }
117
118     private static class RegisterMultivimProxyThread implements Runnable {
119
120         // Thread lock Object
121         private final Object lockObject = new Object();
122
123         private IMultivimProxyAdapter2MSBManager adapter2MSBMgr = new MultivimProxyAdapter2MSBManager();
124
125         // url and mothedtype
126         private Map<String, String> paramsMap;
127
128         // driver body
129         private JSONObject adapterInfo;
130
131         public RegisterMultivimProxyThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
132             this.paramsMap = paramsMap;
133             this.adapterInfo = adapterInfo;
134         }
135
136         @Override
137         public void run() {
138             LOG.info("start register resmgr", RegisterMultivimProxyThread.class);
139
140             if(paramsMap == null || adapterInfo == null) {
141                 LOG.error("parameter is null,please check!", RegisterMultivimProxyThread.class);
142                 return;
143             }
144
145             // catch Runtime Exception
146             try {
147                 sendRequest(paramsMap, adapterInfo);
148             } catch(RuntimeException e) {
149                 LOG.error(e.getMessage(), e);
150             }
151
152         }
153
154         private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo) {
155             JSONObject resultObj = adapter2MSBMgr.registerProxy(paramsMap, driverInfo);
156
157             if(Integer.valueOf(resultObj.get("retCode").toString()) == HttpConstant.HTTP_CREATED) {
158                 LOG.info("Resmgr has now Successfully Registered to the Microservice BUS!");
159             } else {
160                 LOG.error("Resmgr failed to  Register to the Microservice BUS! Reason:"
161                         + resultObj.get("reason").toString() + " retCode:" + resultObj.get("retCode").toString());
162
163                 // if registration fails,wait one minute and try again
164                 try {
165                     synchronized(lockObject) {
166                         lockObject.wait(Constant.REPEAT_REG_TIME);
167                     }
168                 } catch(InterruptedException e) {
169                     LOG.error(e.getMessage(), e);
170                 }
171
172                 sendRequest(this.paramsMap, this.adapterInfo);
173             }
174
175         }
176
177     }
178
179     @Override
180     public void unregister() {
181         // unregister
182     }
183
184 }