Issue-id: OCS-9
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / resources / CustomRouteResource.java
1 /**
2  * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
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 package org.openo.msb.resources;
17
18 import io.swagger.annotations.Api;
19 import io.swagger.annotations.ApiOperation;
20 import io.swagger.annotations.ApiParam;
21 import io.swagger.annotations.ApiResponse;
22 import io.swagger.annotations.ApiResponses;
23
24 import java.net.URI;
25
26 import javax.ws.rs.DELETE;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.POST;
29 import javax.ws.rs.PUT;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.UriInfo;
37
38 import org.apache.http.HttpStatus;
39 import org.openo.msb.api.CustomRouteInfo;
40 import org.openo.msb.wrapper.CustomRouteServiceWrapper;
41
42 import com.codahale.metrics.annotation.Timed;
43
44 @Path("/customRoute")
45 @Api(tags = { "CustomRoute" })
46 @Produces(MediaType.APPLICATION_JSON)
47 public class CustomRouteResource {
48         
49     @Context
50     UriInfo uriInfo; // actual uri info
51     
52         @GET
53         @Path("/all")
54         @ApiOperation(value = "get all CustomRoute ",  code = HttpStatus.SC_OK,response = CustomRouteInfo.class, responseContainer = "List")
55     @ApiResponses(value = {@ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "get CustomRouteInfo List  fail", response = String.class)})
56         @Produces(MediaType.APPLICATION_JSON)
57         @Timed
58         public CustomRouteInfo[] getCustomRoutes() {
59                 return CustomRouteServiceWrapper.getInstance().getAllCustomRouteInstances();
60         }
61         
62         @POST
63         @Path("/instance")
64         @ApiOperation(value = "add one CustomRoute ", code = HttpStatus.SC_CREATED,response = CustomRouteInfo.class)
65         @ApiResponses(value = {
66                                @ApiResponse(code = HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, message = "Unprocessable CustomRouteInfo Entity ", response = String.class),
67                                @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "add CustomRouteInfo fail", response = String.class),
68                                @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "Unprocessable CustomRouteInfo JSON REQUEST", response = String.class)})
69         @Produces(MediaType.APPLICATION_JSON)
70         @Timed
71         public Response addCustomRoute(
72                         @ApiParam(value = "CustomRoute Instance Info", required = true) CustomRouteInfo customRouteInfo) {
73             CustomRouteInfo new_customRouteInfo = CustomRouteServiceWrapper.getInstance().saveCustomRouteInstance(customRouteInfo,"");
74             URI returnURI =
75                 uriInfo.getAbsolutePathBuilder()
76                         .path("/instance?serviceName=" + new_customRouteInfo.getServiceName()).build();
77         return Response.created(returnURI).entity(new_customRouteInfo).build();
78
79         }
80         
81         @GET
82         @Path("/instance")
83         @ApiOperation(value = "get one CustomRoute ",code = HttpStatus.SC_OK,  response = CustomRouteInfo.class)
84         @ApiResponses(value = {
85                                 @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "CustomRoute not found", response = String.class),
86                                 @ApiResponse(code = HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, message = "Unprocessable CustomRoute Entity ", response = String.class),
87                                 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "get CustomRoute fail", response = String.class)})
88         @Produces(MediaType.APPLICATION_JSON)
89         @Timed
90         public CustomRouteInfo getCustomRoute(
91                         @ApiParam(value = "CustomRoute serviceName", required = false) @QueryParam("serviceName") String serviceName) {
92             
93          
94                 return CustomRouteServiceWrapper.getInstance().getCustomRouteInstance(serviceName);
95
96         }
97         
98         @PUT
99         @Path("/instance")
100         @ApiOperation(value = "update one CustomRoute by serviceName",  code = HttpStatus.SC_CREATED,response = CustomRouteInfo.class)
101         @ApiResponses(value = {
102                                @ApiResponse(code = HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, message = "Unprocessable CustomRoute Entity ", response = String.class),
103                                @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "update CustomRoute fail", response = String.class),
104                                @ApiResponse(code = HttpStatus.SC_BAD_REQUEST, message = "Unprocessable CustomRoute JSON REQUEST", response = String.class)})
105         @Produces(MediaType.APPLICATION_JSON)
106         @Timed
107         public Response updateCustomRoute(
108                         @ApiParam(value = "CustomRoute serviceName", required = true) @QueryParam("serviceName") String serviceName,
109                         @ApiParam(value = "CustomRoute Instance Info", required = true) CustomRouteInfo customRoute) {
110
111             CustomRouteInfo new_customRouteInfo= CustomRouteServiceWrapper.getInstance().updateCustomRouteInstance(serviceName,customRoute,"");
112
113         return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(new_customRouteInfo).build();
114
115         }
116
117         @DELETE
118         @Path("/instance")
119         @ApiOperation(value = "delete one CustomRoute by serviceName",  code = HttpStatus.SC_NO_CONTENT)
120         @ApiResponses(value = {
121                                 @ApiResponse(code = HttpStatus.SC_NO_CONTENT, message = "delete customRoute succeed "),
122                                 @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "customRoute not found", response = String.class),
123                                 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "delete customRoute fail", response = String.class)})
124         @Produces(MediaType.APPLICATION_JSON)
125         @Timed
126         public void deleteCustomRoute(
127                         @ApiParam(value = "CustomRoute serviceName", required = true) @QueryParam("serviceName") String serviceName) {
128
129                  CustomRouteServiceWrapper.getInstance().deleteCustomRoute(serviceName,"*","");
130
131         }
132         
133         @PUT
134         @Path("/status")
135         @ApiOperation(value = "update one CustomRoute  status by serviceName ",code = HttpStatus.SC_CREATED, response = CustomRouteInfo.class)
136     @ApiResponses(value = {
137                                 @ApiResponse(code = HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, message = "Unprocessable customRoute Entity ", response = String.class),
138                                 @ApiResponse(code = HttpStatus.SC_NOT_FOUND, message = "customRoute not found", response = String.class),
139                                 @ApiResponse(code = HttpStatus.SC_INTERNAL_SERVER_ERROR, message = "update status fail", response = String.class)})
140         @Produces(MediaType.APPLICATION_JSON)
141         @Timed
142         public Response updateCustomRouteStatus(
143                         @ApiParam(value = "CustomRoute serviceName", required = true) @QueryParam("serviceName") String serviceName,
144                         @ApiParam(value = "CustomRoute status,1:abled  0:disabled", required = true)  @QueryParam("status") String status) {
145
146             CustomRouteInfo new_customRouteInfo = CustomRouteServiceWrapper.getInstance().updateCustomRouteStatus(serviceName,status);
147         return Response.created(uriInfo.getAbsolutePathBuilder().build()).entity(new_customRouteInfo).build();
148
149
150         }
151
152 }