2 * Copyright 2016 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.msb.apiroute.resources;
19 import io.swagger.annotations.Api;
20 import io.swagger.annotations.ApiOperation;
21 import io.swagger.annotations.ApiParam;
22 import io.swagger.annotations.ApiResponse;
23 import io.swagger.annotations.ApiResponses;
26 import java.util.List;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.DefaultValue;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriInfo;
43 import org.apache.http.HttpStatus;
44 import org.onap.msb.apiroute.api.MicroServiceFullInfo;
45 import org.onap.msb.apiroute.api.exception.ExtendedInternalServerErrorException;
46 import org.onap.msb.apiroute.health.ConsulLinkHealthCheck;
47 import org.onap.msb.apiroute.health.RedisHealthCheck;
48 import org.onap.msb.apiroute.wrapper.MicroServiceWrapper;
49 import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 import com.codahale.metrics.annotation.Timed;
54 import com.codahale.metrics.health.HealthCheck.Result;
57 // @Api(tags = {"MSB-Service Resource"})
58 @Produces(MediaType.APPLICATION_JSON)
59 public class MicroServiceResource {
61 private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceResource.class);
64 UriInfo uriInfo; // actual uri info
68 @ApiOperation(value = "get all microservices ", code = HttpStatus.SC_OK, response = MicroServiceFullInfo.class, responseContainer = "List")
69 @ApiResponses(value = {@ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "get microservice List fail", response = String.class)})
70 @Produces(MediaType.APPLICATION_JSON)
72 public List<MicroServiceFullInfo> getMicroService() {
73 return MicroServiceWrapper.getInstance().getAllMicroServiceInstances();
78 @ApiOperation(value = "add one microservice ", code = HttpStatus.SC_CREATED, response = MicroServiceFullInfo.class)
79 @ApiResponses(value = {
80 @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
81 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "add microservice fail", response = String.class),
82 @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "Unprocessable MicroServiceInfo JSON REQUEST", response = String.class)})
83 @Produces(MediaType.APPLICATION_JSON)
85 public Response addMicroService(
86 @ApiParam(value = "MicroServiceInfo Instance Info", required = true) MicroServiceFullInfo microServiceInfo,
87 @Context HttpServletRequest request,
88 @ApiParam(value = "createOrUpdate", required = false) @QueryParam("createOrUpdate") @DefaultValue("true") boolean createOrUpdate,
89 @ApiParam(value = "port", required = false) @QueryParam("port") @DefaultValue("") String port) {
91 String ip=MicroServiceUtil.getRealIp(request);
93 MicroServiceFullInfo microServiceFullInfo =MicroServiceWrapper.getInstance().saveMicroServiceInstance(microServiceInfo,createOrUpdate,ip,port);
94 URI returnURI =uriInfo.getAbsolutePathBuilder().path("/" + microServiceInfo.getServiceName() + "/version/"+ microServiceInfo.getVersion()).build();
95 return Response.created(returnURI).entity(microServiceFullInfo).build();
101 @Path("/{serviceName}/version/{version}")
102 @ApiOperation(value = "get one microservice ", code = HttpStatus.SC_OK, response = MicroServiceFullInfo.class)
103 @ApiResponses(value = {
104 @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "microservice not found", response = String.class),
105 @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
106 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "get microservice fail", response = String.class)})
107 @Produces(MediaType.APPLICATION_JSON)
109 public MicroServiceFullInfo getMicroService(
110 @ApiParam(value = "microservice serviceName") @PathParam("serviceName") String serviceName,
111 @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"") @PathParam("version") @DefaultValue("") String version) {
114 return MicroServiceWrapper.getInstance().getMicroServiceInstance(serviceName, version);
120 @Path("/{serviceName}/version/{version}")
121 @ApiOperation(value = "update one microservice by serviceName and version", code = HttpStatus.SC_CREATED, response = MicroServiceFullInfo.class)
122 @ApiResponses(value = {
123 @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
124 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "update microservice fail", response = String.class),
125 @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "Unprocessable MicroServiceInfo JSON REQUEST", response = String.class)})
126 @Produces(MediaType.APPLICATION_JSON)
128 public Response updateMicroService(
129 @ApiParam(value = "microservice serviceName") @PathParam("serviceName") String serviceName,
130 @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"") @PathParam("version") @DefaultValue("") String version,
131 @ApiParam(value = "microservice Instance Info", required = true) MicroServiceFullInfo microServiceInfo,
132 @Context HttpServletRequest request) {
134 String ip=MicroServiceUtil.getRealIp(request);
135 MicroServiceFullInfo microServiceFullInfo = MicroServiceWrapper.getInstance().saveMicroServiceInstance(microServiceInfo,
137 return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(microServiceFullInfo).build();
145 @Path("/{serviceName}/version/{version}/nodes/{ip}/{port}")
146 @ApiOperation(value = "delete single node by serviceName and version and node", code = HttpStatus.SC_NO_CONTENT)
147 @ApiResponses(value = {
148 @ApiResponse(code = HttpStatus.SC_NO_CONTENT, message = "delete node succeed "),
149 @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "node not found", response = String.class),
150 @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
151 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "delete node fail", response = String.class)})
152 @Produces(MediaType.APPLICATION_JSON)
154 public void deleteNode(
155 @ApiParam(value = "microservice serviceName", required = true) @PathParam("serviceName") String serviceName,
156 @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"", required = false) @PathParam("version") @DefaultValue("") String version,
157 @ApiParam(value = "ip") @PathParam("ip") String ip,
158 @ApiParam(value = "port") @PathParam("port") String port) {
160 MicroServiceWrapper.getInstance().deleteMicroServiceInstance(serviceName, version, ip,port);
166 @Path("/{serviceName}/version/{version}")
167 @ApiOperation(value = "delete one full microservice by serviceName and version", code = HttpStatus.SC_NO_CONTENT)
168 @ApiResponses(value = {
169 @ApiResponse(code = HttpStatus.SC_NO_CONTENT, message = "delete microservice succeed "),
170 @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "microservice not found", response = String.class),
171 @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
172 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "delete microservice fail", response = String.class)})
173 @Produces(MediaType.APPLICATION_JSON)
175 public void deleteMicroService(
176 @ApiParam(value = "microservice serviceName", required = true) @PathParam("serviceName") String serviceName,
177 @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"", required = false) @PathParam("version") @DefaultValue("") String version) {
179 MicroServiceWrapper.getInstance().deleteMicroService(serviceName, version);
184 @Path("/{serviceName}/version/{version}/status/{status}")
185 @ApiOperation(value = "update microservice status by serviceName and version", code = HttpStatus.SC_CREATED, response = MicroServiceFullInfo.class)
186 @ApiResponses(value = {
187 @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
188 @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "microservice not found", response = String.class),
189 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "update status fail", response = String.class)})
190 @Produces(MediaType.APPLICATION_JSON)
192 public Response updateServiceStatus(
193 @ApiParam(value = "microservice serviceName", required = true) @PathParam("serviceName") String serviceName,
194 @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"", required = false) @PathParam("version") @DefaultValue("") String version,
195 @ApiParam(value = "status,1:abled 0:disabled") @PathParam("status") String status) {
197 MicroServiceFullInfo microServiceFullInfo = MicroServiceWrapper.getInstance().updateMicroServiceStatus(serviceName, version,status);
199 return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(microServiceFullInfo).build();
205 @ApiOperation(value = "apigateway healthy check ", code = HttpStatus.SC_OK, response = String.class)
206 @ApiResponses(value = { @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "check fail", response = String.class) })
207 @Produces(MediaType.TEXT_PLAIN)
209 public Response health() {
212 Result rst = RedisHealthCheck.getResult();
213 if (!rst.isHealthy()) {
214 LOGGER.warn("health check failed:"+rst.getMessage());
215 throw new ExtendedInternalServerErrorException(rst.getMessage());
219 rst = ConsulLinkHealthCheck.getResult();
220 if (!rst.isHealthy()) {
221 LOGGER.warn("health check failed:"+rst.getMessage());
222 throw new ExtendedInternalServerErrorException(rst.getMessage());
225 return Response.ok("apigateway healthy check:ok").build();