Improve realibility
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / api / internalsvc / impl / VnfmAdapter2DriverMgrService.java
1 /*
2  * Copyright 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.VnfmAdapter2DriverManager;
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.inf.IVnfmAdapter2DriverManager;
31 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.api.internalsvc.inf.IVnfmAdapter2DriverMgrService;
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 VFC 1.0 Jan 23, 2017
46  */
47 public class VnfmAdapter2DriverMgrService implements IVnfmAdapter2DriverMgrService {
48
49     private static final Logger LOG = LoggerFactory.getLogger(VnfmAdapter2DriverMgrService.class);
50
51     public static final String VNFMADAPTER2DRIVERMGR = "vnfmadapter2drivermgr.json";
52
53     @Override
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);
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                 RegisterVnfm2DriverMgrThread vnfmAdapterThread =
66                         new RegisterVnfm2DriverMgrThread(paramsMap, adapterObject);
67                 Executors.newSingleThreadExecutor().submit(vnfmAdapterThread);
68             } else {
69                 LOG.error("vnfmadapter2drivermgr info is null,please check!");
70             }
71
72         } catch(IOException e) {
73             LOG.error("Failed to read vnfmadapter2drivermgr info! " + e.getMessage(), e);
74         }
75
76     }
77
78     /**
79      * Retrieve VIM driver information.
80      *
81      * @return
82      * @throws IOException
83      */
84     public static String readVnfmAdapterInfoFromJson() throws IOException {
85         String fileContent = "";
86
87         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
88                 + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR)
89                 + "adapterInfo" + System.getProperty(Constant.FILE_SEPARATOR) + VNFMADAPTER2DRIVERMGR;
90
91         try (InputStream ins = new FileInputStream(fileName)){
92             try(BufferedInputStream bins = new BufferedInputStream(ins)){
93
94                 byte[] contentByte = new byte[ins.available()];
95                 int num = bins.read(contentByte);
96
97                 if(num > 0) {
98                     fileContent = new String(contentByte);
99                 }
100             }
101         } catch(FileNotFoundException e) {
102             LOG.error(fileName + "is not found!", e);
103         }
104
105         return fileContent;
106     }
107
108     private static class RegisterVnfm2DriverMgrThread implements Runnable {
109
110         private IVnfmAdapter2DriverManager adapter2DriverMgr = new VnfmAdapter2DriverManager();
111
112         // url and mothedtype
113         private Map<String, String> paramsMap;
114
115         // driver body
116         private JSONObject adapterInfo;
117
118         public RegisterVnfm2DriverMgrThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
119             this.paramsMap = paramsMap;
120             this.adapterInfo = adapterInfo;
121         }
122
123         @Override
124         public void run() {
125             LOG.info("start register vnfmadapter to Driver Manager", RegisterVnfm2DriverMgrThread.class);
126
127             if(paramsMap == null || adapterInfo == null) {
128                 LOG.error("parameter is null,please check!", RegisterVnfm2DriverMgrThread.class);
129                 return;
130             }
131
132             // catch Runtime Exception
133             try {
134                 sendRequest(paramsMap, adapterInfo);
135             } catch(RuntimeException e) {
136                 LOG.error(e.getMessage(), e);
137             }
138
139         }
140
141         private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo) {
142             JSONObject resultObj = adapter2DriverMgr.registerDriver(paramsMap, driverInfo);
143
144             if(Integer.valueOf(resultObj.get(Constant.RETCODE).toString()) == Constant.HTTP_CREATED) {
145                 LOG.info("Vnfmadapter has now Successfully Registered to the Driver Manager!");
146             } else {
147                 LOG.error("Vnfmadapter failed to  Register to the Driver Manager! Reason:"
148                         + resultObj.get(Constant.REASON).toString() + " retCode:"
149                         + resultObj.get(Constant.RETCODE).toString());
150
151                 // if registration fails,wait one minute and try again
152                 try {
153                     Thread.sleep(Constant.REPEAT_REG_TIME);
154                 } catch(InterruptedException e) {
155                     LOG.error(e.getMessage(), e);
156                     // Restore interrupted state...
157                     Thread.currentThread().interrupt();
158                 }
159
160                 sendRequest(this.paramsMap, this.adapterInfo);
161             }
162
163         }
164
165     }
166
167     @Override
168     public void unregister() {
169         // unregister
170     }
171
172 }