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;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.codehaus.enunciate.jaxrs.ResponseCode;
30 import org.codehaus.enunciate.jaxrs.StatusCodes;
31 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
32 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
33 import org.opendaylight.neutron.spi.NeutronSecurityRule;
36 * Neutron Northbound REST APIs for Security Rule.<br>
37 * This class provides REST APIs for managing neutron Security Rule
41 * Authentication scheme : <b>HTTP Basic</b><br>
42 * Authentication realm : <b>opendaylight</b><br>
43 * Transport : <b>HTTP and HTTPS</b><br>
45 * HTTPS Authentication is disabled by default. Administrator can enable it in
46 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
47 * trusted authority.<br>
49 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
52 @Path ("/security-group-rules")
53 public class NeutronSecurityRulesNorthbound
54 extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD> {
55 private static final String RESOURCE_NAME = "Security Rule";
58 protected String getResourceName() {
63 protected NeutronSecurityRule extractFields(NeutronSecurityRule o, List<String> fields) {
64 return o.extractFields(fields);
68 protected NeutronSecurityRuleRequest newNeutronRequest(NeutronSecurityRule o) {
69 return new NeutronSecurityRuleRequest(o);
73 protected INeutronSecurityRuleCRUD getNeutronCRUD() {
74 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSecurityRuleCRUD(this);
75 if (answer.getSecurityRuleInterface() == null) {
76 throw new ServiceUnavailableException(serviceUnavailable());
78 return answer.getSecurityRuleInterface();
82 * Returns a list of all Security Rules
85 @Produces ({MediaType.APPLICATION_JSON})
87 @ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
88 @ResponseCode (code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
89 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
90 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
91 public Response listRules(
93 @QueryParam ("fields") List<String> fields,
94 // OpenStack security rule attributes
95 @QueryParam ("id") String querySecurityRuleUUID,
96 @QueryParam ("direction") String querySecurityRuleDirection,
97 @QueryParam ("protocol") String querySecurityRuleProtocol,
98 @QueryParam ("port_range_min") Integer querySecurityRulePortMin,
99 @QueryParam ("port_range_max") Integer querySecurityRulePortMax,
100 @QueryParam ("ethertype") String querySecurityRuleEthertype,
101 @QueryParam ("remote_ip_prefix") String querySecurityRuleIpPrefix,
102 @QueryParam ("remote_group_id") String querySecurityRemoteGroupID,
103 @QueryParam ("security_group_id") String querySecurityRuleGroupID,
104 @QueryParam ("tenant_id") String querySecurityRuleTenantID,
105 @QueryParam ("limit") String limit,
106 @QueryParam ("marker") String marker,
107 @QueryParam ("page_reverse") String pageReverse
109 INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
110 List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAll();
111 List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
112 Iterator<NeutronSecurityRule> i = allSecurityRules.iterator();
113 while (i.hasNext()) {
114 NeutronSecurityRule nsr = i.next();
115 if ((querySecurityRuleUUID == null ||
116 querySecurityRuleUUID.equals(nsr.getID())) &&
117 (querySecurityRuleDirection == null ||
118 querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection())) &&
119 (querySecurityRuleProtocol == null ||
120 querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol())) &&
121 (querySecurityRulePortMin == null ||
122 querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin())) &&
123 (querySecurityRulePortMax == null ||
124 querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax())) &&
125 (querySecurityRuleEthertype == null ||
126 querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype())) &&
127 (querySecurityRuleIpPrefix == null ||
128 querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix())) &&
129 (querySecurityRuleGroupID == null ||
130 querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID())) &&
131 (querySecurityRemoteGroupID == null ||
132 querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID())) &&
133 (querySecurityRuleTenantID == null ||
134 querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
135 if (fields.size() > 0) {
136 ans.add(extractFields(nsr, fields));
142 return Response.status(HttpURLConnection.HTTP_OK).entity(
143 new NeutronSecurityRuleRequest(ans)).build();
147 * Returns a specific Security Rule
150 @Path ("{securityRuleUUID}")
152 @Produces ({MediaType.APPLICATION_JSON})
154 @ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
155 @ResponseCode (code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
156 @ResponseCode (code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
157 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
158 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
159 public Response showSecurityRule(@PathParam ("securityRuleUUID") String securityRuleUUID,
161 @QueryParam ("fields") List<String> fields) {
162 return show(securityRuleUUID, fields);
166 * Creates new Security Rule
170 @Produces ({MediaType.APPLICATION_JSON})
171 @Consumes ({MediaType.APPLICATION_JSON})
173 @ResponseCode (code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
174 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
175 public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
176 return create(input);
180 * Updates a Security Rule
183 @Path ("{securityRuleUUID}")
185 @Produces ({MediaType.APPLICATION_JSON})
186 @Consumes ({MediaType.APPLICATION_JSON})
188 @ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
189 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
190 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
191 public Response updateSecurityRule(
192 @PathParam ("securityRuleUUID") String securityRuleUUID, final NeutronSecurityRuleRequest input) {
193 return update(securityRuleUUID, input);
197 * Deletes a Security Rule
200 @Path ("{securityRuleUUID}")
203 @ResponseCode (code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
204 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
205 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
206 public Response deleteSecurityRule(
207 @PathParam ("securityRuleUUID") String securityRuleUUID) {
208 return delete(securityRuleUUID);