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.INeutronSecurityRuleAware;
32 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
33 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
34 import org.opendaylight.neutron.spi.NeutronSecurityRule;
37 * Neutron Northbound REST APIs for Security Rule.<br>
38 * This class provides REST APIs for managing neutron Security Rule
42 * Authentication scheme : <b>HTTP Basic</b><br>
43 * Authentication realm : <b>opendaylight</b><br>
44 * Transport : <b>HTTP and HTTPS</b><br>
46 * HTTPS Authentication is disabled by default. Administrator can enable it in
47 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
48 * trusted authority.<br>
50 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
53 @Path ("/security-group-rules")
54 public class NeutronSecurityRulesNorthbound
55 extends AbstractNeutronNorthbound<NeutronSecurityRule, NeutronSecurityRuleRequest, INeutronSecurityRuleCRUD, INeutronSecurityRuleAware> {
56 private static final String RESOURCE_NAME = "Security Rule";
59 protected String getResourceName() {
64 protected NeutronSecurityRule extractFields(NeutronSecurityRule o, List<String> fields) {
65 return o.extractFields(fields);
69 protected NeutronSecurityRuleRequest newNeutronRequest(NeutronSecurityRule o) {
70 return new NeutronSecurityRuleRequest(o);
74 protected INeutronSecurityRuleCRUD getNeutronCRUD() {
75 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSecurityRuleCRUD(this);
76 if (answer.getSecurityRuleInterface() == null) {
77 throw new ServiceUnavailableException(serviceUnavailable());
79 return answer.getSecurityRuleInterface();
83 protected Object[] getInstances() {
84 return NeutronUtil.getInstances(INeutronSecurityRuleAware.class, this);
88 protected int canCreate(Object instance, NeutronSecurityRule singleton) {
89 INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
90 return service.canCreateNeutronSecurityRule(singleton);
94 protected void created(Object instance, NeutronSecurityRule singleton) {
95 INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
96 service.neutronSecurityRuleCreated(singleton);
100 protected int canUpdate(Object instance, NeutronSecurityRule delta, NeutronSecurityRule original) {
101 INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
102 return service.canUpdateNeutronSecurityRule(delta, original);
106 protected void updated(Object instance, NeutronSecurityRule updated) {
107 INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
108 service.neutronSecurityRuleUpdated(updated);
112 protected int canDelete(Object instance, NeutronSecurityRule singleton) {
113 INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
114 return service.canDeleteNeutronSecurityRule(singleton);
118 protected void deleted(Object instance, NeutronSecurityRule singleton) {
119 INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
120 service.neutronSecurityRuleDeleted(singleton);
124 * Returns a list of all Security Rules
127 @Produces ({MediaType.APPLICATION_JSON})
129 @ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
130 @ResponseCode (code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
131 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
132 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
133 public Response listRules(
135 @QueryParam ("fields") List<String> fields,
136 // OpenStack security rule attributes
137 @QueryParam ("id") String querySecurityRuleUUID,
138 @QueryParam ("direction") String querySecurityRuleDirection,
139 @QueryParam ("protocol") String querySecurityRuleProtocol,
140 @QueryParam ("port_range_min") Integer querySecurityRulePortMin,
141 @QueryParam ("port_range_max") Integer querySecurityRulePortMax,
142 @QueryParam ("ethertype") String querySecurityRuleEthertype,
143 @QueryParam ("remote_ip_prefix") String querySecurityRuleIpPrefix,
144 @QueryParam ("remote_group_id") String querySecurityRemoteGroupID,
145 @QueryParam ("security_group_id") String querySecurityRuleGroupID,
146 @QueryParam ("tenant_id") String querySecurityRuleTenantID,
147 @QueryParam ("limit") String limit,
148 @QueryParam ("marker") String marker,
149 @QueryParam ("page_reverse") String pageReverse
151 INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
152 List<NeutronSecurityRule> allSecurityRules = securityRuleInterface.getAllNeutronSecurityRules();
153 List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
154 Iterator<NeutronSecurityRule> i = allSecurityRules.iterator();
155 while (i.hasNext()) {
156 NeutronSecurityRule nsr = i.next();
157 if ((querySecurityRuleUUID == null ||
158 querySecurityRuleUUID.equals(nsr.getID())) &&
159 (querySecurityRuleDirection == null ||
160 querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection())) &&
161 (querySecurityRuleProtocol == null ||
162 querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol())) &&
163 (querySecurityRulePortMin == null ||
164 querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin())) &&
165 (querySecurityRulePortMax == null ||
166 querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax())) &&
167 (querySecurityRuleEthertype == null ||
168 querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype())) &&
169 (querySecurityRuleIpPrefix == null ||
170 querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix())) &&
171 (querySecurityRuleGroupID == null ||
172 querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID())) &&
173 (querySecurityRemoteGroupID == null ||
174 querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID())) &&
175 (querySecurityRuleTenantID == null ||
176 querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
177 if (fields.size() > 0) {
178 ans.add(extractFields(nsr, fields));
184 return Response.status(HttpURLConnection.HTTP_OK).entity(
185 new NeutronSecurityRuleRequest(ans)).build();
189 * Returns a specific Security Rule
192 @Path ("{securityRuleUUID}")
194 @Produces ({MediaType.APPLICATION_JSON})
196 @ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
197 @ResponseCode (code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
198 @ResponseCode (code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
199 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
200 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
201 public Response showSecurityRule(@PathParam ("securityRuleUUID") String securityRuleUUID,
203 @QueryParam ("fields") List<String> fields) {
204 return show(securityRuleUUID, fields);
208 * Creates new Security Rule
212 @Produces ({MediaType.APPLICATION_JSON})
213 @Consumes ({MediaType.APPLICATION_JSON})
215 @ResponseCode (code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
216 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
217 public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
218 return create(input);
222 * Updates a Security Rule
225 @Path ("{securityRuleUUID}")
227 @Produces ({MediaType.APPLICATION_JSON})
228 @Consumes ({MediaType.APPLICATION_JSON})
230 @ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
231 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
232 public Response updateSecurityRule(
233 @PathParam ("securityRuleUUID") String securityRuleUUID, final NeutronSecurityRuleRequest input) {
234 return update(securityRuleUUID, input);
238 * Deletes a Security Rule
241 @Path ("{securityRuleUUID}")
244 @ResponseCode (code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
245 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
246 public Response deleteSecurityRule(
247 @PathParam ("securityRuleUUID") String securityRuleUUID) {
248 return delete(securityRuleUUID);