/*
* Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.neutron.northbound.api;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.codehaus.enunciate.jaxrs.ResponseCode;
import org.codehaus.enunciate.jaxrs.StatusCodes;
import org.opendaylight.neutron.spi.INeutronSecurityRuleAware;
import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
import org.opendaylight.neutron.spi.NeutronSecurityRule;
/**
* Neutron Northbound REST APIs for Security Rule.
* This class provides REST APIs for managing neutron Security Rule
*
*
*
* Authentication scheme : HTTP Basic
* Authentication realm : opendaylight
* Transport : HTTP and HTTPS
*
* HTTPS Authentication is disabled by default. Administrator can enable it in
* tomcat-server.xml after adding a proper keystore / SSL certificate from a
* trusted authority.
* More info :
* http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
*/
@Path ("/security-group-rules")
public class NeutronSecurityRulesNorthbound
extends AbstractNeutronNorthbound {
private static final String RESOURCE_NAME = "Security Rule";
@Override
protected String getResourceName() {
return RESOURCE_NAME;
}
@Override
protected NeutronSecurityRule extractFields(NeutronSecurityRule o, List fields) {
return o.extractFields(fields);
}
@Override
protected NeutronSecurityRuleRequest newNeutronRequest(NeutronSecurityRule o) {
return new NeutronSecurityRuleRequest(o);
}
@Override
protected INeutronSecurityRuleCRUD getNeutronCRUD() {
NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSecurityRuleCRUD(this);
if (answer.getSecurityRuleInterface() == null) {
throw new ServiceUnavailableException(serviceUnavailable());
}
return answer.getSecurityRuleInterface();
}
@Override
protected Object[] getInstances() {
return NeutronUtil.getInstances(INeutronSecurityRuleAware.class, this);
}
@Override
protected int canCreate(Object instance, NeutronSecurityRule singleton) {
INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
return service.canCreateNeutronSecurityRule(singleton);
}
@Override
protected void created(Object instance, NeutronSecurityRule singleton) {
INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
service.neutronSecurityRuleCreated(singleton);
}
@Override
protected int canUpdate(Object instance, NeutronSecurityRule delta, NeutronSecurityRule original) {
INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
return service.canUpdateNeutronSecurityRule(delta, original);
}
@Override
protected void updated(Object instance, NeutronSecurityRule updated) {
INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
service.neutronSecurityRuleUpdated(updated);
}
@Override
protected int canDelete(Object instance, NeutronSecurityRule singleton) {
INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
return service.canDeleteNeutronSecurityRule(singleton);
}
@Override
protected void deleted(Object instance, NeutronSecurityRule singleton) {
INeutronSecurityRuleAware service = (INeutronSecurityRuleAware) instance;
service.neutronSecurityRuleDeleted(singleton);
}
/**
* Returns a list of all Security Rules
*/
@GET
@Produces ({MediaType.APPLICATION_JSON})
@StatusCodes ({
@ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
@ResponseCode (code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
@ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
@ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
public Response listRules(
// return fields
@QueryParam ("fields") List fields,
// OpenStack security rule attributes
@QueryParam ("id") String querySecurityRuleUUID,
@QueryParam ("direction") String querySecurityRuleDirection,
@QueryParam ("protocol") String querySecurityRuleProtocol,
@QueryParam ("port_range_min") Integer querySecurityRulePortMin,
@QueryParam ("port_range_max") Integer querySecurityRulePortMax,
@QueryParam ("ethertype") String querySecurityRuleEthertype,
@QueryParam ("remote_ip_prefix") String querySecurityRuleIpPrefix,
@QueryParam ("remote_group_id") String querySecurityRemoteGroupID,
@QueryParam ("security_group_id") String querySecurityRuleGroupID,
@QueryParam ("tenant_id") String querySecurityRuleTenantID,
@QueryParam ("limit") String limit,
@QueryParam ("marker") String marker,
@QueryParam ("page_reverse") String pageReverse
) {
INeutronSecurityRuleCRUD securityRuleInterface = getNeutronCRUD();
List allSecurityRules = securityRuleInterface.getAllNeutronSecurityRules();
List ans = new ArrayList();
Iterator i = allSecurityRules.iterator();
while (i.hasNext()) {
NeutronSecurityRule nsr = i.next();
if ((querySecurityRuleUUID == null ||
querySecurityRuleUUID.equals(nsr.getID())) &&
(querySecurityRuleDirection == null ||
querySecurityRuleDirection.equals(nsr.getSecurityRuleDirection())) &&
(querySecurityRuleProtocol == null ||
querySecurityRuleProtocol.equals(nsr.getSecurityRuleProtocol())) &&
(querySecurityRulePortMin == null ||
querySecurityRulePortMin.equals(nsr.getSecurityRulePortMin())) &&
(querySecurityRulePortMax == null ||
querySecurityRulePortMax.equals(nsr.getSecurityRulePortMax())) &&
(querySecurityRuleEthertype == null ||
querySecurityRuleEthertype.equals(nsr.getSecurityRuleEthertype())) &&
(querySecurityRuleIpPrefix == null ||
querySecurityRuleIpPrefix.equals(nsr.getSecurityRuleRemoteIpPrefix())) &&
(querySecurityRuleGroupID == null ||
querySecurityRuleGroupID.equals(nsr.getSecurityRuleGroupID())) &&
(querySecurityRemoteGroupID == null ||
querySecurityRemoteGroupID.equals(nsr.getSecurityRemoteGroupID())) &&
(querySecurityRuleTenantID == null ||
querySecurityRuleTenantID.equals(nsr.getTenantID()))) {
if (fields.size() > 0) {
ans.add(extractFields(nsr, fields));
} else {
ans.add(nsr);
}
}
}
return Response.status(HttpURLConnection.HTTP_OK).entity(
new NeutronSecurityRuleRequest(ans)).build();
}
/**
* Returns a specific Security Rule
*/
@Path ("{securityRuleUUID}")
@GET
@Produces ({MediaType.APPLICATION_JSON})
@StatusCodes ({
@ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
@ResponseCode (code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
@ResponseCode (code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
@ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
@ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
public Response showSecurityRule(@PathParam ("securityRuleUUID") String securityRuleUUID,
// return fields
@QueryParam ("fields") List fields) {
return show(securityRuleUUID, fields);
}
/**
* Creates new Security Rule
*/
@POST
@Produces ({MediaType.APPLICATION_JSON})
@Consumes ({MediaType.APPLICATION_JSON})
@StatusCodes ({
@ResponseCode (code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
@ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
public Response createSecurityRules(final NeutronSecurityRuleRequest input) {
return create(input);
}
/**
* Updates a Security Rule
*/
@Path ("{securityRuleUUID}")
@PUT
@Produces ({MediaType.APPLICATION_JSON})
@Consumes ({MediaType.APPLICATION_JSON})
@StatusCodes ({
@ResponseCode (code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
@ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
public Response updateSecurityRule(
@PathParam ("securityRuleUUID") String securityRuleUUID, final NeutronSecurityRuleRequest input) {
return update(securityRuleUUID, input);
}
/**
* Deletes a Security Rule
*/
@Path ("{securityRuleUUID}")
@DELETE
@StatusCodes ({
@ResponseCode (code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
@ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
public Response deleteSecurityRule(
@PathParam ("securityRuleUUID") String securityRuleUUID) {
return delete(securityRuleUUID);
}
}