diff options
Diffstat (limited to 'apiroute/apiroute-service/src/main')
3 files changed, 285 insertions, 48 deletions
diff --git a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/api/MicroServiceFullInfo.java b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/api/MicroServiceFullInfo.java index a5c5bf0..2bade46 100644 --- a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/api/MicroServiceFullInfo.java +++ b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/api/MicroServiceFullInfo.java @@ -27,7 +27,7 @@ public class MicroServiceFullInfo implements Serializable { @ApiModelProperty(required = true) - private String serviceName; + private String serviceName = ""; @ApiModelProperty(example = "v1") private String version = ""; diff --git a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/resources/MicroServiceResource.java b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/resources/MicroServiceResource.java index be33613..7ade6fa 100644 --- a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/resources/MicroServiceResource.java +++ b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/resources/MicroServiceResource.java @@ -13,8 +13,7 @@ ******************************************************************************/ package org.onap.msb.apiroute.resources; -import java.net.URI; -import java.util.List; +import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DELETE; @@ -32,12 +31,22 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.onap.msb.apiroute.api.DiscoverInfo; import org.onap.msb.apiroute.api.MicroServiceFullInfo; import org.onap.msb.apiroute.api.exception.ExtendedInternalServerErrorException; import org.onap.msb.apiroute.health.ConsulLinkHealthCheck; import org.onap.msb.apiroute.health.RedisHealthCheck; -import org.onap.msb.apiroute.wrapper.MicroServiceWrapper; -import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil; +import org.onap.msb.apiroute.wrapper.util.ConfigUtil; +import org.onap.msb.apiroute.wrapper.util.HttpClientUtil; +import org.onap.msb.apiroute.wrapper.util.RouteUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +62,6 @@ import io.swagger.annotations.ApiResponses; // @Api(tags = {"MSB-Service Resource"}) @Produces(MediaType.APPLICATION_JSON) public class MicroServiceResource { - private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceResource.class); @Context @@ -67,11 +75,12 @@ public class MicroServiceResource { message = "get microservice List fail", response = String.class)}) @Produces(MediaType.APPLICATION_JSON) @Timed - public List<MicroServiceFullInfo> getMicroService() { - return MicroServiceWrapper.getInstance().getAllMicroServiceInstances(); + public Response getMicroService() { + String resourceUrl = "/"; + return routeHttpGet2DiscoveryService(resourceUrl); } - /*@POST + @POST @Path("/") @ApiOperation(value = "add one microservice ", code = HttpStatus.SC_CREATED, response = MicroServiceFullInfo.class) @ApiResponses(value = { @@ -89,20 +98,11 @@ public class MicroServiceResource { @Context HttpServletRequest request, @ApiParam(value = "createOrUpdate", required = false) @QueryParam("createOrUpdate") @DefaultValue("true") boolean createOrUpdate, - @ApiParam(value = "port", required = false) @QueryParam("port") @DefaultValue("") String port) { - - String ip = MicroServiceUtil.getRealIp(request); - - MicroServiceFullInfo microServiceFullInfo = MicroServiceWrapper.getInstance() - .saveMicroServiceInstance(microServiceInfo, createOrUpdate, ip, port); - URI returnURI = uriInfo.getAbsolutePathBuilder() - .path("/" + microServiceInfo.getServiceName() + "/version/" + microServiceInfo.getVersion()) - .build(); - return Response.created(returnURI).entity(microServiceFullInfo).build(); + @ApiParam(value = "port", required = false) @QueryParam("port") @DefaultValue("") String port) + throws Exception { + return routeRegistration2DiscoveryService(microServiceInfo); } - - @GET @Path("/{serviceName}/version/{version}") @ApiOperation(value = "get one microservice ", code = HttpStatus.SC_OK, response = MicroServiceFullInfo.class) @@ -115,14 +115,11 @@ public class MicroServiceResource { response = String.class)}) @Produces(MediaType.APPLICATION_JSON) @Timed - public MicroServiceFullInfo getMicroService( + public Response getMicroService( @ApiParam(value = "microservice serviceName") @PathParam("serviceName") String serviceName, @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"") @PathParam("version") @DefaultValue("") String version) { - - - return MicroServiceWrapper.getInstance().getMicroServiceInstance(serviceName, version); - - + String resourceUrl = new StringBuilder("/").append(serviceName).append("/version/").append(version).toString(); + return routeHttpGet2DiscoveryService(resourceUrl); } @PUT @@ -145,15 +142,10 @@ public class MicroServiceResource { required = true) MicroServiceFullInfo microServiceInfo, @Context HttpServletRequest request) { - String ip = MicroServiceUtil.getRealIp(request); - MicroServiceFullInfo microServiceFullInfo = - MicroServiceWrapper.getInstance().saveMicroServiceInstance(microServiceInfo, false, ip, ""); - return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(microServiceFullInfo).build(); - + String resourceUrl = new StringBuilder("/").append(serviceName).append("/version/").append(version).toString(); + return routeHttpPut2DiscoveryService(resourceUrl, microServiceInfo); } - - @DELETE @Path("/{serviceName}/version/{version}/nodes/{ip}/{port}") @ApiOperation(value = "delete single node by serviceName and version and node", code = HttpStatus.SC_NO_CONTENT) @@ -165,7 +157,7 @@ public class MicroServiceResource { response = String.class)}) @Produces(MediaType.APPLICATION_JSON) @Timed - public void deleteNode( + public Response deleteNode( @ApiParam(value = "microservice serviceName", required = true) @PathParam("serviceName") String serviceName, @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"", @@ -173,11 +165,11 @@ public class MicroServiceResource { @ApiParam(value = "ip") @PathParam("ip") String ip, @ApiParam(value = "port") @PathParam("port") String port) { - MicroServiceWrapper.getInstance().deleteMicroServiceInstance(serviceName, version, ip, port); - + String resourceUrl = new StringBuilder("/").append(serviceName).append("/version/").append(version) + .append("/nodes/").append(ip).append("/").append(port).toString(); + return routeHttpDelete2DiscoveryService(resourceUrl); } - @DELETE @Path("/{serviceName}/version/{version}") @ApiOperation(value = "delete one full microservice by serviceName and version", code = HttpStatus.SC_NO_CONTENT) @@ -190,14 +182,13 @@ public class MicroServiceResource { response = String.class)}) @Produces(MediaType.APPLICATION_JSON) @Timed - public void deleteMicroService( + public Response deleteMicroService( @ApiParam(value = "microservice serviceName", required = true) @PathParam("serviceName") String serviceName, @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"", required = false) @PathParam("version") @DefaultValue("") String version) { - - MicroServiceWrapper.getInstance().deleteMicroService(serviceName, version); - + String resourceUrl = new StringBuilder("/").append(serviceName).append("/version/").append(version).toString(); + return routeHttpDelete2DiscoveryService(resourceUrl); } @PUT @@ -219,14 +210,139 @@ public class MicroServiceResource { @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"", required = false) @PathParam("version") @DefaultValue("") String version, @ApiParam(value = "status,1:abled 0:disabled") @PathParam("status") String status) { + String resourceUrl = new StringBuilder("/").append(serviceName).append("/version/").append(version) + .append("/status/").append(status).toString(); + return this.routeHttpPut2DiscoveryService(resourceUrl); + } - MicroServiceFullInfo microServiceFullInfo = - MicroServiceWrapper.getInstance().updateMicroServiceStatus(serviceName, version, status); + /** + * @param discoveryUrl + * @return + */ + private Response routeHttpDelete2DiscoveryService(String resourceUrl) { + String discoveryServiceUrl = getDiscoveryServiceUrl(); + String fullUrl = new StringBuilder(discoveryServiceUrl).append(resourceUrl).toString(); + CloseableHttpClient httpClient = null; + CloseableHttpResponse httpResponse = null; + try { + httpClient = HttpClients.createDefault(); + HttpDelete httpDelete = new HttpDelete(fullUrl); + httpResponse = httpClient.execute(httpDelete); + return cloneHttpResponse(httpResponse); + } catch (Exception e) { + throw new ExtendedInternalServerErrorException(e.getMessage()); + } finally { + HttpClientUtil.closeHttpClient(httpClient, httpResponse); + } + } - return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(microServiceFullInfo).build(); + /** + * @param discoveryUrl + * @return + */ + private Response routeHttpPut2DiscoveryService(String resourceUrl, Object bean) { + String discoveryServiceUrl = getDiscoveryServiceUrl(); + String fullUrl = new StringBuilder(discoveryServiceUrl).append(resourceUrl).toString(); + CloseableHttpClient httpClient = null; + CloseableHttpResponse httpResponse = null; + try { + HttpPut httpPut = HttpClientUtil.createHttpPut(fullUrl, bean); + httpClient = HttpClients.createDefault(); + httpResponse = httpClient.execute(httpPut); + return cloneHttpResponse(httpResponse); + } catch (Exception e) { + throw new ExtendedInternalServerErrorException(e.getMessage()); + } finally { + HttpClientUtil.closeHttpClient(httpClient, httpResponse); + } + } + /** + * @param discoveryUrl + * @return + */ + private Response routeHttpPut2DiscoveryService(String resourceUrl) { + String discoveryServiceUrl = getDiscoveryServiceUrl(); + String fullUrl = new StringBuilder(discoveryServiceUrl).append(resourceUrl).toString(); + CloseableHttpClient httpClient = null; + CloseableHttpResponse httpResponse = null; + try { + HttpPut httpPut = HttpClientUtil.createHttpPut(fullUrl); + httpClient = HttpClients.createDefault(); + httpResponse = httpClient.execute(httpPut); + return cloneHttpResponse(httpResponse); + } catch (Exception e) { + throw new ExtendedInternalServerErrorException(e.getMessage()); + } finally { + HttpClientUtil.closeHttpClient(httpClient, httpResponse); + } } + /** + * @param discoveryUrl + * @return + */ + private Response routeHttpGet2DiscoveryService(String resourceUrl) { + String discoveryServiceUrl = getDiscoveryServiceUrl(); + String fullUrl = new StringBuilder(discoveryServiceUrl).append(resourceUrl).toString(); + CloseableHttpClient httpClient = null; + CloseableHttpResponse httpResponse = null; + try { + HttpGet httpGet = HttpClientUtil.createHttpGet(fullUrl); + httpClient = HttpClients.createDefault(); + httpResponse = httpClient.execute(httpGet); + return cloneHttpResponse(httpResponse); + } catch (Exception e) { + throw new ExtendedInternalServerErrorException(e.getMessage()); + } finally { + HttpClientUtil.closeHttpClient(httpClient, httpResponse); + } + } + + /** + * @param microServiceFullInfo + * @param registrationUrl + * @return + */ + private Response routeRegistration2DiscoveryService(MicroServiceFullInfo microServiceFullInfo) { + String registrationUrl = getDiscoveryServiceUrl(); + CloseableHttpClient httpClient = null; + CloseableHttpResponse httpResponse = null; + try { + HttpPost httpPost = HttpClientUtil.createHttpPost(registrationUrl, microServiceFullInfo); + httpClient = HttpClients.createDefault(); + httpResponse = httpClient.execute(httpPost); + return cloneHttpResponse(httpResponse); + } catch (Exception e) { + throw new ExtendedInternalServerErrorException(e.getMessage()); + } finally { + HttpClientUtil.closeHttpClient(httpClient, httpResponse); + } + } + + /** + * @param httpResponse + * @return + * @throws IOException + */ + private Response cloneHttpResponse(CloseableHttpResponse httpResponse) throws IOException { + String jsonString = EntityUtils.toString(httpResponse.getEntity()); + Response response = Response.status(httpResponse.getStatusLine().getStatusCode()).entity(jsonString).build(); + return response; + } + + /** + * @return + */ + private String getDiscoveryServiceUrl() { + DiscoverInfo discoverInfo = ConfigUtil.getInstance().getDiscoverInfo(); + String registrationUrl = new StringBuilder().append("http://").append(discoverInfo) + .append(RouteUtil.MSB_ROUTE_URL).toString(); + return registrationUrl; + } + + // Discovery service doesn't provide this API, so requests to this URL are not routed to + // discovery service @GET @Path("/health") @ApiOperation(value = "apigateway healthy check ", code = HttpStatus.SC_OK, response = String.class) @@ -251,7 +367,5 @@ public class MicroServiceResource { } return Response.ok("apigateway healthy check:ok").build(); - }*/ - - + } } diff --git a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java index cb80159..8e28fa1 100644 --- a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java +++ b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java @@ -14,13 +14,25 @@ package org.onap.msb.apiroute.wrapper.util; import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import org.apache.http.HttpMessage; +import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,6 +43,117 @@ public class HttpClientUtil { private static int connectionTimeOut = 2 * 1000; + public static HttpPost createHttpPost(String url, Object bean) throws Exception { + HttpPost httpPost = new HttpPost(url); + setCommonHeader(httpPost); + setStringEntity(httpPost, bean); + return httpPost; + } + + public static HttpGet createHttpGet(String url) { + HttpGet httpGet = new HttpGet(url); + setCommonHeader(httpGet); + return httpGet; + } + + public static HttpPut createHttpPut(String url, Object bean) throws Exception { + HttpPut httpPut = new HttpPut(url); + setCommonHeader(httpPut); + setStringEntity(httpPut, bean); + return httpPut; + } + + public static HttpPut createHttpPut(String url) throws Exception { + HttpPut httpPut = new HttpPut(url); + setCommonHeader(httpPut); + return httpPut; + } + + private static void setCommonHeader(HttpMessage httpMessage) { + httpMessage.addHeader("Content-type", "application/json; charset=utf-8"); + httpMessage.setHeader("Accept", "application/json"); + } + + private static void setStringEntity(HttpEntityEnclosingRequestBase httpMessage, Object bean) throws Exception { + String entity = JacksonJsonUtil.beanToJson(bean); + httpMessage.setEntity(new StringEntity(entity, Charset.forName("UTF-8"))); + } + + public static void closeHttpClient(CloseableHttpClient httpClient, CloseableHttpResponse response) { + closeHttpClient(httpClient); + closeHttpResponse(response); + } + + private static void closeHttpClient(CloseableHttpClient httpClient) { + if (httpClient != null) { + try { + httpClient.close(); + } catch (IOException e) { + logger.error(httpClient + ":close httpClient faild"); + } + } + } + + private static void closeHttpResponse(CloseableHttpResponse response) { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + logger.error(response + ":close response faild"); + } + } + } + + public static CloseableHttpResponse httpGetWithResponse(String url) throws Exception { + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader("Content-type", "application/json; charset=utf-8"); + httpGet.setHeader("Accept", "application/json"); + try { + return httpClient.execute(httpGet); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + logger.error(url + ":close httpClient faild"); + } + } + } + + public static void delete(String url, String parameter) throws Exception { + String result = null; + String baseUrl; + if (parameter != null) { + List<NameValuePair> params = new ArrayList<NameValuePair>(); + params.add(new BasicNameValuePair("serviceName", parameter)); + baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8"); + } else { + baseUrl = url; + } + + CloseableHttpClient httpClient = HttpClients.createDefault();; + try { + + HttpDelete httpDelete = new HttpDelete(baseUrl); + CloseableHttpResponse res = httpClient.execute(httpDelete); + + if (res.getStatusLine().getStatusCode() != 200) { + throw new Exception("delete fail"); + } + + res.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":delete connect faild"; + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":close httpClient faild"; + } + } + + } + public static String httpGet(String url) { String result = null; |