msb protocol synch change
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / serviceListener / MicroServiceChangeListener.java
1 /**
2  * Copyright 2016 ZTE Corporation.
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 package org.openo.msb.wrapper.serviceListener;
17
18 import java.util.Set;
19
20 import org.apache.commons.lang3.StringUtils;
21 import org.openo.msb.api.ApiRouteInfo;
22 import org.openo.msb.api.CustomRouteInfo;
23 import org.openo.msb.api.IuiRouteInfo;
24 import org.openo.msb.api.Node;
25 import org.openo.msb.api.RouteServer;
26 import org.openo.msb.api.Service;
27 import org.openo.msb.wrapper.ApiRouteServiceWrapper;
28 import org.openo.msb.wrapper.CustomRouteServiceWrapper;
29 import org.openo.msb.wrapper.IuiRouteServiceWrapper;
30 import org.openo.msb.wrapper.util.RegExpTestUtil;
31 import org.openo.msb.wrapper.util.RouteUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 public class MicroServiceChangeListener implements IMicroServiceChangeListener {
37
38   private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceChangeListener.class);
39
40   @Override
41   public void onSave(Service microServiceInfo, String serverPort) {
42
43     if ("UI".equals(microServiceInfo.getProtocol())) {
44       IuiRouteInfo iuiRouteInfo = this.buildIuiRouteInfo(microServiceInfo);
45       if (null != iuiRouteInfo) {
46         IuiRouteServiceWrapper.getInstance().saveIuiRouteInstance(iuiRouteInfo);
47       }
48     } else if ("REST".equals(microServiceInfo.getProtocol())) {
49       ApiRouteInfo apiRouteInfo = this.buildApiRouteInfo(microServiceInfo);
50       if (null != apiRouteInfo) {
51         ApiRouteServiceWrapper.getInstance().saveApiRouteInstance(apiRouteInfo, serverPort);
52       }
53     } else if ("HTTP".equals(microServiceInfo.getProtocol())) {
54       CustomRouteInfo customRouteInfo = this.buildCustomRouteInfo(microServiceInfo);
55       if (null != customRouteInfo) {
56         CustomRouteServiceWrapper.getInstance()
57             .saveCustomRouteInstance(customRouteInfo, serverPort);
58       }
59     }
60
61   }
62
63   @Override
64   public void onChange(String serviceName, String version, Service microServiceInfo,
65       String serverPort) {
66
67     if ("UI".equals(microServiceInfo.getProtocol())) {
68       if (serviceName.startsWith("iui_") || serviceName.startsWith("IUI_")) {
69         serviceName = serviceName.substring(4);
70       }
71       IuiRouteInfo iuiRouteInfo = this.buildIuiRouteInfo(microServiceInfo);
72       if (null != iuiRouteInfo) {
73         IuiRouteServiceWrapper.getInstance().updateIuiRouteInstance(serviceName, iuiRouteInfo);
74       }
75     } else if ("REST".equals(microServiceInfo.getProtocol())) {
76       ApiRouteInfo apiRouteInfo = this.buildApiRouteInfo(microServiceInfo);
77       if (null != apiRouteInfo) {
78         ApiRouteServiceWrapper.getInstance().updateApiRouteInstance(serviceName, version,
79             apiRouteInfo, serverPort);
80       }
81     } else if ("HTTP".equals(microServiceInfo.getProtocol())) {
82       if (!serviceName.startsWith("/")) {
83         serviceName = "/" + serviceName;
84       }
85       CustomRouteInfo customRouteInfo = this.buildCustomRouteInfo(microServiceInfo);
86       if (null != customRouteInfo) {
87         CustomRouteServiceWrapper.getInstance().updateCustomRouteInstance(serviceName,
88             customRouteInfo, serverPort);
89       }
90     }
91
92
93
94   }
95
96   @Override
97   public void onStatusChange(String serviceName, String url, String version, String protocol,
98       String status) {
99     if ("UI".equals(protocol)) {
100
101       if (serviceName.startsWith("iui_") || serviceName.startsWith("IUI_")) {
102         serviceName = serviceName.substring(4);
103       }
104       IuiRouteServiceWrapper.getInstance().updateIuiRouteStatus(serviceName, status);
105
106     } else if ("REST".equals(protocol)) {
107       ApiRouteServiceWrapper.getInstance().updateApiRouteStatus(serviceName, version, status);
108     } else if ("HTTP".equals(protocol)) {
109       if (!serviceName.startsWith("/")) {
110         serviceName = "/" + serviceName;
111       }
112       CustomRouteServiceWrapper.getInstance().updateCustomRouteStatus(serviceName, status);
113     }
114
115   }
116
117   @Override
118   public void onDelete(String serviceName, String url, String version, String protocol,
119       String serverPort) {
120
121     if ("UI".equals(protocol)) {
122       if (serviceName.startsWith("iui_") || serviceName.startsWith("IUI_")) {
123         serviceName = serviceName.substring(4);
124       }
125       IuiRouteServiceWrapper.getInstance().deleteIuiRoute(serviceName, "*");
126
127     } else if ("REST".equals(protocol)) {
128       ApiRouteServiceWrapper.getInstance().deleteApiRoute(serviceName, version, "*", serverPort);
129     } else if ("HTTP".equals(protocol)) {
130       if (!serviceName.startsWith("/")) {
131         serviceName = "/" + serviceName;
132       }
133
134       CustomRouteServiceWrapper.getInstance().deleteCustomRoute(serviceName, "*", serverPort);
135     }
136
137   }
138
139
140   /**
141    * @Title ifApiRouteUrl
142    * @Description TODO(According to judge whether the API registration URL format)
143    * @param url
144    * @return
145    * @return boolean
146    */
147   private boolean ifApiRouteUrl(String url) {
148     return RegExpTestUtil.apiRouteUrlRegExpTest(url);
149   }
150
151
152   /**
153    * From MicroServiceInfo to ApiRouteInfo
154    * 
155    * @param microServiceInfo
156    * @return
157    */
158   private ApiRouteInfo buildApiRouteInfo(Service microServiceInfo) {
159
160     ApiRouteInfo apiRouteInfo = new ApiRouteInfo();
161     apiRouteInfo.setUrl(microServiceInfo.getUrl());
162
163     Set<Node> nodes = microServiceInfo.getNodes();
164     RouteServer[] routeServers = new RouteServer[nodes.size()];
165
166
167     int i = 0;
168     for (Node node : nodes) {
169       RouteServer routeServer = new RouteServer(node.getIp(), node.getPort());
170       routeServers[i] = routeServer;
171       i++;
172     }
173
174
175     apiRouteInfo.setServers(routeServers);
176     String[] rangs = StringUtils.split(microServiceInfo.getVisualRange(), "|");
177     if (RouteUtil.contain(rangs, "0")) {
178       apiRouteInfo.setVisualRange("0");
179     } else {
180       apiRouteInfo.setVisualRange("1");
181     }
182
183
184     if ("ip_hash".equals(microServiceInfo.getLb_policy())) {
185       apiRouteInfo.setUseOwnUpstream("1");
186     }
187
188
189
190     apiRouteInfo.setServiceName(microServiceInfo.getServiceName());
191     apiRouteInfo.setVersion(microServiceInfo.getVersion());
192     // TODO:set json and metrics defaultValue
193     String version =
194         "".equals(microServiceInfo.getVersion()) ? "" : "/" + microServiceInfo.getVersion();
195     apiRouteInfo.setApiJson(microServiceInfo.getUrl() + "/swagger.json");
196     apiRouteInfo.setMetricsUrl("/admin/metrics");
197     return apiRouteInfo;
198   }
199
200
201   /**
202    * From MicroServiceInfo to CustomRouteInfo
203    * 
204    * @param microServiceInfo
205    * @return
206    */
207   private CustomRouteInfo buildCustomRouteInfo(Service microServiceInfo) {
208
209     CustomRouteInfo customRouteInfo = new CustomRouteInfo();
210     customRouteInfo.setUrl(microServiceInfo.getUrl());
211
212     Set<Node> nodes = microServiceInfo.getNodes();
213     RouteServer[] routeServers = new RouteServer[nodes.size()];
214
215
216     int i = 0;
217     for (Node node : nodes) {
218       RouteServer routeServer = new RouteServer(node.getIp(), node.getPort());
219       routeServers[i] = routeServer;
220       i++;
221     }
222
223     customRouteInfo.setServers(routeServers);
224     String[] rangs = StringUtils.split(microServiceInfo.getVisualRange(), "|");
225     if (RouteUtil.contain(rangs, "0")) {
226       customRouteInfo.setVisualRange("0");
227     } else {
228       customRouteInfo.setVisualRange("1");
229     }
230
231     if ("ip_hash".equals(microServiceInfo.getLb_policy())) {
232       customRouteInfo.setUseOwnUpstream("1");
233     }
234
235     String serviceName;
236     if (!microServiceInfo.getServiceName().startsWith("/")) {
237       serviceName = "/" + microServiceInfo.getServiceName();
238     } else {
239       serviceName = microServiceInfo.getServiceName();
240     }
241     customRouteInfo.setServiceName(serviceName);
242
243     return customRouteInfo;
244   }
245
246
247   /**
248    * From MicroServiceInfo to IuiRouteInfo
249    * 
250    * @param microServiceInfo
251    * @return
252    */
253   private IuiRouteInfo buildIuiRouteInfo(Service microServiceInfo) {
254
255     IuiRouteInfo iuiRouteInfo = new IuiRouteInfo();
256     iuiRouteInfo.setUrl(microServiceInfo.getUrl());
257
258     Set<Node> nodes = microServiceInfo.getNodes();
259     RouteServer[] routeServers = new RouteServer[nodes.size()];
260
261
262     int i = 0;
263     for (Node node : nodes) {
264       RouteServer routeServer = new RouteServer(node.getIp(), node.getPort());
265       routeServers[i] = routeServer;
266       i++;
267     }
268
269     iuiRouteInfo.setServers(routeServers);
270     String[] rangs = StringUtils.split(microServiceInfo.getVisualRange(), "|");
271     if (RouteUtil.contain(rangs, "0")) {
272       iuiRouteInfo.setVisualRange("0");
273     } else {
274       iuiRouteInfo.setVisualRange("1");
275     }
276
277     if ("ip_hash".equals(microServiceInfo.getLb_policy())) {
278       iuiRouteInfo.setUseOwnUpstream("1");
279     }
280
281     String serviceName = microServiceInfo.getServiceName();
282     if (serviceName.startsWith("iui_") || serviceName.startsWith("IUI_")) {
283       serviceName = serviceName.substring(4);
284     }
285
286
287     iuiRouteInfo.setServiceName(serviceName);
288
289     return iuiRouteInfo;
290   }
291 }