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