f75ec066fc47b0d2b50f01b6ff9698ef586a3569
[vfc/nfvo/resmanagement.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.resmanagement.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.resmanagement.common.constant.Constant;
29 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
30 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
31 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
32 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.SystemEnvVariablesFactory;
33 import org.onap.vfc.nfvo.resmanagement.service.adapter.inf.IResmgrAdapter2MSBManager;
34 import org.onap.vfc.nfvo.resmanagement.service.adapter.inf.IResmgrAdapterMgrService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import net.sf.json.JSONObject;
39
40 /**
41  * <br>
42  * <p>
43  * </p>
44  * 
45  * @author
46  * @version NFVO 0.5 Sep 22, 2016
47  */
48 public class ResmgrAdapterMgrService implements IResmgrAdapterMgrService {
49
50     private static final Logger LOG = LoggerFactory.getLogger(ResmgrAdapterMgrService.class);
51
52     public static final String RESMGRADAPTERINFO = "resmgradapterinfo.json";
53
54     @Override
55     public void register() {
56         // set BUS URL and mothedtype
57         Map<String, String> paramsMap = new HashMap<>();
58         paramsMap.put("url", UrlConstant.REST_MSB_REGISTER);
59         paramsMap.put("methodType", ParamConstant.PARAM_POST);
60
61         // get resmgr info and raise registration
62         try {
63             String resmgrInfo = readVimAdapterInfoFromJson();
64             if(!"".equals(resmgrInfo)) {
65                 JSONObject adapterObject = JSONObject.fromObject(resmgrInfo);
66                 RegisterResmgrThread resmgrThread = new RegisterResmgrThread(paramsMap, adapterObject);
67                 Executors.newSingleThreadExecutor().submit(resmgrThread);
68             } else {
69                 LOG.error("Resmgr info is null,please check!");
70             }
71
72         } catch(IOException e) {
73             LOG.error("Failed to read Resmgr 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 readVimAdapterInfoFromJson() throws IOException {
85         InputStream ins = null;
86         BufferedInputStream bins = null;
87         String fileContent = "";
88
89         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
90                 + "etc" + System.getProperty("file.separator") + "adapterInfo" + System.getProperty("file.separator")
91                 + RESMGRADAPTERINFO;
92
93         try {
94             ins = new FileInputStream(fileName);
95             bins = new BufferedInputStream(ins);
96
97             byte[] contentByte = new byte[ins.available()];
98             int num = bins.read(contentByte);
99
100             if(num > 0) {
101                 fileContent = new String(contentByte);
102             }
103         } catch(FileNotFoundException e) {
104             LOG.error(fileName + "is not found!", e);
105         } finally {
106             if(ins != null) {
107                 ins.close();
108             }
109             if(bins != null) {
110                 bins.close();
111             }
112         }
113
114         return fileContent;
115     }
116
117     private static class RegisterResmgrThread implements Runnable {
118
119         // Thread lock Object
120         private final Object lockObject = new Object();
121
122         private IResmgrAdapter2MSBManager adapter2MSBMgr = new ResmgrAdapter2MSBManager();
123
124         // url and mothedtype
125         private Map<String, String> paramsMap;
126
127         // driver body
128         private JSONObject adapterInfo;
129
130         public RegisterResmgrThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
131             this.paramsMap = paramsMap;
132             this.adapterInfo = adapterInfo;
133         }
134
135         @Override
136         public void run() {
137             LOG.info("start register resmgr", RegisterResmgrThread.class);
138
139             if(paramsMap == null || adapterInfo == null) {
140                 LOG.error("parameter is null,please check!", RegisterResmgrThread.class);
141                 return;
142             }
143
144             // catch Runtime Exception
145             try {
146                 sendRequest(paramsMap, adapterInfo);
147             } catch(RuntimeException e) {
148                 LOG.error(e.getMessage(), e);
149             }
150
151         }
152
153         private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo) {
154             JSONObject resultObj = adapter2MSBMgr.registerResmgr(paramsMap, driverInfo);
155
156             if(Integer.valueOf(resultObj.get("retCode").toString()) == HttpConstant.HTTP_CREATED) {
157                 LOG.info("Resmgr has now Successfully Registered to the Microservice BUS!");
158             } else {
159                 LOG.error("Resmgr failed to  Register to the Microservice BUS! Reason:"
160                         + resultObj.get("reason").toString() + " retCode:" + resultObj.get("retCode").toString());
161
162                 // if registration fails,wait one minute and try again
163                 try {
164                     synchronized(lockObject) {
165                         lockObject.wait(Constant.REPEAT_REG_TIME);
166                     }
167                 } catch(InterruptedException e) {
168                     LOG.error(e.getMessage(), e);
169                 }
170
171                 sendRequest(this.paramsMap, this.adapterInfo);
172             }
173
174         }
175
176     }
177
178     @Override
179     public void unregister() {
180         // unregister
181     }
182
183 }