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
9 package org.opendaylight.neutron.northbound.api;
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.INeutronSecurityRuleCRUD;
28 import org.opendaylight.neutron.spi.NeutronSecurityRule;
31 * Neutron Northbound REST APIs for Security Rule.<br>
32 * This class provides REST APIs for managing neutron Security Rule
36 * Authentication scheme : <b>HTTP Basic</b><br>
37 * Authentication realm : <b>opendaylight</b><br>
38 * Transport : <b>HTTP and HTTPS</b><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>
44 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
47 @Path("/security-group-rules")
48 public final class NeutronSecurityRulesNorthbound
49 extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD> {
50 private static final String RESOURCE_NAME = "Security Rule";
53 protected String getResourceName() {
58 * Returns a list of all Security Rules
61 @Produces({ MediaType.APPLICATION_JSON })
62 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
63 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
64 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
65 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
66 public Response listRules(
68 @QueryParam("fields") List<String> fields,
69 // OpenStack security rule attributes
70 @QueryParam("id") String querySecurityRuleUUID,
71 @QueryParam("direction") String querySecurityRuleDirection,
72 @QueryParam("protocol") String querySecurityRuleProtocol,
73 @QueryParam("port_range_min") Integer querySecurityRulePortMin,
74 @QueryParam("port_range_max") Integer querySecurityRulePortMax,
75 @QueryParam("ethertype") String querySecurityRuleEthertype,
76 @QueryParam("remote_ip_prefix") String querySecurityRuleIpPrefix,
77 @QueryParam("remote_group_id") String querySecurityRemoteGroupID,
78 @QueryParam("security_group_id") String querySecurityRuleGroupID,
79 @QueryParam("tenant_id") String querySecurityRuleTenantID,
80 @QueryParam("limit") String limit,
81 @QueryParam("marker") String marker,
82 @QueryParam("page_reverse") String pageReverse) {
83 INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
84 List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAll();
85 List<NeutronSecurityRule> ans = new ArrayList<>();
86 for (NeutronSecurityRule nsr : allSecurityRules) {
87 if ((querySecurityRuleUUID == null || querySecurityRuleUUID.equals(nsr.getID()))
88 && (querySecurityRuleDirection == null
89 || querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection()))
90 && (querySecurityRuleProtocol == null
91 || querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol()))
92 && (querySecurityRulePortMin == null
93 || querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin()))
94 && (querySecurityRulePortMax == null
95 || querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax()))
96 && (querySecurityRuleEthertype == null
97 || querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype()))
98 && (querySecurityRuleIpPrefix == null
99 || querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix()))
100 && (querySecurityRuleGroupID == null
101 || querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID()))
102 && (querySecurityRemoteGroupID == null
103 || querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID()))
104 && (querySecurityRuleTenantID == null || querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
105 if (fields.size() > 0) {
106 ans.add(nsr.extractFields(fields));
112 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSecurityRuleRequest(ans)).build();
116 * Returns a specific Security Rule
119 @Path("{securityRuleUUID}")
121 @Produces({ MediaType.APPLICATION_JSON })
122 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
123 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
124 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
125 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
126 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
127 public Response showSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
129 @QueryParam("fields") List<String> fields) {
130 return show(securityRuleUUID, fields);
134 * Creates new Security Rule
138 @Produces({ MediaType.APPLICATION_JSON })
139 @Consumes({ MediaType.APPLICATION_JSON })
140 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
141 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
142 public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
143 return create(input);
147 * Updates a Security Rule
150 @Path("{securityRuleUUID}")
152 @Produces({ MediaType.APPLICATION_JSON })
153 @Consumes({ MediaType.APPLICATION_JSON })
154 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
155 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
156 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
157 public Response updateSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID,
158 final NeutronSecurityRuleRequest input) {
159 return update(securityRuleUUID, input);
163 * Deletes a Security Rule
166 @Path("{securityRuleUUID}")
168 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
169 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
170 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
171 public Response deleteSecurityRule(@PathParam("securityRuleUUID") String securityRuleUUID) {
172 return delete(securityRuleUUID);