package org.opendaylight.neutron.northbound.api;
+import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
@Path("/fw/firewalls_policies")
public class NeutronFirewallPolicyNorthbound {
+ private static final int HTTP_OK_BOTTOM = 200;
+ private static final int HTTP_OK_TOP = 299;
+ private static final String INTERFACE_NAME = "Firewall Policy CRUD Interface";
+ private static final String UUID_NO_EXIST = "Firewall Policy UUID does not exist.";
+ private static final String NO_PROVIDERS = "No providers registered. Please try again later";
+ private static final String NO_PROVIDER_LIST = "Couldn't get providers list. Please try again later";
+
private NeutronFirewallPolicy extractFields(NeutronFirewallPolicy o, List<String> fields) {
return o.extractFields(fields);
}
+ private NeutronCRUDInterfaces getNeutronInterfaces() {
+ NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronFirewallPolicyCRUD(this);
+ if (answer.getFirewallPolicyInterface() == null) {
+ throw new ServiceUnavailableException(INTERFACE_NAME
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+ return answer;
+ }
+
/**
* Returns a list of all Firewall Policies */
@GET
@Produces({ MediaType.APPLICATION_JSON })
@StatusCodes({
- @ResponseCode(code = 200, condition = "Operation successful"),
- @ResponseCode(code = 401, condition = "Unauthorized"),
- @ResponseCode(code = 501, condition = "Not Implemented"),
- @ResponseCode(code = 503, condition = "No providers available") })
+ @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 listGroups(
// return fields
@QueryParam("page_reverse") String pageReverse
// sorting not supported
) {
- INeutronFirewallPolicyCRUD firewallPolicyInterface = NeutronCRUDInterfaces.getINeutronFirewallPolicyCRUD(this);
-
- if (firewallPolicyInterface == null) {
- throw new ServiceUnavailableException("Firewall Policy CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
- List<NeutronFirewallPolicy> allFirewallPolicies = firewallPolicyInterface.getAllNeutronFirewallPolicies();
+ INeutronFirewallPolicyCRUD firewallPolicyInterface = getNeutronInterfaces().getFirewallPolicyInterface();
List<NeutronFirewallPolicy> ans = new ArrayList<NeutronFirewallPolicy>();
- Iterator<NeutronFirewallPolicy> i = allFirewallPolicies.iterator();
- while (i.hasNext()) {
- NeutronFirewallPolicy nsg = i.next();
+ for (NeutronFirewallPolicy nsg : firewallPolicyInterface.getAllNeutronFirewallPolicies()) {
if ((queryFirewallPolicyUUID == null ||
queryFirewallPolicyUUID.equals(nsg.getFirewallPolicyUUID())) &&
(queryFirewallPolicyTenantID == null ||
ans.add(nsg);
}
}
- } // ans.add((NeutronFirewallPolicy) rules);
+ }
//TODO: apply pagination to results
- return Response.status(200).entity(
+ return Response.status(HttpURLConnection.HTTP_OK).entity(
new NeutronFirewallPolicyRequest(ans)).build();
}
@GET
@Produces({ MediaType.APPLICATION_JSON })
@StatusCodes({
- @ResponseCode(code = 200, condition = "Operation successful"),
- @ResponseCode(code = 401, condition = "Unauthorized"),
- @ResponseCode(code = 404, condition = "Not Found"),
- @ResponseCode(code = 501, condition = "Not Implemented"),
- @ResponseCode(code = 503, condition = "No providers available") })
+ @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 showFirewallPolicy(@PathParam("firewallPolicyUUID") String firewallPolicyUUID,
// return fields
@QueryParam("fields") List<String> fields) {
- INeutronFirewallPolicyCRUD firewallPolicyInterface = NeutronCRUDInterfaces.getINeutronFirewallPolicyCRUD(this);
- if (firewallPolicyInterface == null) {
- throw new ServiceUnavailableException("Firewall Policy CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
+ INeutronFirewallPolicyCRUD firewallPolicyInterface = getNeutronInterfaces().getFirewallPolicyInterface();
if (!firewallPolicyInterface.neutronFirewallPolicyExists(firewallPolicyUUID)) {
- throw new ResourceNotFoundException("Firewall Policy UUID does not exist.");
+ throw new ResourceNotFoundException(UUID_NO_EXIST);
}
if (fields.size() > 0) {
NeutronFirewallPolicy ans = firewallPolicyInterface.getNeutronFirewallPolicy(firewallPolicyUUID);
- return Response.status(200).entity(
+ return Response.status(HttpURLConnection.HTTP_OK).entity(
new NeutronFirewallPolicyRequest(extractFields(ans, fields))).build();
} else {
- return Response.status(200).entity(new NeutronFirewallPolicyRequest(firewallPolicyInterface.getNeutronFirewallPolicy(firewallPolicyUUID))).build();
+ return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronFirewallPolicyRequest(firewallPolicyInterface.getNeutronFirewallPolicy(firewallPolicyUUID))).build();
}
}
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@StatusCodes({
- @ResponseCode(code = 201, condition = "Created"),
- @ResponseCode(code = 400, condition = "Bad Request"),
- @ResponseCode(code = 401, condition = "Unauthorized"),
- @ResponseCode(code = 403, condition = "Forbidden"),
- @ResponseCode(code = 404, condition = "Not Found"),
- @ResponseCode(code = 409, condition = "Conflict"),
- @ResponseCode(code = 501, condition = "Not Implemented"),
- @ResponseCode(code = 503, condition = "No providers available") })
+ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
+ @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
+ @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+ @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
+ @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+ @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
+ @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+ @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
public Response createFirewallPolicies(final NeutronFirewallPolicyRequest input) {
- INeutronFirewallPolicyCRUD firewallPolicyInterface = NeutronCRUDInterfaces.getINeutronFirewallPolicyCRUD(this);
- if (firewallPolicyInterface == null) {
- throw new ServiceUnavailableException("Firewall Policy CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
+ INeutronFirewallPolicyCRUD firewallPolicyInterface = getNeutronInterfaces().getFirewallPolicyInterface();
if (input.isSingleton()) {
NeutronFirewallPolicy singleton = input.getSingleton();
- /*
- * Verify that the Firewall Policy doesn't already exist.
- */
- if (firewallPolicyInterface.neutronFirewallPolicyExists(singleton.getFirewallPolicyUUID())) {
- throw new BadRequestException("Firewall Policy UUID already exists");
- }
- firewallPolicyInterface.addNeutronFirewallPolicy(singleton);
-
Object[] instances = NeutronUtil.getInstances(INeutronFirewallPolicyAware.class, this);
if (instances != null) {
if (instances.length > 0) {
for (Object instance : instances) {
INeutronFirewallPolicyAware service = (INeutronFirewallPolicyAware) instance;
int status = service.canCreateNeutronFirewallPolicy(singleton);
- if (status < 200 || status > 299) {
+ if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
return Response.status(status).build();
}
}
} else {
- throw new ServiceUnavailableException("No providers registered. Please try again later");
+ throw new ServiceUnavailableException(NO_PROVIDERS);
}
} else {
- throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
+ throw new ServiceUnavailableException(NO_PROVIDER_LIST);
}
firewallPolicyInterface.addNeutronFirewallPolicy(singleton);
if (instances != null) {
}
}
} else {
- List<NeutronFirewallPolicy> bulk = input.getBulk();
- Iterator<NeutronFirewallPolicy> i = bulk.iterator();
- HashMap<String, NeutronFirewallPolicy> testMap = new HashMap<String, NeutronFirewallPolicy>();
Object[] instances = NeutronUtil.getInstances(INeutronFirewallPolicyAware.class, this);
- while (i.hasNext()) {
- NeutronFirewallPolicy test = i.next();
-
- /*
- * Verify that the firewall policy doesn't already exist
- */
-
- if (firewallPolicyInterface.neutronFirewallPolicyExists(test.getFirewallPolicyUUID())) {
- throw new BadRequestException("Firewall Policy UUID already is already created");
- }
- if (testMap.containsKey(test.getFirewallPolicyUUID())) {
- throw new BadRequestException("Firewall Policy UUID already exists");
- }
+ for (NeutronFirewallPolicy test : input.getBulk()) {
if (instances != null) {
if (instances.length > 0) {
for (Object instance : instances) {
INeutronFirewallPolicyAware service = (INeutronFirewallPolicyAware) instance;
int status = service.canCreateNeutronFirewallPolicy(test);
- if (status < 200 || status > 299) {
+ if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
return Response.status(status).build();
}
}
} else {
- throw new ServiceUnavailableException("No providers registered. Please try again later");
+ throw new ServiceUnavailableException(NO_PROVIDERS);
}
} else {
- throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
+ throw new ServiceUnavailableException(NO_PROVIDER_LIST);
}
}
/*
* now, each element of the bulk request can be added to the cache
*/
- i = bulk.iterator();
- while (i.hasNext()) {
- NeutronFirewallPolicy test = i.next();
+ for (NeutronFirewallPolicy test : input.getBulk()) {
firewallPolicyInterface.addNeutronFirewallPolicy(test);
if (instances != null) {
for (Object instance : instances) {
}
}
}
- return Response.status(201).entity(input).build();
+ return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
}
/**
@Consumes({ MediaType.APPLICATION_JSON })
//@TypeHint(OpenStackSubnets.class)
@StatusCodes({
- @ResponseCode(code = 200, condition = "Operation successful"),
- @ResponseCode(code = 400, condition = "Bad Request"),
- @ResponseCode(code = 401, condition = "Unauthorized"),
- @ResponseCode(code = 403, condition = "Forbidden"),
- @ResponseCode(code = 404, condition = "Not Found"),
- @ResponseCode(code = 501, condition = "Not Implemented"),
- @ResponseCode(code = 503, condition = "No providers available") })
+ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+ @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
+ @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+ @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
+ @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 updateFirewallPolicy(
@PathParam("firewallPolicyUUID") String firewallPolicyUUID, final NeutronFirewallPolicyRequest input) {
- INeutronFirewallPolicyCRUD firewallPolicyInterface = NeutronCRUDInterfaces.getINeutronFirewallPolicyCRUD(this);
- if (firewallPolicyInterface == null) {
- throw new ServiceUnavailableException("Firewall Policy CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
+ INeutronFirewallPolicyCRUD firewallPolicyInterface = getNeutronInterfaces().getFirewallPolicyInterface();
- /*
- * verify the Firewall Policy exists and there is only one delta provided
- */
- if (!firewallPolicyInterface.neutronFirewallPolicyExists(firewallPolicyUUID)) {
- throw new ResourceNotFoundException("Firewall Policy UUID does not exist.");
- }
- if (!input.isSingleton()) {
- throw new BadRequestException("Only singleton edit supported");
- }
NeutronFirewallPolicy delta = input.getSingleton();
NeutronFirewallPolicy original = firewallPolicyInterface.getNeutronFirewallPolicy(firewallPolicyUUID);
- /*
- * updates restricted by Neutron
- */
- if (delta.getFirewallPolicyUUID() != null ||
- delta.getFirewallPolicyTenantID() != null ||
- delta.getFirewallPolicyName() != null ||
- delta.getFirewallPolicyDescription() != null ||
- delta.getFirewallPolicyIsShared() != null ||
- delta.getFirewallPolicyRules().size() > 0 ||
- delta.getFirewallPolicyIsAudited() != null) {
- throw new BadRequestException("Attribute edit blocked by Neutron");
- }
-
Object[] instances = NeutronUtil.getInstances(INeutronFirewallPolicyAware.class, this);
if (instances != null) {
if (instances.length > 0) {
for (Object instance : instances) {
INeutronFirewallPolicyAware service = (INeutronFirewallPolicyAware) instance;
int status = service.canUpdateNeutronFirewallPolicy(delta, original);
- if (status < 200 || status > 299) {
+ if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
return Response.status(status).build();
}
}
} else {
- throw new ServiceUnavailableException("No providers registered. Please try again later");
+ throw new ServiceUnavailableException(NO_PROVIDERS);
}
} else {
- throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
+ throw new ServiceUnavailableException(NO_PROVIDER_LIST);
}
/*
service.neutronFirewallPolicyUpdated(updatedFirewallPolicy);
}
}
- return Response.status(200).entity(new NeutronFirewallPolicyRequest(firewallPolicyInterface.getNeutronFirewallPolicy(firewallPolicyUUID))).build();
+ return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronFirewallPolicyRequest(firewallPolicyInterface.getNeutronFirewallPolicy(firewallPolicyUUID))).build();
}
/**
@Path("{firewallPolicyUUID}")
@DELETE
@StatusCodes({
- @ResponseCode(code = 204, condition = "No Content"),
- @ResponseCode(code = 401, condition = "Unauthorized"),
- @ResponseCode(code = 404, condition = "Not Found"),
- @ResponseCode(code = 409, condition = "Conflict"),
- @ResponseCode(code = 501, condition = "Not Implemented"),
- @ResponseCode(code = 503, condition = "No providers available") })
+ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
+ @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+ @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+ @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
+ @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+ @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
public Response deleteFirewallPolicy(
@PathParam("firewallPolicyUUID") String firewallPolicyUUID) {
- INeutronFirewallPolicyCRUD firewallPolicyInterface = NeutronCRUDInterfaces.getINeutronFirewallPolicyCRUD(this);
- if (firewallPolicyInterface == null) {
- throw new ServiceUnavailableException("Firewall Policy CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
+ INeutronFirewallPolicyCRUD firewallPolicyInterface = getNeutronInterfaces().getFirewallPolicyInterface();
- /*
- * verify the Firewall Policy exists and it isn't currently in use
- */
- if (!firewallPolicyInterface.neutronFirewallPolicyExists(firewallPolicyUUID)) {
- throw new ResourceNotFoundException("Firewall Policy UUID does not exist.");
- }
- if (firewallPolicyInterface.neutronFirewallPolicyInUse(firewallPolicyUUID)) {
- return Response.status(409).build();
- }
NeutronFirewallPolicy singleton = firewallPolicyInterface.getNeutronFirewallPolicy(firewallPolicyUUID);
Object[] instances = NeutronUtil.getInstances(INeutronFirewallPolicyAware.class, this);
if (instances != null) {
for (Object instance : instances) {
INeutronFirewallPolicyAware service = (INeutronFirewallPolicyAware) instance;
int status = service.canDeleteNeutronFirewallPolicy(singleton);
- if (status < 200 || status > 299) {
+ if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
return Response.status(status).build();
}
}
} else {
- throw new ServiceUnavailableException("No providers registered. Please try again later");
+ throw new ServiceUnavailableException(NO_PROVIDERS);
}
} else {
- throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
+ throw new ServiceUnavailableException(NO_PROVIDER_LIST);
}
firewallPolicyInterface.removeNeutronFirewallPolicy(firewallPolicyUUID);
service.neutronFirewallPolicyDeleted(singleton);
}
}
- return Response.status(204).build();
+ return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
}
}