Fix gvnfm juju compile problem
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / service / api / internalsvc / impl / JujuAdapterMgrService.java
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.vnfm.gvnfm.jujuvnfmadapter.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.gvnfm.jujuvnfmadapter.service.adapter.impl.JujuAdapter2MSBManager;
29 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.adapter.inf.IJujuAdapter2MSBManager;
30 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.api.internalsvc.inf.IJujuAdapterMgrService;
31 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
32 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.UrlConstant;
33 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import net.sf.json.JSONObject;
38
39 /**
40  * Juju adapter manager service class.<br>
41  * <p>
42  * </p>
43  * 
44  * @author
45  * @version NFVO 0.5 Sep 12, 2016
46  */
47 public class JujuAdapterMgrService implements IJujuAdapterMgrService {
48
49     private static final Logger LOG = LoggerFactory.getLogger(JujuAdapterMgrService.class);
50
51     @Override
52     public void register() {
53         // set BUS URL and mothedtype
54         Map<String, String> paramsMap = new HashMap<>();
55         paramsMap.put("url", UrlConstant.REST_MSB_REGISTER);
56         paramsMap.put("methodType", Constant.POST);
57
58         // get juju adapter info and raise registration
59         try {
60             String adapterInfo = readJujuAdapterInfoFromJson();
61             if(!"".equals(adapterInfo)) {
62                 JSONObject adapterObject = JSONObject.fromObject(adapterInfo);
63                 RegisterJujuAdapterThread jujuAdapterThread = new RegisterJujuAdapterThread(paramsMap, adapterObject);
64                 Executors.newSingleThreadExecutor().submit(jujuAdapterThread);
65             } else {
66                 LOG.error("JujuVnfmAdapter info is null,please check!", JujuAdapterMgrService.class);
67             }
68
69         } catch(IOException e) {
70             LOG.error("Failed to read JujuVnfmAdapter info!" + e, JujuAdapterMgrService.class);
71         }
72
73     }
74
75     /**
76      * Read juju adapter information from Json.<br>
77      * 
78      * @return
79      * @throws IOException
80      * @since NFVO 0.5
81      */
82     public static String readJujuAdapterInfoFromJson() throws IOException {
83         InputStream ins = null;
84         BufferedInputStream bins = null;
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) + Constant.JUJUADAPTERINFO;
90
91         try {
92             ins = new FileInputStream(fileName);
93             bins = new BufferedInputStream(ins);
94
95             byte[] contentByte = new byte[ins.available()];
96             int num = bins.read(contentByte);
97
98             if(num > 0) {
99                 fileContent = new String(contentByte);
100             }
101         } catch(FileNotFoundException e) {
102             LOG.error(fileName + "is not found!", e, JujuAdapterMgrService.class);
103         } finally {
104             if(null != ins) {
105                 ins.close();
106             }
107
108             if(null != bins) {
109                 bins.close();
110             }
111         }
112
113         return fileContent;
114     }
115
116     private static class RegisterJujuAdapterThread implements Runnable {
117
118         // Thread lock Object
119         private final Object lockObject = new Object();
120
121         private IJujuAdapter2MSBManager adapter2MSBMgr = new JujuAdapter2MSBManager();
122
123         // url and mothedtype
124         private Map<String, String> paramsMap;
125
126         // driver body
127         private JSONObject adapterInfo;
128
129         public RegisterJujuAdapterThread(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 jujuvnfmadapter", RegisterJujuAdapterThread.class);
137
138             if(paramsMap == null || adapterInfo == null) {
139                 LOG.error("parameter is null,please check!", RegisterJujuAdapterThread.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.registerJujuAdapter(paramsMap, driverInfo);
154
155             if(Integer.valueOf(resultObj.get("retCode").toString()) == Constant.HTTP_CREATED) {
156                 LOG.info("JujuVnfmAdapter has now Successfully Registered to the Microservice BUS!",
157                         JujuAdapterMgrService.class);
158             } else {
159                 LOG.error("JujuVnfmAdapter 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("sendRequest error", e);
169                 }
170
171                 sendRequest(this.paramsMap, this.adapterInfo);
172             }
173
174         }
175
176     }
177
178     @Override
179     public void unregister() {
180         // Not implemented
181
182     }
183
184     /**
185      * Main method.<br>
186      * 
187      * @param args
188      * @since NFVO 0.5
189      */
190     public static void main(String[] args) {
191         LOG.info(SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty(Constant.FILE_SEPARATOR)
192                 + "etc" + System.getProperty(Constant.FILE_SEPARATOR) + "adapterInfo"
193                 + System.getProperty(Constant.FILE_SEPARATOR) + Constant.JUJUADAPTERINFO);
194     }
195
196 }