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