1e0ef33548570b19b9b4e5723e4c71921c02ec86
[vfc/nfvo/wfengine.git] / common-util / src / main / java / org / openo / baseservice / bus / util / RegisterService.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.openo.baseservice.bus.util;
18
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Properties;
24
25 import javax.ws.rs.core.Response;
26
27 import org.apache.cxf.jaxrs.client.WebClient;
28 import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
29 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 /**
35  * Provide the service register cbb for common use.
36  * <br/>
37  * <p>
38  * </p>
39  * 
40  * @author
41  * @version  
42  */
43 public class RegisterService {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(RegisterService.class);
46
47     private static final String BUS_CONFIGURE_FILE = "/etc/microservice.ini";
48     
49     private static final String BUS_SERVICE_URL = "/openoapi/microservices/v1/services";
50
51     private static final String BUS_ADDRESS_KEY = "msb.address";
52     
53     private static String busPath = null;
54
55     /**
56      * Constructor<br/>
57      * <p>
58      * </p>
59      * @throws IOException 
60      * 
61      * @since  
62      */
63     private RegisterService() {
64     }
65
66     /**
67      * register the micro service.
68      * <br/>
69      * 
70      * @param jsonPath: the service json object to register to the bus.
71      * @param createOrUpdate: true, create and update the old ip port. false, create and delete the
72      *            old one;
73      * @return
74      * @throws IOException
75      * @since  
76      */
77     public static Response registerService(String jsonPath, boolean createOrUpdate) throws IOException {
78         
79         String serviceInfo = getServiceModel(jsonPath);
80         
81         WebClient client = initializeClient();
82         
83         client.type("application/json");
84         
85         client.accept("application/json");
86         
87         client.path(BUS_SERVICE_URL);
88         
89         client.query("createOrUpdate", createOrUpdate);
90
91         LOGGER.info("Connecting bus address : " + busPath + BUS_SERVICE_URL);
92         
93         return client.invoke("POST", serviceInfo);
94         
95     }
96
97     /**
98      * get the service's model. and return it as a string ;
99      * <br/>
100      * 
101      * @param jsonPath
102      * @return
103      * @since   
104      */
105     private static String getServiceModel(String jsonPath) {
106         
107         String serviceInfo = "";
108         
109         try {
110             LOGGER.info("begin to read file micro service json " + jsonPath);
111             
112             FileInputStream busFile = new FileInputStream(jsonPath);
113             
114             int size = busFile.available();
115             
116             byte[] buffer = new byte[size];
117             
118             busFile.read(buffer);
119             
120             busFile.close();
121             
122             serviceInfo = new String(buffer);
123             LOGGER.info("finished to read micro service json file. ");
124         } catch(Exception ex) {
125             LOGGER.error("Read the micro service json file error :", ex);
126         }
127         return serviceInfo;
128     }
129     /**
130      * initialize the bus ip and port.
131      * <br/>
132      * 
133      * @return
134      * @throws IOException
135      * @since   
136      */
137     private static String getBusAdderss() throws IOException {
138
139         LOGGER.info("begin to get the bus baseurl.");
140         FileInputStream busFile = null;
141         String url = "msb.openo.org:80";
142
143         String filePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + BUS_CONFIGURE_FILE;
144         LOGGER.info("bus base url file:" + filePath);
145         
146         Properties properties = new Properties();
147
148         try {
149             busFile = new FileInputStream(filePath);
150             properties.load(busFile);
151             url = properties.getProperty(BUS_ADDRESS_KEY);
152         } catch(IOException e) {
153             if (busFile != null) {
154                 busFile.close();
155             }
156             LOGGER.error("Read the bus url failed: ", e);
157         }
158
159         LOGGER.info("initialize the bus baseurl is: " + url);
160         return "http://" + url;
161     }
162     
163     /**
164      * get the bus's client's address. and initialize the web client.
165      * <br/>
166      * 
167      * @return
168      * @throws IOException 
169      * @since   
170      */
171     private static WebClient initializeClient() throws IOException {
172         
173         final List<Object> providers = new ArrayList<Object>();
174
175         JacksonJsonProvider jacksonJsonProvider = new JacksonJsonProvider();
176
177         providers.add(jacksonJsonProvider);
178
179         if (busPath == null) {
180             busPath = getBusAdderss();
181         }
182
183         return WebClient.create(busPath, providers);
184     }
185 }