Remove service registration and discovery REST api
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / resources / MicroServiceResource.java
1 /*******************************************************************************
2  * Copyright 2016-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.apiroute.resources;
15
16 import java.net.URI;
17 import java.util.List;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.ws.rs.DELETE;
21 import javax.ws.rs.DefaultValue;
22 import javax.ws.rs.GET;
23 import javax.ws.rs.POST;
24 import javax.ws.rs.PUT;
25 import javax.ws.rs.Path;
26 import javax.ws.rs.PathParam;
27 import javax.ws.rs.Produces;
28 import javax.ws.rs.QueryParam;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.UriInfo;
33
34 import org.apache.http.HttpStatus;
35 import org.onap.msb.apiroute.api.MicroServiceFullInfo;
36 import org.onap.msb.apiroute.api.exception.ExtendedInternalServerErrorException;
37 import org.onap.msb.apiroute.health.ConsulLinkHealthCheck;
38 import org.onap.msb.apiroute.health.RedisHealthCheck;
39 import org.onap.msb.apiroute.wrapper.MicroServiceWrapper;
40 import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.codahale.metrics.annotation.Timed;
45 import com.codahale.metrics.health.HealthCheck.Result;
46
47 import io.swagger.annotations.ApiOperation;
48 import io.swagger.annotations.ApiParam;
49 import io.swagger.annotations.ApiResponse;
50 import io.swagger.annotations.ApiResponses;
51
52 @Path("/services")
53 // @Api(tags = {"MSB-Service Resource"})
54 @Produces(MediaType.APPLICATION_JSON)
55 public class MicroServiceResource {
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceResource.class);
58
59     @Context
60     UriInfo uriInfo; // actual uri info
61
62     @GET
63     @Path("/")
64     @ApiOperation(value = "get all microservices ", code = HttpStatus.SC_OK, response = MicroServiceFullInfo.class,
65                     responseContainer = "List")
66     @ApiResponses(value = {@ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR,
67                     message = "get microservice List  fail", response = String.class)})
68     @Produces(MediaType.APPLICATION_JSON)
69     @Timed
70     public List<MicroServiceFullInfo> getMicroService() {
71         return MicroServiceWrapper.getInstance().getAllMicroServiceInstances();
72     }
73
74     /*@POST
75     @Path("/")
76     @ApiOperation(value = "add one microservice ", code = HttpStatus.SC_CREATED, response = MicroServiceFullInfo.class)
77     @ApiResponses(value = {
78                     @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY,
79                                     message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
80                     @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "add microservice fail",
81                                     response = String.class),
82                     @ApiResponse(code = HttpStatus.SC_BAD_REQUEST,
83                                     message = "Unprocessable MicroServiceInfo JSON REQUEST", response = String.class)})
84     @Produces(MediaType.APPLICATION_JSON)
85     @Timed
86     public Response addMicroService(
87                     @ApiParam(value = "MicroServiceInfo Instance Info",
88                                     required = true) MicroServiceFullInfo microServiceInfo,
89                     @Context HttpServletRequest request,
90                     @ApiParam(value = "createOrUpdate",
91                                     required = false) @QueryParam("createOrUpdate") @DefaultValue("true") boolean createOrUpdate,
92                     @ApiParam(value = "port", required = false) @QueryParam("port") @DefaultValue("") String port) {
93
94         String ip = MicroServiceUtil.getRealIp(request);
95
96         MicroServiceFullInfo microServiceFullInfo = MicroServiceWrapper.getInstance()
97                         .saveMicroServiceInstance(microServiceInfo, createOrUpdate, ip, port);
98         URI returnURI = uriInfo.getAbsolutePathBuilder()
99                         .path("/" + microServiceInfo.getServiceName() + "/version/" + microServiceInfo.getVersion())
100                         .build();
101         return Response.created(returnURI).entity(microServiceFullInfo).build();
102     }
103
104
105
106     @GET
107     @Path("/{serviceName}/version/{version}")
108     @ApiOperation(value = "get one microservice ", code = HttpStatus.SC_OK, response = MicroServiceFullInfo.class)
109     @ApiResponses(value = {
110                     @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "microservice not found",
111                                     response = String.class),
112                     @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY,
113                                     message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
114                     @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "get microservice fail",
115                                     response = String.class)})
116     @Produces(MediaType.APPLICATION_JSON)
117     @Timed
118     public MicroServiceFullInfo getMicroService(
119                     @ApiParam(value = "microservice serviceName") @PathParam("serviceName") String serviceName,
120                     @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"") @PathParam("version") @DefaultValue("") String version) {
121
122
123         return MicroServiceWrapper.getInstance().getMicroServiceInstance(serviceName, version);
124
125
126     }
127
128     @PUT
129     @Path("/{serviceName}/version/{version}")
130     @ApiOperation(value = "update one microservice by serviceName and version", code = HttpStatus.SC_CREATED,
131                     response = MicroServiceFullInfo.class)
132     @ApiResponses(value = {
133                     @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY,
134                                     message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
135                     @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "update microservice fail",
136                                     response = String.class),
137                     @ApiResponse(code = HttpStatus.SC_BAD_REQUEST,
138                                     message = "Unprocessable MicroServiceInfo JSON REQUEST", response = String.class)})
139     @Produces(MediaType.APPLICATION_JSON)
140     @Timed
141     public Response updateMicroService(
142                     @ApiParam(value = "microservice serviceName") @PathParam("serviceName") String serviceName,
143                     @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"") @PathParam("version") @DefaultValue("") String version,
144                     @ApiParam(value = "microservice Instance Info",
145                                     required = true) MicroServiceFullInfo microServiceInfo,
146                     @Context HttpServletRequest request) {
147
148         String ip = MicroServiceUtil.getRealIp(request);
149         MicroServiceFullInfo microServiceFullInfo =
150                         MicroServiceWrapper.getInstance().saveMicroServiceInstance(microServiceInfo, false, ip, "");
151         return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(microServiceFullInfo).build();
152
153     }
154
155
156
157     @DELETE
158     @Path("/{serviceName}/version/{version}/nodes/{ip}/{port}")
159     @ApiOperation(value = "delete single node by serviceName and version and node", code = HttpStatus.SC_NO_CONTENT)
160     @ApiResponses(value = {@ApiResponse(code = HttpStatus.SC_NO_CONTENT, message = "delete node succeed "),
161                     @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "node not found", response = String.class),
162                     @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY,
163                                     message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
164                     @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "delete node fail",
165                                     response = String.class)})
166     @Produces(MediaType.APPLICATION_JSON)
167     @Timed
168     public void deleteNode(
169                     @ApiParam(value = "microservice serviceName",
170                                     required = true) @PathParam("serviceName") String serviceName,
171                     @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"",
172                                     required = false) @PathParam("version") @DefaultValue("") String version,
173                     @ApiParam(value = "ip") @PathParam("ip") String ip,
174                     @ApiParam(value = "port") @PathParam("port") String port) {
175
176         MicroServiceWrapper.getInstance().deleteMicroServiceInstance(serviceName, version, ip, port);
177
178     }
179
180
181     @DELETE
182     @Path("/{serviceName}/version/{version}")
183     @ApiOperation(value = "delete one full microservice by serviceName and version", code = HttpStatus.SC_NO_CONTENT)
184     @ApiResponses(value = {@ApiResponse(code = HttpStatus.SC_NO_CONTENT, message = "delete microservice succeed "),
185                     @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "microservice not found",
186                                     response = String.class),
187                     @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY,
188                                     message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
189                     @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "delete microservice fail",
190                                     response = String.class)})
191     @Produces(MediaType.APPLICATION_JSON)
192     @Timed
193     public void deleteMicroService(
194                     @ApiParam(value = "microservice serviceName",
195                                     required = true) @PathParam("serviceName") String serviceName,
196                     @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"",
197                                     required = false) @PathParam("version") @DefaultValue("") String version) {
198
199         MicroServiceWrapper.getInstance().deleteMicroService(serviceName, version);
200
201     }
202
203     @PUT
204     @Path("/{serviceName}/version/{version}/status/{status}")
205     @ApiOperation(value = "update  microservice status by serviceName and version", code = HttpStatus.SC_CREATED,
206                     response = MicroServiceFullInfo.class)
207     @ApiResponses(value = {
208                     @ApiResponse(code = HttpStatus.SC_UNPROCESSABLE_ENTITY,
209                                     message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
210                     @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "microservice not found",
211                                     response = String.class),
212                     @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "update status fail",
213                                     response = String.class)})
214     @Produces(MediaType.APPLICATION_JSON)
215     @Timed
216     public Response updateServiceStatus(
217                     @ApiParam(value = "microservice serviceName",
218                                     required = true) @PathParam("serviceName") String serviceName,
219                     @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"",
220                                     required = false) @PathParam("version") @DefaultValue("") String version,
221                     @ApiParam(value = "status,1:abled  0:disabled") @PathParam("status") String status) {
222
223         MicroServiceFullInfo microServiceFullInfo =
224                         MicroServiceWrapper.getInstance().updateMicroServiceStatus(serviceName, version, status);
225
226         return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(microServiceFullInfo).build();
227
228     }
229
230     @GET
231     @Path("/health")
232     @ApiOperation(value = "apigateway healthy check ", code = HttpStatus.SC_OK, response = String.class)
233     @ApiResponses(value = {@ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "check fail",
234                     response = String.class)})
235     @Produces(MediaType.TEXT_PLAIN)
236     @Timed
237     public Response health() {
238
239         // redis
240         Result rst = RedisHealthCheck.getResult();
241         if (!rst.isHealthy()) {
242             LOGGER.warn("health check failed:" + rst.getMessage());
243             throw new ExtendedInternalServerErrorException(rst.getMessage());
244         }
245
246         // consul
247         rst = ConsulLinkHealthCheck.getResult();
248         if (!rst.isHealthy()) {
249             LOGGER.warn("health check failed:" + rst.getMessage());
250             throw new ExtendedInternalServerErrorException(rst.getMessage());
251         }
252
253         return Response.ok("apigateway healthy check:ok").build();
254     }*/
255
256
257 }