Remove Chinese comments
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / discovery / MSBService.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.sdk.discovery;
15
16 import org.apache.commons.lang3.StringUtils;
17 import org.onap.msb.sdk.discovery.common.RouteConst;
18 import org.onap.msb.sdk.discovery.common.RouteException;
19 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
20 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
21 import org.onap.msb.sdk.discovery.entity.NodeAddress;
22 import org.onap.msb.sdk.discovery.entity.RouteResult;
23 import org.onap.msb.sdk.discovery.util.HttpClientUtil;
24 import org.onap.msb.sdk.discovery.util.JacksonJsonUtil;
25 import org.onap.msb.sdk.discovery.util.MsbUtil;
26 import org.onap.msb.sdk.discovery.util.RegExpTestUtil;
27
28
29
30 public class MSBService {
31
32   /**
33    * @Title queryMicroServiceInfo
34    * @Description Get information of a service
35    * @param msbAddress MSB Address
36    * @param serviceName
37    * @param version null if no version
38    * @throws RouteException
39    * @return MicroServiceFullInfo
40    */
41   public MicroServiceFullInfo queryMicroServiceInfo(String msbAddress, String serviceName,
42       String version) throws RouteException {
43
44           MsbUtil.checkServiceName(serviceName);
45           version=MsbUtil.checkVersion(version);
46
47     MicroServiceFullInfo microServiceInfo = null;
48
49
50     String apiRouteUrl = (new StringBuilder().append("http://").append(msbAddress)
51         .append(RouteConst.MSB_ROUTE_URL).append("/").append(serviceName).append("/version/")
52         .append(version).append("?ifPassStatus=true")).toString();
53
54     String resultJson = HttpClientUtil.httpGet(apiRouteUrl);
55     microServiceInfo =
56         (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
57
58     return microServiceInfo;
59   }
60
61
62   /**
63    * @Title registerMicroServiceInfo
64    * @Description
65    * @param msbAddress
66    * @param microServiceInfo
67    * @throws RouteException
68    * @return MicroServiceFullInfo
69    */
70   public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
71       MicroServiceInfo microServiceInfo) throws RouteException {
72     return this.registerMicroServiceInfo(msbAddress, microServiceInfo, true);
73
74   }
75
76   /**
77    * @Title registerMicroServiceInfo
78    * @Description
79    * @param msbAddress
80    * @param microServiceInfo
81    * @param createOrUpdate true
82    * @throws RouteException
83    * @return MicroServiceFullInfo
84    */
85   public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
86       MicroServiceInfo microServiceInfo, boolean createOrUpdate) throws RouteException {
87
88     if (StringUtils.isBlank(microServiceInfo.getServiceName())
89         || StringUtils.isBlank(microServiceInfo.getProtocol())
90         || microServiceInfo.getNodes().size() == 0) {
91
92       throw new RouteException("register MicroServiceInfo FAIL: Some MicroServiceInfo's required fields are empty","DATA_FORMAT_ERROR");
93
94     }
95
96
97     if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
98       if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
99         throw new RouteException("register MicroServiceInfo FAIL:version is not a valid  format","DATA_FORMAT_ERROR");
100       }
101     }
102
103
104
105     if (!RouteConst.checkExistProtocol(microServiceInfo.getProtocol().trim())) {
106       throw new RouteException("register MicroServiceInfo FAIL:Protocol is wrong,value range:("+ RouteConst.listProtocol() + ")", "DATA_FORMAT_ERROR");
107
108     }
109
110
111
112     String apiRouteJson = JacksonJsonUtil.beanToJson(microServiceInfo);
113
114     String apiRouteUrl =
115         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
116             .append("?createOrUpdate=").append(createOrUpdate)).toString();
117
118     String resultJson = HttpClientUtil.httpPostWithJSON(apiRouteUrl, apiRouteJson);
119
120     MicroServiceFullInfo microServiceFullInfo =
121         (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
122
123     return microServiceFullInfo;
124   }
125
126   /**
127    * @Title cancelMicroServiceInfo
128    * @Description deregister a service
129    * @param msbAddress
130    * @param serviceName
131    * @param version
132    * @throws RouteException
133    * @return RouteResult
134    */
135   public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version)
136       throws RouteException {
137     RouteResult result = new RouteResult();
138
139     MsbUtil.checkServiceName(serviceName);
140     version=MsbUtil.checkVersion(version);
141
142
143
144     String url =
145         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
146             .append("/").append(serviceName).append("/version/").append(version).toString();
147
148     HttpClientUtil.delete(url, null);
149
150
151     result.setResult(RouteConst.REQUEST_SUCCESS);
152     result.setInfo("cancel MicroServiceInfo success");
153
154
155     return result;
156   }
157
158   /**
159    * @Title cancelMicroServiceInfo
160    * @Description deregister a service
161    * @param msbAddress
162    * @param serviceName
163    * @param version
164    * @param ip
165    * @param port
166    * @throws RouteException
167    * @return RouteResult
168    */
169   public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version,
170       String ip, String port) throws RouteException {
171
172     RouteResult result = new RouteResult();
173
174     MsbUtil.checkServiceName(serviceName);
175     version=MsbUtil.checkVersion(version);
176
177     String url =
178         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
179             .append("/").append(serviceName).append("/version/").append(version).append("/nodes/")
180             .append(ip).append("/").append(port).toString();
181
182     HttpClientUtil.delete(url, null);
183
184
185     result.setResult(RouteConst.REQUEST_SUCCESS);
186     result.setInfo("cancel MicroServiceInfo success");
187
188     return result;
189   }
190
191
192   /**
193    * @Title healthCheckbyTTL
194    * @Description
195    * @param msbAddress
196    * @param serviceName
197    * @param version
198    * @param ip
199    * @param port
200    * @throws RouteException
201    * @return CheckNode
202    */
203   public NodeAddress healthCheckbyTTL(String msbAddress, String serviceName, String version,
204       String ip, String port) throws RouteException {
205
206           MsbUtil.checkServiceName(serviceName);
207           version=MsbUtil.checkVersion(version);
208           MsbUtil.checkHost(ip,port);
209
210
211     NodeAddress checkNode = new NodeAddress(ip, port);
212
213
214     String healthCheckJson = JacksonJsonUtil.beanToJson(checkNode);
215
216     String healthCheckUrl =
217         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
218             .append("/").append(serviceName).append("/version/").append(version).append("/ttl"))
219                 .toString();
220
221     HttpClientUtil.httpPutWithJSON(healthCheckUrl, healthCheckJson);
222
223     return checkNode;
224   }
225
226
227
228 }