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