aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/msb/sdk/discovery/MSBService.java
blob: 94d9fdce7b80918156d0fdd1f92c7f88b788de41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright 2017 ZTE, Inc. and others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package org.onap.msb.sdk.discovery;

import org.apache.commons.lang3.StringUtils;
import org.onap.msb.sdk.discovery.common.RouteConst;
import org.onap.msb.sdk.discovery.common.RouteException;
import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
import org.onap.msb.sdk.discovery.entity.NodeAddress;
import org.onap.msb.sdk.discovery.entity.RouteResult;
import org.onap.msb.sdk.discovery.util.HttpClientUtil;
import org.onap.msb.sdk.discovery.util.JacksonJsonUtil;
import org.onap.msb.sdk.discovery.util.MsbUtil;
import org.onap.msb.sdk.discovery.util.RegExpTestUtil;



public class MSBService {

  /**
   * @Title queryMicroServiceInfo
   * @Description Get information of a service
   * @param msbAddress MSB Address
   * @param serviceName
   * @param version null if no version
   * @throws RouteException
   * @return MicroServiceFullInfo
   */
  public MicroServiceFullInfo queryMicroServiceInfo(String msbAddress, String serviceName,
      String version) throws RouteException {

	  MsbUtil.checkServiceName(serviceName);
	  version=MsbUtil.checkVersion(version);

    MicroServiceFullInfo microServiceInfo = null;


    String apiRouteUrl = (new StringBuilder().append("http://").append(msbAddress)
        .append(RouteConst.MSB_ROUTE_URL).append("/").append(serviceName).append("/version/")
        .append(version).append("?ifPassStatus=true")).toString();

    String resultJson = HttpClientUtil.httpGet(apiRouteUrl);
    microServiceInfo =
        (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);

    return microServiceInfo;
  }


  /**
   * @Title registerMicroServiceInfo
   * @Description
   * @param msbAddress
   * @param microServiceInfo
   * @throws RouteException
   * @return MicroServiceFullInfo
   */
  public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
      MicroServiceInfo microServiceInfo) throws RouteException {
    return this.registerMicroServiceInfo(msbAddress, microServiceInfo, true);

  }

  /**
   * @Title registerMicroServiceInfo
   * @Description
   * @param msbAddress
   * @param microServiceInfo
   * @param createOrUpdate true
   * @throws RouteException
   * @return MicroServiceFullInfo
   */
  public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
      MicroServiceInfo microServiceInfo, boolean createOrUpdate) throws RouteException {

    if (StringUtils.isBlank(microServiceInfo.getServiceName())
        || StringUtils.isBlank(microServiceInfo.getProtocol())
        || microServiceInfo.getNodes().size() == 0) {

      throw new RouteException("register MicroServiceInfo FAIL: Some MicroServiceInfo's required fields are empty","DATA_FORMAT_ERROR");

    }


    if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
      if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
        throw new RouteException("register MicroServiceInfo FAIL:version is not a valid  format","DATA_FORMAT_ERROR");
      }
    }



    if (!RouteConst.checkExistProtocol(microServiceInfo.getProtocol().trim())) {
      throw new RouteException("register MicroServiceInfo FAIL:Protocol is wrong,value range:("+ RouteConst.listProtocol() + ")", "DATA_FORMAT_ERROR");

    }



    String apiRouteJson = JacksonJsonUtil.beanToJson(microServiceInfo);

    String apiRouteUrl =
        (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
            .append("?createOrUpdate=").append(createOrUpdate)).toString();

    String resultJson = HttpClientUtil.httpPostWithJSON(apiRouteUrl, apiRouteJson);

    MicroServiceFullInfo microServiceFullInfo =
        (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);

    return microServiceFullInfo;
  }

  /**
   * @Title cancelMicroServiceInfo
   * @Description deregister a service
   * @param msbAddress
   * @param serviceName
   * @param version
   * @throws RouteException
   * @return RouteResult
   */
  public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version)
      throws RouteException {
    RouteResult result = new RouteResult();

    MsbUtil.checkServiceName(serviceName);
    version=MsbUtil.checkVersion(version);



    String url =
        (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
            .append("/").append(serviceName).append("/version/").append(version).toString();

    HttpClientUtil.delete(url, null);


    result.setResult(RouteConst.REQUEST_SUCCESS);
    result.setInfo("cancel MicroServiceInfo success");


    return result;
  }

  /**
   * @Title cancelMicroServiceInfo
   * @Description deregister a service
   * @param msbAddress
   * @param serviceName
   * @param version
   * @param ip
   * @param port
   * @throws RouteException
   * @return RouteResult
   */
  public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version,
      String ip, String port) throws RouteException {

    RouteResult result = new RouteResult();

    MsbUtil.checkServiceName(serviceName);
    version=MsbUtil.checkVersion(version);

    String url =
        (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
            .append("/").append(serviceName).append("/version/").append(version).append("/nodes/")
            .append(ip).append("/").append(port).toString();

    HttpClientUtil.delete(url, null);


    result.setResult(RouteConst.REQUEST_SUCCESS);
    result.setInfo("cancel MicroServiceInfo success");

    return result;
  }


  /**
   * @Title healthCheckbyTTL
   * @Description
   * @param msbAddress
   * @param serviceName
   * @param version
   * @param ip
   * @param port
   * @throws RouteException
   * @return CheckNode
   */
  public NodeAddress healthCheckbyTTL(String msbAddress, String serviceName, String version,
      String ip, String port) throws RouteException {

	  MsbUtil.checkServiceName(serviceName);
	  version=MsbUtil.checkVersion(version);
	  MsbUtil.checkHost(ip,port);


    NodeAddress checkNode = new NodeAddress(ip, port);


    String healthCheckJson = JacksonJsonUtil.beanToJson(checkNode);

    String healthCheckUrl =
        (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
            .append("/").append(serviceName).append("/version/").append(version).append("/ttl"))
                .toString();

    HttpClientUtil.httpPutWithJSON(healthCheckUrl, healthCheckJson);

    return checkNode;
  }



}