ecf131c2daf0bfd89ab3c992490b1275e494d7f5
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronFirewallPolicyNorthbound.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.INeutronFirewallPolicyCRUD;
28 import org.opendaylight.neutron.spi.NeutronFirewallPolicy;
29
30 /**
31  * Neutron Northbound REST APIs for Firewall Policies.<br>
32  * This class provides REST APIs for managing neutron Firewall Policies
33  *
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("/fw/firewall_policies")
48 public final class NeutronFirewallPolicyNorthbound extends
49         AbstractNeutronNorthbound<NeutronFirewallPolicy, NeutronFirewallPolicyRequest, INeutronFirewallPolicyCRUD> {
50
51     private static final String RESOURCE_NAME = "Firewall Policy";
52
53     @Override
54     protected String getResourceName() {
55         return RESOURCE_NAME;
56     }
57
58     /**
59      * Returns a list of all Firewall Policies.
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
68     public Response listGroups(
69             // return fields
70             @QueryParam("fields") List<String> fields,
71             // OpenStack Firewall Policy attributes
72             @QueryParam("id") String queryFirewallPolicyUUID,
73             @QueryParam("tenant_id") String queryFirewallPolicyTenantID,
74             @QueryParam("name") String queryFirewallPolicyName,
75             @QueryParam("shared") Boolean querySecurityPolicyIsShared,
76             @QueryParam("audited") Boolean querySecurityPolicyIsAudited,
77             // pagination
78             @QueryParam("limit") String limit,
79             @QueryParam("marker") String marker,
80             @QueryParam("page_reverse") String pageReverse
81     // sorting not supported
82     ) {
83         INeutronFirewallPolicyCRUD firewallPolicyInterface = getNeutronCRUD();
84         List<NeutronFirewallPolicy> ans = new ArrayList<>();
85         for (NeutronFirewallPolicy nsg : firewallPolicyInterface.getAll()) {
86             if ((queryFirewallPolicyUUID == null || queryFirewallPolicyUUID.equals(nsg.getID()))
87                     && (queryFirewallPolicyTenantID == null || queryFirewallPolicyTenantID.equals(nsg.getTenantID()))
88                     && (queryFirewallPolicyName == null || queryFirewallPolicyName.equals(nsg.getName()))
89                     && (querySecurityPolicyIsShared == null
90                             || querySecurityPolicyIsShared.equals(nsg.getFirewallPolicyIsShared()))
91                     && (querySecurityPolicyIsAudited == null
92                             || querySecurityPolicyIsAudited.equals(nsg.getFirewallPolicyIsAudited()))) {
93                 if (fields.size() > 0) {
94                     ans.add(nsg.extractFields(fields));
95                 } else {
96                     ans.add(nsg);
97                 }
98             }
99         }
100         //TODO: apply pagination to results
101         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronFirewallPolicyRequest(ans)).build();
102     }
103
104     /**
105      * Returns a specific Firewall Policy.
106      */
107
108     @Path("{firewallPolicyUUID}")
109     @GET
110     @Produces({ MediaType.APPLICATION_JSON })
111     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
112             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
113             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
114             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
115             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
116     public Response showFirewallPolicy(@PathParam("firewallPolicyUUID") String firewallPolicyUUID,
117             // return fields
118             @QueryParam("fields") List<String> fields) {
119         return show(firewallPolicyUUID, fields);
120     }
121
122     /**
123      * Creates new Firewall Policy.
124      * */
125     @POST
126     @Produces({ MediaType.APPLICATION_JSON })
127     @Consumes({ MediaType.APPLICATION_JSON })
128     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
129             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
130     public Response createFirewallPolicies(final NeutronFirewallPolicyRequest input) {
131         return create(input);
132     }
133
134     /**
135      * Updates a Firewall Policy.
136      */
137     @Path("{firewallPolicyUUID}")
138     @PUT
139     @Produces({ MediaType.APPLICATION_JSON })
140     @Consumes({ MediaType.APPLICATION_JSON })
141     //@TypeHint(OpenStackSubnets.class)
142     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
143             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
144             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
145     public Response updateFirewallPolicy(@PathParam("firewallPolicyUUID") String firewallPolicyUUID,
146             final NeutronFirewallPolicyRequest input) {
147         return update(firewallPolicyUUID, input);
148     }
149
150     /**
151      * Deletes a Firewall Policy.
152      */
153
154     @Path("{firewallPolicyUUID}")
155     @DELETE
156     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
157             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
158             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
159     public Response deleteFirewallPolicy(@PathParam("firewallPolicyUUID") String firewallPolicyUUID) {
160         return delete(firewallPolicyUUID);
161     }
162 }