Checkstyle formatting issues fix (Northbound API)
[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.Iterator;
14 import java.util.List;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import org.codehaus.enunciate.jaxrs.ResponseCode;
27 import org.codehaus.enunciate.jaxrs.StatusCodes;
28 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
29 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
30 import org.opendaylight.neutron.spi.NeutronSecurityRule;
31
32 /**
33  * Neutron Northbound REST APIs for Security Rule.<br>
34  * This class provides REST APIs for managing neutron Security Rule
35  * <p>
36  * <br>
37  * <br>
38  * Authentication scheme : <b>HTTP Basic</b><br>
39  * Authentication realm : <b>opendaylight</b><br>
40  * Transport : <b>HTTP and HTTPS</b><br>
41  * <br>
42  * HTTPS Authentication is disabled by default. Administrator can enable it in
43  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
44  * trusted authority.<br>
45  * More info :
46  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
47  */
48
49 @Path("/security-group-rules")
50 public class NeutronSecurityRulesNorthbound
51         extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD> {
52     private static final String RESOURCE_NAME = "Security Rule";
53
54     @Override
55     protected String getResourceName() {
56         return RESOURCE_NAME;
57     }
58
59     @Override
60     protected NeutronSecurityRule extractFields(NeutronSecurityRule o, List<String> fields) {
61         return o.extractFields(fields);
62     }
63
64     @Override
65     protected NeutronSecurityRuleRequest newNeutronRequest(NeutronSecurityRule o) {
66         return new NeutronSecurityRuleRequest(o);
67     }
68
69     @Override
70     protected INeutronSecurityRuleCRUD getNeutronCRUD() {
71         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSecurityRuleCRUD(this);
72         if (answer.getSecurityRuleInterface() == null) {
73             throw new ServiceUnavailableException(serviceUnavailable());
74         }
75         return answer.getSecurityRuleInterface();
76     }
77
78     /**
79      * Returns a list of all Security Rules
80      */
81     @GET
82     @Produces({ MediaType.APPLICATION_JSON })
83     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
84             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
85             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
86             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
87     public Response listRules(
88             // return fields
89             @QueryParam("fields") List<String> fields,
90             // OpenStack security rule attributes
91             @QueryParam("id") String querySecurityRuleUUID,
92             @QueryParam("direction") String querySecurityRuleDirection,
93             @QueryParam("protocol") String querySecurityRuleProtocol,
94             @QueryParam("port_range_min") Integer querySecurityRulePortMin,
95             @QueryParam("port_range_max") Integer querySecurityRulePortMax,
96             @QueryParam("ethertype") String querySecurityRuleEthertype,
97             @QueryParam("remote_ip_prefix") String querySecurityRuleIpPrefix,
98             @QueryParam("remote_group_id") String querySecurityRemoteGroupID,
99             @QueryParam("security_group_id") String querySecurityRuleGroupID,
100             @QueryParam("tenant_id") String querySecurityRuleTenantID,
101             @QueryParam("limit") String limit,
102             @QueryParam("marker") String marker,
103             @QueryParam("page_reverse") String pageReverse) {
104         INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
105         List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAll();
106         List<NeutronSecurityRule> ans = new ArrayList<>();
107         Iterator<NeutronSecurityRule> i = allSecurityRules.iterator();
108         while (i.hasNext()) {
109             NeutronSecurityRule nsr = i.next();
110             if ((querySecurityRuleUUID == null || querySecurityRuleUUID.equals(nsr.getID()))
111                     && (querySecurityRuleDirection == null
112                             || querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection()))
113                     && (querySecurityRuleProtocol == null
114                             || querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol()))
115                     && (querySecurityRulePortMin == null
116                             || querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin()))
117                     && (querySecurityRulePortMax == null
118                             || querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax()))
119                     && (querySecurityRuleEthertype == null
120                             || querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype()))
121                     && (querySecurityRuleIpPrefix == null
122                             || querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix()))
123                     && (querySecurityRuleGroupID == null
124                             || querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID()))
125                     && (querySecurityRemoteGroupID == null
126                             || querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID()))
127                     && (querySecurityRuleTenantID == null || querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
128                 if (fields.size() > 0) {
129                     ans.add(extractFields(nsr, fields));
130                 } else {
131                     ans.add(nsr);
132                 }
133             }
134         }
135         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(ans)).build();
136     }
137
138     /**
139      * Returns a specific Security Rule
140      */
141
142     @Path("{securityRuleUUID}")
143     @GET
144     @Produces({ MediaType.APPLICATION_JSON })
145     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
146             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
147             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
148             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
149             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
150     public Response showSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
151             // return fields
152             @QueryParam("fields") List<String> fields) {
153         return show(securityRuleUUID, fields);
154     }
155
156     /**
157      * Creates new Security Rule
158      */
159
160     @POST
161     @Produces({ MediaType.APPLICATION_JSON })
162     @Consumes({ MediaType.APPLICATION_JSON })
163     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
164             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
165     public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
166         return create(input);
167     }
168
169     /**
170      * Updates a Security Rule
171      */
172
173     @Path("{securityRuleUUID}")
174     @PUT
175     @Produces({ MediaType.APPLICATION_JSON })
176     @Consumes({ MediaType.APPLICATION_JSON })
177     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
178             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
179             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
180     public Response updateSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
181             final NeutronSecurityRuleRequest input) {
182         return update(securityRuleUUID, input);
183     }
184
185     /**
186      * Deletes a Security Rule
187      */
188
189     @Path("{securityRuleUUID}")
190     @DELETE
191     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
192             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
193             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
194     public Response deleteSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID) {
195         return delete(securityRuleUUID);
196     }
197 }