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