Implement NeutronMeteringLabelsNorthbound methods 75/17875/4
authorRyan Moats <rmoats@us.ibm.com>
Tue, 7 Apr 2015 19:50:57 +0000 (14:50 -0500)
committerRyan Moats <rmoats@us.ibm.com>
Wed, 8 Apr 2015 14:54:33 +0000 (09:54 -0500)
Change-Id: Ibfb378e0302fb0b3dbabdbd9a4138c0bdad3085a
Signed-off-by: Ryan Moats <rmoats@us.ibm.com>
neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronLoadBalancerPoolMemberRequest.java [deleted file]
neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronMeteringLabelCRUD.java [new file with mode: 0644]
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronCRUDInterfaces.java
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronMeteringLabel.java
neutron-spi/src/test/java/org/opendaylight/neutron/spi/NeutronMeteringLabelJAXBTest.java
northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronMeteringLabelRequest.java
northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronMeteringLabelsNorthbound.java
transcriber/src/main/java/org/opendaylight/neutron/transcriber/Activator.java
transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronMeteringLabelInterface.java [new file with mode: 0644]

diff --git a/neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronLoadBalancerPoolMemberRequest.java b/neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronLoadBalancerPoolMemberRequest.java
deleted file mode 100644 (file)
index 313c58c..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2014 Red Hat, Inc.  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.spi;
-
-import javax.xml.bind.annotation.XmlElement;
-import java.util.List;
-
-public class INeutronLoadBalancerPoolMemberRequest {
-
-    /**
-     * See OpenStack Network API v2.0 Reference for description of
-     * http://docs.openstack.org/api/openstack-network/2.0/content/
-     */
-
-    @XmlElement(name = "member")
-    NeutronLoadBalancerPoolMember singletonLoadBalancerPoolMember;
-
-    @XmlElement(name = "members")
-    List<NeutronLoadBalancerPoolMember> bulkRequest;
-
-    INeutronLoadBalancerPoolMemberRequest() {
-    }
-
-    public INeutronLoadBalancerPoolMemberRequest(List<NeutronLoadBalancerPoolMember> bulk) {
-        bulkRequest = bulk;
-        singletonLoadBalancerPoolMember = null;
-    }
-
-    INeutronLoadBalancerPoolMemberRequest(NeutronLoadBalancerPoolMember group) {
-        singletonLoadBalancerPoolMember = group;
-    }
-
-    public List<NeutronLoadBalancerPoolMember> getBulk() {
-        return bulkRequest;
-    }
-
-    public NeutronLoadBalancerPoolMember getSingleton() {
-        return singletonLoadBalancerPoolMember;
-    }
-
-    public boolean isSingleton() {
-        return (singletonLoadBalancerPoolMember != null);
-    }
-}
diff --git a/neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronMeteringLabelCRUD.java b/neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronMeteringLabelCRUD.java
new file mode 100644 (file)
index 0000000..7bf342b
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2014 Red Hat, Inc.  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.spi;
+
+import java.util.List;
+
+public interface INeutronMeteringLabelCRUD {
+
+    /**
+     * Applications call this interface method to determine if a particular
+     *NeutronMeteringLabel object exists
+     *
+     * @param uuid
+     *            UUID of the NeutronMeteringLabel object
+     * @return boolean
+     */
+
+    public boolean neutronMeteringLabelExists(String uuid);
+
+    /**
+     * Applications call this interface method to return if a particular
+     * NeutronMeteringLabel object exists
+     *
+     * @param uuid
+     *            UUID of the NeutronMeteringLabel object
+     * @return {@link org.opendaylight.neutron.neutron.spi.NeutronMeteringLabel}
+     *          OpenStackNeutronMeteringLabel class
+     */
+
+    public NeutronMeteringLabel getNeutronMeteringLabel(String uuid);
+
+    /**
+     * Applications call this interface method to return all NeutronMeteringLabel objects
+     *
+     * @return List of OpenStackNetworks objects
+     */
+
+    public List<NeutronMeteringLabel> getAllNeutronMeteringLabels();
+
+    /**
+     * Applications call this interface method to add a NeutronMeteringLabel object to the
+     * concurrent map
+     *
+     * @param input
+     *            OpenStackNetwork object
+     * @return boolean on whether the object was added or not
+     */
+
+    public boolean addNeutronMeteringLabel(NeutronMeteringLabel input);
+
+    /**
+     * Applications call this interface method to remove a Neutron NeutronMeteringLabel object to the
+     * concurrent map
+     *
+     * @param uuid
+     *            identifier for the NeutronMeteringLabel object
+     * @return boolean on whether the object was removed or not
+     */
+
+    public boolean removeNeutronMeteringLabel(String uuid);
+
+    /**
+     * Applications call this interface method to edit a NeutronMeteringLabel object
+     *
+     * @param uuid
+     *            identifier of the NeutronMeteringLabel object
+     * @param delta
+     *            OpenStackNeutronMeteringLabel object containing changes to apply
+     * @return boolean on whether the object was updated or not
+     */
+
+    public boolean updateNeutronMeteringLabel(String uuid, NeutronMeteringLabel delta);
+
+    /**
+     * Applications call this interface method to see if a MAC address is in use
+     *
+     * @param uuid
+     *            identifier of the NeutronMeteringLabel object
+     * @return boolean on whether the macAddress is already associated with a
+     * port or not
+     */
+
+    public boolean neutronMeteringLabelInUse(String uuid);
+
+}
index d62a5ca3d4aec00382cef5024fe0d1e55bfedd37..e1c76a5cf4d4bdae66b0e54582991dca68cdb058 100644 (file)
@@ -93,6 +93,11 @@ public class NeutronCRUDInterfaces {
         return answer;
     }
 
