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