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