+    public static INeutronMeteringLabelCRUD getINeutronMeteringLabelCRUD(Object o) {
+        INeutronMeteringLabelCRUD answer = (INeutronMeteringLabelCRUD) getInstances(INeutronMeteringLabelCRUD.class, o);
+        return answer;
+    }
+
     public static Object getInstances(Class<?> clazz,Object bundle) {
         try {
             BundleContext bCtx = FrameworkUtil.getBundle(bundle.getClass())
index 8d292060862fec216bba4bbeb22f8360985b034b..21154b703234d03220a74d0a23e4ede6f163723e 100644 (file)
@@ -10,6 +10,9 @@ package org.opendaylight.neutron.spi;
 
 import java.io.Serializable;
 
+import java.util.Iterator;
+import java.util.List;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -52,19 +55,19 @@ public class NeutronMeteringLabel implements Serializable {
         this.meteringLabelName = name;
     }
 
-    public String getMeteringTenantID() {
+    public String getMeteringLabelTenantID() {
         return tenantID;
     }
 
-    public void setMeteringTenantID(String tenantID) {
+    public void setMeteringLabelTenantID(String tenantID) {
         this.tenantID = tenantID;
     }
 
-    public String getMeteringDescription() {
+    public String getMeteringLabelDescription() {
         return description;
     }
 
-    public void setMeteringDescription(String description) {
+    public void setMeteringLabelDescription(String description) {
         this.description = description;
     }
 
@@ -81,4 +84,33 @@ public class NeutronMeteringLabel implements Serializable {
             ", tenant_id=" + tenantID + "]";
     }
 
+    /**
+     * This method copies selected fields from the object and returns them
+     * as a new object, suitable for marshaling.
+     *
+     * @param fields
+     *            List of attributes to be extracted
+     * @return a NeutronMeteringLabel object with only the selected fields
+     * populated
+     */
+    public NeutronMeteringLabel extractFields(List<String> fields) {
+        NeutronMeteringLabel ans = new NeutronMeteringLabel();
+        Iterator<String> i = fields.iterator();
+        while (i.hasNext()) {
+            String s = i.next();
+            if (s.equals("id")) {
+                ans.setMeteringLabelUUID(this.getMeteringLabelUUID());
+            }
+            if (s.equals("name")) {
+                ans.setMeteringLabelName(this.getMeteringLabelName());
+            }
+            if (s.equals("tenant_id")) {
+                ans.setMeteringLabelTenantID(this.getMeteringLabelTenantID());
+            }
+            if (s.equals("description")) {
+                ans.setMeteringLabelDescription(this.getMeteringLabelDescription());
+            }
+        }
+        return ans;
+    }
 }
index 3d8a280a7bfe828b2ad8705da749bc7c605b4184..01cdc745a56a7906be331d2ef6adcedaa620ba52 100644 (file)
@@ -34,10 +34,10 @@ public class NeutronMeteringLabelJAXBTest {
                     testObject.getMeteringLabelName());
 
             Assert.assertEquals("NeutronMeteringLabel JAXB Test 3: Testing description failed",
-                    "Provides allowed address pairs", testObject.getMeteringDescription());
+                    "Provides allowed address pairs", testObject.getMeteringLabelDescription());
 
             Assert.assertEquals("NeutronMeteringLabel JAXB Test 4: Testing tenant_id failed",
-                    "9bacb3c5d39d41a79512987f338cf177", testObject.getMeteringTenantID());
+                    "9bacb3c5d39d41a79512987f338cf177", testObject.getMeteringLabelTenantID());
         } catch (Exception e) {
             Assert.fail("Test failed");
         }
index 0ab53fe1f391cb82a5f65cba0a359071288c4a20..643e88e2a93fd3ebb5a87327f4bb08cfc7f39eb1 100644 (file)
@@ -28,14 +28,31 @@ public class NeutronMeteringLabelRequest {
     @XmlElement(name="metering_label")
     NeutronMeteringLabel singletonMeteringLabel;
 
+    @XmlElement(name="metering_labels")
+    List<NeutronMeteringLabel> bulkMeteringLabels;
+
     NeutronMeteringLabelRequest() {
     }
 
     NeutronMeteringLabelRequest(NeutronMeteringLabel label) {
         singletonMeteringLabel = label;
+        bulkMeteringLabels = null;
+    }
+
+    NeutronMeteringLabelRequest(List<NeutronMeteringLabel> bulk) {
+        bulkMeteringLabels = bulk;
+        singletonMeteringLabel = null;
     }
 
     public NeutronMeteringLabel getSingleton() {
         return singletonMeteringLabel;
     }
+
+    public boolean isSingleton() {
+        return (singletonMeteringLabel != null);
+    }
+
+    public List<NeutronMeteringLabel> getBulk() {
+        return bulkMeteringLabels;
+    }
 }
index 0128d4d183d326c29e660dcc8ada0586fa51daec..938703bd5aaf99bb5c53dc5889f3ca93a667f85b 100644 (file)
@@ -31,6 +31,11 @@ import org.codehaus.enunciate.jaxrs.ResponseCode;
 import org.codehaus.enunciate.jaxrs.StatusCodes;
 import org.codehaus.enunciate.jaxrs.TypeHint;
 
+import org.opendaylight.neutron.spi.INeutronMeteringLabelAware;
+import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
+import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
+import org.opendaylight.neutron.spi.NeutronMeteringLabel;
+
 /**
  * Neutron Northbound REST APIs for Metering Lables.<br>
  * This class provides REST APIs for managing neutron metering labels
@@ -52,6 +57,10 @@ import org.codehaus.enunciate.jaxrs.TypeHint;
 @Path("/metering/metering-labels")
 public class NeutronMeteringLabelsNorthbound {
 
+    private NeutronMeteringLabel extractFields(NeutronMeteringLabel o, List<String> fields) {
+        return o.extractFields(fields);
+    }
+
     @Context
     UriInfo uriInfo;
 
@@ -62,10 +71,43 @@ public class NeutronMeteringLabelsNorthbound {
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackNetworks.class)
     @StatusCodes({
-        @ResponseCode(code = 501, condition = "Not Implemented") })
+            @ResponseCode(code = 200, condition = "Operation successful"),
+            @ResponseCode(code = 401, condition = "Unauthorized"),
+            @ResponseCode(code = 501, condition = "Not Implemented"),
+            @ResponseCode(code = 503, condition = "No providers available") })
     public Response listMeteringLabels(
+            // return fields
+            @QueryParam("fields") List<String> fields,
+            // filter fields
+            @QueryParam("id") String queryID,
+            @QueryParam("name") String queryName,
+            @QueryParam("tenant_id") String queryTenantID,
+            @QueryParam("description") String queryDescription
+            // pagination and sorting are TODO
             ) {
-        throw new UnimplementedException("Unimplemented");
+        INeutronMeteringLabelCRUD labelInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelCRUD(this);
+        if (labelInterface == null) {
+            throw new ServiceUnavailableException("NeutronMeteringLabel CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        List<NeutronMeteringLabel> allNeutronMeteringLabels = labelInterface.getAllNeutronMeteringLabels();
+        List<NeutronMeteringLabel> ans = new ArrayList<NeutronMeteringLabel>();
+        Iterator<NeutronMeteringLabel> i = allNeutronMeteringLabels.iterator();
+        while (i.hasNext()) {
+            NeutronMeteringLabel oSS = i.next();
+            if ((queryID == null || queryID.equals(oSS.getMeteringLabelUUID())) &&
+                    (queryName == null || queryName.equals(oSS.getMeteringLabelName())) &&
+                    (queryDescription == null || queryDescription.equals(oSS.getMeteringLabelDescription())) &&
+                    (queryTenantID == null || queryTenantID.equals(oSS.getMeteringLabelTenantID()))) {
+                if (fields.size() > 0)
+                    ans.add(extractFields(oSS,fields));
+                else
+                    ans.add(oSS);
+            }
+        }
+        //TODO: apply pagination to results
+        return Response.status(200).entity(
+                new NeutronMeteringLabelRequest(ans)).build();
     }
 
     /**
@@ -75,11 +117,32 @@ public class NeutronMeteringLabelsNorthbound {
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
     @StatusCodes({
-        @ResponseCode(code = 501, condition = "Not Implemented") })
+            @ResponseCode(code = 200, condition = "Operation successful"),
+            @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") })
     public Response showMeteringLabel(
-            @PathParam("labelUUID") String labelUUID
-            ) {
-        throw new UnimplementedException("Unimplemented");
+            @PathParam("labelUUID") String labelUUID,
+            // return fields
+            @QueryParam("fields") List<String> fields) {
+        INeutronMeteringLabelCRUD labelInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelCRUD(this);
+        if (labelInterface == null) {
+            throw new ServiceUnavailableException("MeteringLabel CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        if (!labelInterface.neutronMeteringLabelExists(labelUUID)) {
+            throw new ResourceNotFoundException("MeteringLabel UUID not found");
+        }
+        if (fields.size() > 0) {
+            NeutronMeteringLabel ans = labelInterface.getNeutronMeteringLabel(labelUUID);
+            return Response.status(200).entity(
+                    new NeutronMeteringLabelRequest(extractFields(ans, fields))).build();
+        } else {
+            return Response.status(200).entity(
+                    new NeutronMeteringLabelRequest(labelInterface.getNeutronMeteringLabel(labelUUID))).build();
+        }
     }
 
     /**
@@ -89,9 +152,59 @@ public class NeutronMeteringLabelsNorthbound {
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(NeutronNetwork.class)
     @StatusCodes({
-        @ResponseCode(code = 501, condition = "Not Implemented") })
+            @ResponseCode(code = 201, condition = "Created"),
+            @ResponseCode(code = 400, condition = "Bad Request"),
+            @ResponseCode(code = 401, condition = "Unauthorized"),
+            @ResponseCode(code = 501, condition = "Not Implemented"),
+            @ResponseCode(code = 503, condition = "No providers available") })
     public Response createMeteringLabel(final NeutronMeteringLabelRequest input) {
-        throw new UnimplementedException("Unimplemented");
+        INeutronMeteringLabelCRUD meteringLabelInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelCRUD(this);
+        if (meteringLabelInterface == null) {
+            throw new ServiceUnavailableException("MeteringLabel CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        if (input.isSingleton()) {
+            NeutronMeteringLabel singleton = input.getSingleton();
+
+            /*
+             * verify that the meteringLabel doesn't already exist (issue: is deeper inspection necessary?)
+             */
+            if (meteringLabelInterface.neutronMeteringLabelExists(singleton.getMeteringLabelUUID()))
+                throw new BadRequestException("meteringLabel UUID already exists");
+            Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelAware.class, this);
+            if (instances != null) {
+                if (instances.length > 0) {
+                    for (Object instance : instances) {
+                        INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
+                        int status = service.canCreateMeteringLabel(singleton);
+                        if (status < 200 || status > 299)
+                            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");
+            }
+
+            /*
+             * add meteringLabel to the cache
+             */
+            meteringLabelInterface.addNeutronMeteringLabel(singleton);
+            if (instances != null) {
+                for (Object instance : instances) {
+                    INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
+                    service.neutronMeteringLabelCreated(singleton);
+                }
+            }
+        } else {
+
+            /*
+             * only singleton meteringLabel creates supported
+             */
+            throw new BadRequestException("Only singleton meteringLabel creates supported");
+        }
+        return Response.status(201).entity(input).build();
     }
 
     /**
@@ -100,9 +213,48 @@ public class NeutronMeteringLabelsNorthbound {
     @Path("{labelUUID}")
     @DELETE
     @StatusCodes({
-        @ResponseCode(code = 501, condition = "Not Implemented") })
+            @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") })
     public Response deleteMeteringLabel(
             @PathParam("labelUUID") String labelUUID) {
-        throw new UnimplementedException("Unimplemented");
+        INeutronMeteringLabelCRUD meteringLabelInterface = NeutronCRUDInterfaces.getINeutronMeteringLabelCRUD(this);
+        if (meteringLabelInterface == null) {
+            throw new ServiceUnavailableException("MeteringLabel CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+
+        /*
+         * verify that the meteringLabel exists and is not in use before removing it
+         */
+        if (!meteringLabelInterface.neutronMeteringLabelExists(labelUUID))
+            throw new ResourceNotFoundException("MeteringLabel UUID not found");
+        NeutronMeteringLabel singleton = meteringLabelInterface.getNeutronMeteringLabel(labelUUID);
+        Object[] instances = NeutronUtil.getInstances(INeutronMeteringLabelAware.class, this);
+        if (instances != null) {
+            if (instances.length > 0) {
+                for (Object instance : instances) {
+                    INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
+                    int status = service.canDeleteMeteringLabel(singleton);
+                    if (status < 200 || status > 299)
+                        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");
+        }
+        meteringLabelInterface.removeNeutronMeteringLabel(labelUUID);
+        if (instances != null) {
+            for (Object instance : instances) {
+                INeutronMeteringLabelAware service = (INeutronMeteringLabelAware) instance;
+                service.neutronMeteringLabelDeleted(singleton);
+            }
+        }
+        return Response.status(204).build();
     }
 }
index 97e7a422db58145dd1ebd8dac174119dcb37601a..3e7d540ea8ede59d53edf5e8c84fa6f87704a2ad 100644 (file)
@@ -19,6 +19,7 @@ import org.opendaylight.neutron.spi.INeutronLoadBalancerHealthMonitorCRUD;
 import org.opendaylight.neutron.spi.INeutronLoadBalancerListenerCRUD;
 import org.opendaylight.neutron.spi.INeutronLoadBalancerPoolCRUD;
 import org.opendaylight.neutron.spi.INeutronLoadBalancerPoolMemberCRUD;
+import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
 import org.opendaylight.neutron.spi.INeutronPortCRUD;
 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
@@ -93,7 +94,7 @@ public class Activator implements BundleActivator {
 
         NeutronLoadBalancerInterface neutronLoadBalancerInterface = new NeutronLoadBalancerInterface();
         ServiceRegistration<INeutronLoadBalancerCRUD> neutronLoadBalancerInterfaceRegistration = context.registerService(INeutronLoadBalancerCRUD.class, neutronLoadBalancerInterface, null);
-        if(neutronFirewallInterfaceRegistration != null) {
+        if(neutronLoadBalancerInterfaceRegistration != null) {
             registrations.add(neutronLoadBalancerInterfaceRegistration);
         }
 
@@ -121,6 +122,12 @@ public class Activator implements BundleActivator {
             registrations.add(neutronLoadBalancerPoolMemberInterfaceRegistration);
         }
 
+        NeutronMeteringLabelInterface neutronMeteringLabelInterface = new NeutronMeteringLabelInterface();
+        ServiceRegistration<INeutronMeteringLabelCRUD> neutronMeteringLabelInterfaceRegistration = context.registerService(INeutronMeteringLabelCRUD.class, neutronMeteringLabelInterface, null);
+        if(neutronMeteringLabelInterfaceRegistration != null) {
+            registrations.add(neutronMeteringLabelInterfaceRegistration);
+        }
+
     }
 
     @Override
diff --git a/transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronMeteringLabelInterface.java b/transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronMeteringLabelInterface.java
new file mode 100644 (file)
index 0000000..2954179
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright IBM Corporation, 2013.  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.transcriber;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.opendaylight.neutron.spi.INeutronMeteringLabelCRUD;
+import org.opendaylight.neutron.spi.NeutronMeteringLabel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NeutronMeteringLabelInterface implements INeutronMeteringLabelCRUD {
+    private static final Logger logger = LoggerFactory.getLogger(NeutronMeteringLabelInterface.class);
+    private ConcurrentMap<String, NeutronMeteringLabel> meteringLabelDB = new ConcurrentHashMap<String, NeutronMeteringLabel>();
+
+
+
+
+    // this method uses reflection to update an object from it's delta.
+
+    private boolean overwrite(Object target, Object delta) {
+        Method[] methods = target.getClass().getMethods();
+
+        for(Method toMethod: methods){
+            if(toMethod.getDeclaringClass().equals(target.getClass())
+                    && toMethod.getName().startsWith("set")){
+
+                String toName = toMethod.getName();
+                String fromName = toName.replace("set", "get");
+
+                try {
+                    Method fromMethod = delta.getClass().getMethod(fromName);
+                    Object value = fromMethod.invoke(delta, (Object[])null);
+                    if(value != null){
+                        toMethod.invoke(target, value);
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    // IfNBMeteringLabelCRUD methods
+
+    @Override
+    public boolean neutronMeteringLabelExists(String uuid) {
+        return meteringLabelDB.containsKey(uuid);
+    }
+
+    @Override
+    public NeutronMeteringLabel getNeutronMeteringLabel(String uuid) {
+        if (!neutronMeteringLabelExists(uuid)) {
+            return null;
+        }
+        return meteringLabelDB.get(uuid);
+    }
+
+    @Override
+    public List<NeutronMeteringLabel> getAllNeutronMeteringLabels() {
+        Set<NeutronMeteringLabel> allMeteringLabels = new HashSet<NeutronMeteringLabel>();
+        for (Entry<String, NeutronMeteringLabel> entry : meteringLabelDB.entrySet()) {
+            NeutronMeteringLabel meteringLabel = entry.getValue();
+            allMeteringLabels.add(meteringLabel);
+        }
+        logger.debug("Exiting getAllMeteringLabels, Found {} OpenStackMeteringLabels", allMeteringLabels.size());
+        List<NeutronMeteringLabel> ans = new ArrayList<NeutronMeteringLabel>();
+        ans.addAll(allMeteringLabels);
+        return ans;
+    }
+
+    @Override
+    public boolean addNeutronMeteringLabel(NeutronMeteringLabel input) {
+        if (neutronMeteringLabelExists(input.getMeteringLabelUUID())) {
+            return false;
+        }
+        meteringLabelDB.putIfAbsent(input.getMeteringLabelUUID(), input);
+      //TODO: add code to find INeutronMeteringLabelAware services and call newtorkCreated on them
+        return true;
+    }
+
+    @Override
+    public boolean removeNeutronMeteringLabel(String uuid) {
+        if (!neutronMeteringLabelExists(uuid)) {
+            return false;
+        }
+        meteringLabelDB.remove(uuid);
+      //TODO: add code to find INeutronMeteringLabelAware services and call newtorkDeleted on them
+        return true;
+    }
+
+    @Override
+    public boolean updateNeutronMeteringLabel(String uuid, NeutronMeteringLabel delta) {
+        if (!neutronMeteringLabelExists(uuid)) {
+            return false;
+        }
+        NeutronMeteringLabel target = meteringLabelDB.get(uuid);
+        return overwrite(target, delta);
+    }
+
+    @Override
+    public boolean neutronMeteringLabelInUse(String netUUID) {
+        if (!neutronMeteringLabelExists(netUUID)) {
+            return true;
+        }
+        return false;
+    }
+}