1946201d66f1e12530b5c5d211def3e8e8f826b3
[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.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.onap.vfc.nfvo.resmanagement.common.constant.Constant;
31 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
32 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
33 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
34 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.SystemEnvVariablesFactory;
35 import org.onap.vfc.nfvo.resmanagement.service.adapter.inf.IResmgrAdapter2MSBManager;
36 import org.onap.vfc.nfvo.resmanagement.service.adapter.inf.IResmgrAdapterMgrService;
37
38 import net.sf.json.JSONObject;
39
40 /**
41  * <br>
42  * <p>
43  * </p>
44  *
45  * @author
46  * @version VFC 1.0 Sep 22, 2016
47  */
48 public class ResmgrAdapterMgrService implements IResmgrAdapterMgrService {
49
50     private static final Logger LOG = LogManager.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         String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty("file.separator")
86                 + "etc" + System.getProperty("file.separator") + "adapterInfo" + System.getProperty("file.separator")
87                 + RESMGRADAPTERINFO;
88
89         return readJson(fileName);
90     }
91
92     public static String readJson(String fileName) throws IOException {
93         String fileContent = "";
94
95         try (InputStream ins = new FileInputStream(fileName)){
96             try(BufferedInputStream 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             }
105         } catch(FileNotFoundException e) {
106             LOG.error(fileName + "is not found!", e);
107         }
108
109         return fileContent;
110     }
111
112
113     private static class RegisterResmgrThread implements Runnable {
114
115         private IResmgrAdapter2MSBManager adapter2MSBMgr = new ResmgrAdapter2MSBManager();
116
117         // url and mothedtype
118         private Map<String, String> paramsMap;
119
120         // driver body
121         private JSONObject adapterInfo;
122
123         public RegisterResmgrThread(Map<String, String> paramsMap, JSONObject adapterInfo) {
124             this.paramsMap = paramsMap;
125             this.adapterInfo = adapterInfo;
126         }
127
128         @Override
129         public void run() {
130             LOG.info("start register resmgr", RegisterResmgrThread.class);
131
132             if(paramsMap == null || adapterInfo == null) {
133                 LOG.error("parameter is null,please check!", RegisterResmgrThread.class);
134                 return;
135             }
136
137             // catch Runtime Exception
138             try {
139                 sendRequest(paramsMap, adapterInfo);
140             } catch(RuntimeException e) {
141                 LOG.error(e.getMessage(), e);
142             }
143
144         }
145
146         private void sendRequest(Map<String, String> paramsMap, JSONObject driverInfo) {
147             JSONObject resultObj = adapter2MSBMgr.registerResmgr(paramsMap, driverInfo);
148
149             if(Integer.valueOf(resultObj.get("retCode").toString()) == HttpConstant.HTTP_CREATED) {
150                 LOG.info("Resmgr has now Successfully Registered to the Microservice BUS!");
151             } else {
152                 LOG.error("Resmgr failed to  Register to the Microservice BUS! Reason:"
153                         + resultObj.get("reason").toString() + " retCode:" + resultObj.get("retCode").toString());
154
155                 // if registration fails,wait one minute and try again
156                 try {
157                     Thread.sleep(Constant.REPEAT_REG_TIME);
158                 } catch(InterruptedException e) {
159                     LOG.error(e.getMessage(), e);
160                     // Restore interrupted state...
161                     Thread.currentThread().interrupt();
162                 }
163
164                 sendRequest(this.paramsMap, this.adapterInfo);
165             }
166
167         }
168
169     }
170
171     @Override
172     public void unregister() {
173         // unregister
174     }
175
176 }