2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.neutron.northbound.api;
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;
33 * Neutron Northbound REST APIs for Security Rule.<br>
36 @Path("/security-group-rules")
37 public final class NeutronSecurityRulesNorthbound
38 extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD> {
40 private static final String RESOURCE_NAME = "Security Rule";
43 public NeutronSecurityRulesNorthbound(@OsgiService INeutronSecurityRuleCRUD neutronCRUD) {
48 protected String getResourceName() {
53 * Returns a list of all Security Rules.
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(
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));
107 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(ans)).build();
111 * Returns a specific Security Rule.
113 @Path("{securityRuleUUID}")
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,
123 @QueryParam("fields") List<String> fields) {
124 return show(securityRuleUUID, fields);
128 * Creates new Security Rule.
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);
140 * Updates a Security Rule.
142 @Path("{securityRuleUUID}")
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);
155 * Deletes a Security Rule.
157 @Path("{securityRuleUUID}")
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);