Fix for Bug 3902 : Removal of the Unused imports from neutron.
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronMeteringLabelRulesNorthbound.java
index 8277b6ad8c37440081f7f3317d5eaabc89ea8260..f8fd38cd24d073e3abbf976c188b5990d11bd2e7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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,
@@ -11,13 +11,11 @@ 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 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;
@@ -31,7 +29,6 @@ import javax.ws.rs.core.UriInfo;
 
 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;
@@ -57,15 +54,73 @@ import org.opendaylight.neutron.spi.NeutronMeteringLabelRule;
  */
 
 @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;
 
@@ -90,17 +145,13 @@ public class NeutronMeteringLabelRulesNorthbound {
             @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()))) {
@@ -133,22 +184,7 @@ public class NeutronMeteringLabelRulesNorthbound {
             @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);
     }
 
     /**
@@ -159,40 +195,21 @@ public class NeutronMeteringLabelRulesNorthbound {
     //@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");
             }
 
             /*
@@ -222,49 +239,10 @@ public class NeutronMeteringLabelRulesNorthbound {
     @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);
     }
 }