checkstyle: enable LocalVariableName
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronSecurityRulesNorthbound.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.northbound.api;
10
11 import java.net.HttpURLConnection;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
28 import org.opendaylight.neutron.spi.NeutronSecurityRule;
29
30 /**
31  * Neutron Northbound REST APIs for Security Rule.<br>
32  * This class provides REST APIs for managing neutron Security Rule
33  * <p>
34  * <br>
35  * <br>
36  * Authentication scheme : <b>HTTP Basic</b><br>
37  * Authentication realm : <b>opendaylight</b><br>
38  * Transport : <b>HTTP and HTTPS</b><br>
39  * <br>
40  * HTTPS Authentication is disabled by default. Administrator can enable it in
41  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
42  * trusted authority.<br>
43  * More info :
44  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
45  */
46
47 @Path("/security-group-rules")
48 public final class NeutronSecurityRulesNorthbound
49         extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD> {
50     private static final String RESOURCE_NAME = "Security Rule";
51
52     @Override
53     protected String getResourceName() {
54         return RESOURCE_NAME;
55     }
56
57     /**
58      * Returns a list of all Security Rules
59      */
60     @GET
61     @Produces({ MediaType.APPLICATION_JSON })
62     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
63             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
64             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
65             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
66     public Response listRules(
67             // return fields
68             @QueryParam("fields") List<String> fields,
69             // OpenStack security rule attributes
70             @QueryParam("id") String querySecurityRuleUUID,
71             @QueryParam("direction") String querySecurityRuleDirection,
72             @QueryParam("protocol") String querySecurityRuleProtocol,
73             @QueryParam("port_range_min") Integer querySecurityRulePortMin,
74             @QueryParam("port_range_max") Integer querySecurityRulePortMax,
75             @QueryParam("ethertype") String querySecurityRuleEthertype,
76             @QueryParam("remote_ip_prefix") String querySecurityRuleIpPrefix,
77             @QueryParam("remote_group_id") String querySecurityRemoteGroupID,
78             @QueryParam("security_group_id") String querySecurityRuleGroupID,
79             @QueryParam("tenant_id") String querySecurityRuleTenantID,
80             @QueryParam("limit") String limit,
81             @QueryParam("marker") String marker,
82             @QueryParam("page_reverse") String pageReverse) {
83         INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
84         List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAll();
85         List<NeutronSecurityRule> ans = new ArrayList<>();
86         for (NeutronSecurityRule nsr : allSecurityRules) {
87             if ((querySecurityRuleUUID == null || querySecurityRuleUUID.equals(nsr.getID()))
88                     && (querySecurityRuleDirection == null
89                             || querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection()))
90                     && (querySecurityRuleProtocol == null
91                             || querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol()))
92                     && (querySecurityRulePortMin == null
93                             || querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin()))
94                     && (querySecurityRulePortMax == null
95                             || querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax()))
96                     && (querySecurityRuleEthertype == null
97                             || querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype()))
98                     && (querySecurityRuleIpPrefix == null
99                             || querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix()))
100                     && (querySecurityRuleGroupID == null
101                             || querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID()))
102                     && (querySecurityRemoteGroupID == null
103                             || querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID()))
104                     && (querySecurityRuleTenantID == null || querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
105                 if (fields.size() > 0) {
106                     ans.add(nsr.extractFields(fields));
107                 } else {
108                     ans.add(nsr);
109                 }
110             }
111         }
112         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(ans)).build();
113     }
114
115     /**
116      * Returns a specific Security Rule
117      */
118
119     @Path("{securityRuleUUID}")
120     @GET
121     @Produces({ MediaType.APPLICATION_JSON })
122     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
123             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
124             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
125             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
126             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
127     public Response showSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
128             // return fields
129             @QueryParam("fields") List<String> fields) {
130         return show(securityRuleUUID, fields);
131     }
132
133     /**
134      * Creates new Security Rule
135      */
136
137     @POST
138     @Produces({ MediaType.APPLICATION_JSON })
139     @Consumes({ MediaType.APPLICATION_JSON })
140     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
141             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
142     public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
143         return create(input);
144     }
145
146     /**
147      * Updates a Security Rule
148      */
149
150     @Path("{securityRuleUUID}")
151     @PUT
152     @Produces({ MediaType.APPLICATION_JSON })
153     @Consumes({ MediaType.APPLICATION_JSON })
154     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
155             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
156             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
157     public Response updateSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
158             final NeutronSecurityRuleRequest input) {
159         return update(securityRuleUUID, input);
160     }
161
162     /**
163      * Deletes a Security Rule
164      */
165
166     @Path("{securityRuleUUID}")
167     @DELETE
168     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
169             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
170             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
171     public Response deleteSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID) {
172         return delete(securityRuleUUID);
173     }
174 }