Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / resources / MicroServiceResource.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.msb.apiroute.resources;
18
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;
24
25 import java.net.URI;
26 import java.util.List;
27
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;
42
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;
52
53 import com.codahale.metrics.annotation.Timed;
54 import com.codahale.metrics.health.HealthCheck.Result;
55
56 @Path("/services")
57 // @Api(tags = {"MSB-Service Resource"})
58 @Produces(MediaType.APPLICATION_JSON)
59 public class MicroServiceResource {
60
61   private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceResource.class);
62
63     @Context
64     UriInfo uriInfo; // actual uri info
65
66     @GET
67     @Path("/")
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)
71     @Timed
72     public List<MicroServiceFullInfo> getMicroService() {
73         return MicroServiceWrapper.getInstance().getAllMicroServiceInstances();
74     }
75
76     @POST
77     @Path("/")
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)
84     @Timed
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) {
90        
91        String ip=MicroServiceUtil.getRealIp(request);
92         
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();
96     }
97
98
99
100     @GET
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)
108     @Timed
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) {
112
113
114         return MicroServiceWrapper.getInstance().getMicroServiceInstance(serviceName, version);
115
116
117     }
118
119     @PUT
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)
127     @Timed
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) {
133
134       String ip=MicroServiceUtil.getRealIp(request);
135         MicroServiceFullInfo microServiceFullInfo = MicroServiceWrapper.getInstance().saveMicroServiceInstance(microServiceInfo,
136             false,ip,"");
137         return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(microServiceFullInfo).build();
138
139     }
140
141
142
143
144     @DELETE
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)
153     @Timed
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) {
159
160         MicroServiceWrapper.getInstance().deleteMicroServiceInstance(serviceName, version, ip,port);
161
162     }
163
164
165     @DELETE
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)
174     @Timed
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) {
178
179          MicroServiceWrapper.getInstance().deleteMicroService(serviceName, version);
180
181     }
182
183     @PUT
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)
191     @Timed
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) {
196
197         MicroServiceFullInfo microServiceFullInfo = MicroServiceWrapper.getInstance().updateMicroServiceStatus(serviceName, version,status);
198
199         return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(microServiceFullInfo).build();
200
201     }
202     
203         @GET
204         @Path("/health")
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)
208         @Timed
209         public Response health() {
210                 
211                 // redis
212                 Result rst = RedisHealthCheck.getResult();
213                 if (!rst.isHealthy()) {
214                         LOGGER.warn("health check failed:"+rst.getMessage());
215                         throw new ExtendedInternalServerErrorException(rst.getMessage());
216                 }
217                 
218                 //consul
219                 rst = ConsulLinkHealthCheck.getResult();
220                 if (!rst.isHealthy()) {
221                         LOGGER.warn("health check failed:"+rst.getMessage());
222                         throw new ExtendedInternalServerErrorException(rst.getMessage());
223                 }
224                 
225                 return Response.ok("apigateway healthy check:ok").build();
226         }
227
228
229 }