e754fa27c4b20f86f17cf01d5212ab5bf2d3b8ce
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / api / internalsvc / impl / VnfmAdapterMgrService.java
1 /*
2  * Copyright 2016-2017 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.vnfm.svnfm.vnfmadapter.service.api.internalsvc.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.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;
36
37 import net.sf.json.JSONObject;
38
39 /**
40  * <br/>
41  * <p>
42  * </p>
43  *
44  * @author
45  * @version NFVO 0.5 Aug 31, 2016
46  */
47 public class VnfmAdapterMgrService implements IVnfmAdapterMgrService {
48
49     private static final Logger LOG = LoggerFactory.getLogger(VnfmAdapterMgrService.class);
50
51     public static final String VNFMADAPTERINFO = "vnfmadapterinfo.json";
52
53     @Override
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);
59
60         // get vim adapter info and raise registration
61         try {
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);
67             } else {
68                 LOG.error("VnfmAdapter info is null,please check!");
69             }
70
71         } catch(IOException e) {
72             LOG.error("Failed to read VnfmAdapter info! " + e.getMessage(), e);
73         }
74
75     }
76
77     /**
78      * Retrieve VIM driver information.
79      * 
80      * @return
81      * @throws IOException
82      */
83     public String readVnfmAdapterInfoFromJson() throws IOException {
84         InputStream ins = null;
85         BufferedInputStream bins = null;
86         String fileContent = "";
87
88         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
89                 + "etc" + System.getProperty("file.separator") + "adapterInfo" + System.getProperty("file.separator")
90                 + VNFMADAPTERINFO;
91
92         try {
93             ins = new FileInputStream(fileName);
94             bins = new BufferedInputStream(ins);
95
96             byte[] contentByte = new byte[ins.available()];
97             int num = bins.read(contentByte);
98
99             if(num > 0) {
100                 fileContent = new String(contentByte);
101             }
102         } catch(FileNotFoundException e) {
103             LOG.error(fileName + "is not found!", e);
104         } finally {
105             if(ins != null) {
106                 ins.close();
107             }
108             if(bins != null) {
109                 bins.close();
110             }
111         }
112
113         return fileContent;
114     }
115
116     private static class RegisterVnfmAdapterThread implements Runnable {
117
118         // Thread lock Object
119         private final Object lockObject = new Object();
120
121         private IDriver2MSBManager adapter2MSBMgr = new Driver2MSBManager();
122
123         // url and mothedtype
124         private Map<String, String> paramsMap;
125
126         // driver body
127         private JSONObject adapterInfo;
128
129         public RegisterVnfmAdapterThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
130             this.paramsMap = paramsMap;
131             this.adapterInfo = adapterInfo;
132         }
133
134         @Override
135         public void run() {
136             LOG.info("start register vnfmadapter", RegisterVnfmAdapterThread.class);
137
138             if(paramsMap == null || adapterInfo == null) {
139                 LOG.error("parameter is null,please check!", RegisterVnfmAdapterThread.class);
140                 return;
141             }
142
143             // catch Runtime Exception
144             try {
145                 sendRequest(paramsMap, adapterInfo);
146             } catch(RuntimeException e) {
147                 LOG.error(e.getMessage(), e);
148             }
149
150         }
151
152         private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo) {
153             JSONObject resultObj = adapter2MSBMgr.registerDriver(paramsMap, driverInfo);
154
155             if(Integer.valueOf(resultObj.get("retCode").toString()) == Constant.HTTP_CREATED) {
156                 LOG.info("Vnfmadapter has now Successfully Registered to the Microservice BUS!");
157             } else {
158                 LOG.error("Vnfmadapter failed to  Register to the Microservice BUS! Reason:"
159                         + resultObj.get("reason").toString() + " retCode:" + resultObj.get("retCode").toString());
160
161                 // if registration fails,wait one minute and try again
162                 try {
163                     synchronized(lockObject) {
164                         lockObject.wait(Constant.REPEAT_REG_TIME);
165                     }
166                 } catch(InterruptedException e) {
167                     LOG.error(e.getMessage(), e);
168                 }
169
170                 sendRequest(this.paramsMap, this.adapterInfo);
171             }
172
173         }
174
175     }
176
177     @Override
178     public void unregister() {
179         // unregister
180     }
181
182 }