complete dependency inject CRUD implementations
[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 package org.opendaylight.neutron.northbound.api;
9
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import org.codehaus.enunciate.jaxrs.ResponseCode;
27 import org.codehaus.enunciate.jaxrs.StatusCodes;
28 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
29 import org.opendaylight.neutron.spi.NeutronSecurityRule;
30 import org.ops4j.pax.cdi.api.OsgiService;
31
32 /**
33  * Neutron Northbound REST APIs for Security Rule.<br>
34  */
35 @Singleton
36 @Path("/security-group-rules")
37 public final class NeutronSecurityRulesNorthbound
38         extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD> {
39
40     private static final String RESOURCE_NAME = "Security Rule";
41
42     @Inject
43     public NeutronSecurityRulesNorthbound(@OsgiService INeutronSecurityRuleCRUD neutronCRUD) {
44         super(neutronCRUD);
45     }
46
47     @Override
48     protected String getResourceName() {
49         return RESOURCE_NAME;
50     }
51
52     /**
53      * Returns a list of all Security Rules.
54      */
55     @GET
56     @Produces({ MediaType.APPLICATION_JSON })
57     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
58             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
59             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
60             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
61     public Response listRules(
62             // return fields
63             @QueryParam("fields") List<String> fields,
64             // OpenStack security rule attributes
65             @QueryParam("id") String querySecurityRuleUUID,
66             @QueryParam("direction") String querySecurityRuleDirection,
67             @QueryParam("protocol") String querySecurityRuleProtocol,
68             @QueryParam("port_range_min") Integer querySecurityRulePortMin,
69             @QueryParam("port_range_max") Integer querySecurityRulePortMax,
70             @QueryParam("ethertype") String querySecurityRuleEthertype,
71             @QueryParam("remote_ip_prefix") String querySecurityRuleIpPrefix,
72             @QueryParam("remote_group_id") String querySecurityRemoteGroupID,
73             @QueryParam("security_group_id") String querySecurityRuleGroupID,
74             @QueryParam("tenant_id") String querySecurityRuleTenantID,
75             @QueryParam("limit") String limit,
76             @QueryParam("marker") String marker,
77             @QueryParam("page_reverse") String pageReverse) {
78         INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
79         List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAll();
80         List<NeutronSecurityRule> ans = new ArrayList<>();
81         for (NeutronSecurityRule nsr : allSecurityRules) {
82             if ((querySecurityRuleUUID == null || querySecurityRuleUUID.equals(nsr.getID()))
83                     && (querySecurityRuleDirection == null
84                             || querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection()))
85                     && (querySecurityRuleProtocol == null
86                             || querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol()))
87                     && (querySecurityRulePortMin == null
88                             || querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin()))
89                     && (querySecurityRulePortMax == null
90                             || querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax()))
91                     && (querySecurityRuleEthertype == null
92                             || querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype()))
93                     && (querySecurityRuleIpPrefix == null
94                             || querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix()))
95                     && (querySecurityRuleGroupID == null
96                             || querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID()))
97                     && (querySecurityRemoteGroupID == null
98                             || querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID()))
99                     && (querySecurityRuleTenantID == null || querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
100                 if (fields.size() > 0) {
101                     ans.add(nsr.extractFields(fields));
102                 } else {
103                     ans.add(nsr);
104                 }
105             }
106         }
107         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(ans)).build();
108     }
109
110     /**
111      * Returns a specific Security Rule.
112      */
113     @Path("{securityRuleUUID}")
114     @GET
115     @Produces({ MediaType.APPLICATION_JSON })
116     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
117             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
118             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
119             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
120             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
121     public Response showSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
122             // return fields
123             @QueryParam("fields") List<String> fields) {
124         return show(securityRuleUUID, fields);
125     }
126
127     /**
128      * Creates new Security Rule.
129      */
130     @POST
131     @Produces({ MediaType.APPLICATION_JSON })
132     @Consumes({ MediaType.APPLICATION_JSON })
133     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
134             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
135     public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
136         return create(input);
137     }
138
139     /**
140      * Updates a Security Rule.
141      */
142     @Path("{securityRuleUUID}")
143     @PUT
144     @Produces({ MediaType.APPLICATION_JSON })
145     @Consumes({ MediaType.APPLICATION_JSON })
146     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
147             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
148             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
149     public Response updateSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
150             final NeutronSecurityRuleRequest input) {
151         return update(securityRuleUUID, input);
152     }
153
154     /**
155      * Deletes a Security Rule.
156      */
157     @Path("{securityRuleUUID}")
158     @DELETE
159     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
160             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
161             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
162     public Response deleteSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID) {
163         return delete(securityRuleUUID);
164     }
165 }