/*
- * Copyright IBM Corporation, 2015. All rights reserved.
+ * Copyright (c) 2015 IBM Corporation 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,
import java.net.HttpURLConnection;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
-import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import org.codehaus.enunciate.jaxrs.ResponseCode;
import org.codehaus.enunciate.jaxrs.StatusCodes;
-import org.codehaus.enunciate.jaxrs.TypeHint;
import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleAware;
import org.opendaylight.neutron.spi.INeutronMeteringLabelRuleCRUD;
*/
@Path("/metering/metering-label-rules")
-public class NeutronMeteringLabelRulesNorthbound {
+public class NeutronMeteringLabelRulesNorthbound
+ extends AbstractNeutronNorthboundIAware<NeutronMeteringLabelRule, NeutronMeteringLabelRuleRequest, INeutronMeteringLabelRuleCRUD, INeutronMeteringLabelRuleAware> {
+ private static final String RESOURCE_NAME = "Metering Label Rule";
- private static final int HTTP_OK_BOTTOM = 200;
- private static final int HTTP_OK_TOP = 299;
+ @Override
+ protected String getResourceName() {
+ return RESOURCE_NAME;
+ }
- private NeutronMeteringLabelRule extractFields(NeutronMeteringLabelRule o, List<String> fields) {
+ @Override
+ protected NeutronMeteringLabelRule extractFields(NeutronMeteringLabelRule o, List<String> fields) {
return o.extractFields(fields);
}
+ @Override
+ protected INeutronMeteringLabelRuleCRUD getNeutronCRUD() {
+ NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronMeteringLabelRuleCRUD(this);
+ if (answer.getMeteringLabelRuleInterface() == null) {
+ throw new ServiceUnavailableException(serviceUnavailable());
+ }
+ return answer.getMeteringLabelRuleInterface();
+ }
+
+ @Override
+ protected NeutronMeteringLabelRuleRequest newNeutronRequest(NeutronMeteringLabelRule o) {
+ return new NeutronMeteringLabelRuleRequest(o);
+ }
+
+ @Override
+ protected Object[] getInstances() {
+ return NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
+ }
+
+ @Override
+ protected int canCreate(Object instance, NeutronMeteringLabelRule singleton) {
+ INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
+ return service.canCreateMeteringLabelRule(singleton);
+ }
+
+ @Override
+ protected void created(Object instance, NeutronMeteringLabelRule singleton) {
+ INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
+ service.neutronMeteringLabelRuleCreated(singleton);
+ }
+
+ @Override
+ protected int canUpdate(Object instance, NeutronMeteringLabelRule delta, NeutronMeteringLabelRule original) {
+ return 0;
+ }
+
+ @Override
+ protected void updated(Object instance, NeutronMeteringLabelRule updated) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected int canDelete(Object instance, NeutronMeteringLabelRule singleton) {
+ INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
+ return service.canDeleteMeteringLabelRule(singleton);
+ }
+
+ @Override
+ protected void deleted(Object instance, NeutronMeteringLabelRule singleton) {
+ INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
+ service.neutronMeteringLabelRuleDeleted(singleton);
+ }
+
@Context
UriInfo uriInfo;
@QueryParam("metering_label_id") String queryLabelID
// pagination and sorting are TODO
) {
- INeutronMeteringLabelRuleCRUD ruleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
- if (ruleInterface == null) {
- throw new ServiceUnavailableException("NeutronMeteringLabelRule CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
- List<NeutronMeteringLabelRule> allNeutronMeteringLabelRules = ruleInterface.getAllNeutronMeteringLabelRules();
+ INeutronMeteringLabelRuleCRUD ruleInterface = getNeutronCRUD();
+ List<NeutronMeteringLabelRule> allNeutronMeteringLabelRule = ruleInterface.getAll();
List<NeutronMeteringLabelRule> ans = new ArrayList<NeutronMeteringLabelRule>();
- Iterator<NeutronMeteringLabelRule> i = allNeutronMeteringLabelRules.iterator();
+ Iterator<NeutronMeteringLabelRule> i = allNeutronMeteringLabelRule.iterator();
while (i.hasNext()) {
NeutronMeteringLabelRule oSS = i.next();
- if ((queryID == null || queryID.equals(oSS.getMeteringLabelRuleUUID())) &&
+ if ((queryID == null || queryID.equals(oSS.getID())) &&
(queryDirection == null || queryDirection.equals(oSS.getMeteringLabelRuleDirection())) &&
(queryRemoteIPPrefix == null || queryRemoteIPPrefix.equals(oSS.getMeteringLabelRuleRemoteIPPrefix())) &&
(queryLabelID == null || queryLabelID.equals(oSS.getMeteringLabelRuleLabelID()))) {
@PathParam("ruleUUID") String ruleUUID,
// return fields
@QueryParam("fields") List<String> fields) {
- INeutronMeteringLabelRuleCRUD ruleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
- if (ruleInterface == null) {
- throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
- if (!ruleInterface.neutronMeteringLabelRuleExists(ruleUUID)) {
- throw new ResourceNotFoundException("MeteringLabelRule UUID not found");
- }
- if (fields.size() > 0) {
- NeutronMeteringLabelRule ans = ruleInterface.getNeutronMeteringLabelRule(ruleUUID);
- return Response.status(HttpURLConnection.HTTP_OK).entity(
- new NeutronMeteringLabelRuleRequest(extractFields(ans, fields))).build();
- } else {
- return Response.status(HttpURLConnection.HTTP_OK).entity(
- new NeutronMeteringLabelRuleRequest(ruleInterface.getNeutronMeteringLabelRule(ruleUUID))).build();
- }
+ return show(ruleUUID, fields);
}
/**
//@TypeHint(NeutronNetwork.class)
@StatusCodes({
@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_NOT_IMPLEMENTED, condition = "Not Implemented"),
@ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
public Response createMeteringLabelRule(final NeutronMeteringLabelRuleRequest input) {
- INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
- if (meteringLabelRuleInterface == null) {
- throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
+ INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = getNeutronCRUD();
if (input.isSingleton()) {
NeutronMeteringLabelRule singleton = input.getSingleton();
- /*
- * verify that the meteringLabelRule doesn't already exist (issue: is deeper inspection necessary?)
- */
- if (meteringLabelRuleInterface.neutronMeteringLabelRuleExists(singleton.getMeteringLabelRuleUUID())) {
- throw new BadRequestException("meteringLabelRule UUID already exists");
- }
- Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
+ Object[] instances = getInstances();
if (instances != null) {
- if (instances.length > 0) {
- for (Object instance : instances) {
- INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
- int status = service.canCreateMeteringLabelRule(singleton);
- if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
- return Response.status(status).build();
- }
+ for (Object instance : instances) {
+ INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
+ int status = service.canCreateMeteringLabelRule(singleton);
+ 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");
}
- } else {
- throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
}
/*
@DELETE
@StatusCodes({
@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 deleteMeteringLabelRule(
@PathParam("ruleUUID") String ruleUUID) {
- INeutronMeteringLabelRuleCRUD meteringLabelRuleInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelRuleCRUD(this);
- if (meteringLabelRuleInterface == null) {
- throw new ServiceUnavailableException("MeteringLabelRule CRUD Interface "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- /*
- * verify that the meteringLabelRule exists and is not in use before removing it
- */
- if (!meteringLabelRuleInterface.neutronMeteringLabelRuleExists(ruleUUID)) {
- throw new ResourceNotFoundException("MeteringLabelRule UUID not found");
- }
- NeutronMeteringLabelRule singleton = meteringLabelRuleInterface.getNeutronMeteringLabelRule(ruleUUID);
- Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelRuleAware.class, this);
- if (instances != null) {
- if (instances.length > 0) {
- for (Object instance : instances) {
- INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
- int status = service.canDeleteMeteringLabelRule(singleton);
- 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");
- }
- } else {
- throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
- }
- meteringLabelRuleInterface.removeNeutronMeteringLabelRule(ruleUUID);
- if (instances != null) {
- for (Object instance : instances) {
- INeutronMeteringLabelRuleAware service = (INeutronMeteringLabelRuleAware) instance;
- service.neutronMeteringLabelRuleDeleted(singleton);
- }
- }
- return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
+ return delete(ruleUUID);
}
}