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.ws.rs.Consumes;
14 import javax.ws.rs.DELETE;
15 import javax.ws.rs.GET;
16 import javax.ws.rs.POST;
17 import javax.ws.rs.PUT;
18 import javax.ws.rs.Path;
19 import javax.ws.rs.PathParam;
20 import javax.ws.rs.Produces;
21 import javax.ws.rs.QueryParam;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import org.codehaus.enunciate.jaxrs.ResponseCode;
25 import org.codehaus.enunciate.jaxrs.StatusCodes;
26 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
27 import org.opendaylight.neutron.spi.NeutronSecurityRule;
30 * Neutron Northbound REST APIs for Security Rule.<br>
32 @Path("/security-group-rules")
33 public final class NeutronSecurityRulesNorthbound
34 extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD> {
36 private static final String RESOURCE_NAME = "Security Rule";
39 protected String getResourceName() {
44 * Returns a list of all Security Rules.
47 @Produces({ MediaType.APPLICATION_JSON })
48 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
49 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
50 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
51 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
52 public Response listRules(
54 @QueryParam("fields") List<String> fields,
55 // OpenStack security rule attributes
56 @QueryParam("id") String querySecurityRuleUUID,
57 @QueryParam("direction") String querySecurityRuleDirection,
58 @QueryParam("protocol") String querySecurityRuleProtocol,
59 @QueryParam("port_range_min") Integer querySecurityRulePortMin,
60 @QueryParam("port_range_max") Integer querySecurityRulePortMax,
61 @QueryParam("ethertype") String querySecurityRuleEthertype,
62 @QueryParam("remote_ip_prefix") String querySecurityRuleIpPrefix,
63 @QueryParam("remote_group_id") String querySecurityRemoteGroupID,
64 @QueryParam("security_group_id") String querySecurityRuleGroupID,
65 @QueryParam("tenant_id") String querySecurityRuleTenantID,
66 @QueryParam("limit") String limit,
67 @QueryParam("marker") String marker,
68 @QueryParam("page_reverse") String pageReverse) {
69 INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
70 List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAll();
71 List<NeutronSecurityRule> ans = new ArrayList<>();
72 for (NeutronSecurityRule nsr : allSecurityRules) {
73 if ((querySecurityRuleUUID == null || querySecurityRuleUUID.equals(nsr.getID()))
74 && (querySecurityRuleDirection == null
75 || querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection()))
76 && (querySecurityRuleProtocol == null
77 || querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol()))
78 && (querySecurityRulePortMin == null
79 || querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin()))
80 && (querySecurityRulePortMax == null
81 || querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax()))
82 && (querySecurityRuleEthertype == null
83 || querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype()))
84 && (querySecurityRuleIpPrefix == null
85 || querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix()))
86 && (querySecurityRuleGroupID == null
87 || querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID()))
88 && (querySecurityRemoteGroupID == null
89 || querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID()))
90 && (querySecurityRuleTenantID == null || querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
91 if (fields.size() > 0) {
92 ans.add(nsr.extractFields(fields));
98 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(ans)).build();
102 * Returns a specific Security Rule.
104 @Path("{securityRuleUUID}")
106 @Produces({ MediaType.APPLICATION_JSON })
107 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
108 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
109 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
110 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
111 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
112 public Response showSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
114 @QueryParam("fields") List<String> fields) {
115 return show(securityRuleUUID, fields);
119 * Creates new Security Rule.
122 @Produces({ MediaType.APPLICATION_JSON })
123 @Consumes({ MediaType.APPLICATION_JSON })
124 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
125 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
126 public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
127 return create(input);
131 * Updates a Security Rule.
133 @Path("{securityRuleUUID}")
135 @Produces({ MediaType.APPLICATION_JSON })
136 @Consumes({ MediaType.APPLICATION_JSON })
137 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
138 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
139 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
140 public Response updateSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
141 final NeutronSecurityRuleRequest input) {
142 return update(securityRuleUUID, input);
146 * Deletes a Security Rule.
148 @Path("{securityRuleUUID}")
150 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
151 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
152 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
153 public Response deleteSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID) {
154 return delete(securityRuleUUID);