Add IovisorModule manager and listener 55/30355/3
authoradetalhouet <adetalhouet@inocybe.com>
Sun, 29 Nov 2015 22:32:07 +0000 (17:32 -0500)
committeradetalhouet <adetalhouet@inocybe.com>
Mon, 30 Nov 2015 23:38:57 +0000 (18:38 -0500)
Change-Id: Icf4cef2e03ef42c085aabca8a0bd0a984bb3e687
Signed-off-by: adetalhouet <adetalhouet@inocybe.com>
renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/IovisorRenderer.java
renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/endpoint/EndpointManager.java
renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/module/IovisorModuleListener.java [new file with mode: 0644]
renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/module/IovisorModuleManager.java [new file with mode: 0644]

index 1796972db8b93a0d9c8039668fd4744e4b13e6dd..abc933d018e33d4e20a8a634fa9f04b6785ecc68 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.groupbasedpolicy.renderer.iovisor;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
 import org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint.EndpointManager;
+import org.opendaylight.groupbasedpolicy.renderer.iovisor.module.IovisorModuleManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -22,10 +23,12 @@ public class IovisorRenderer implements AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(IovisorRenderer.class);
 
     private EndpointManager endPointManager;
+    private IovisorModuleManager iovisorModuleManager;
 
     public IovisorRenderer(DataBroker dataBroker, RpcProviderRegistry rpcProviderRegistry) {
         LOG.info("IOVisor Renderer has Started");
         this.endPointManager = new EndpointManager(dataBroker, rpcProviderRegistry);
+        this.iovisorModuleManager = new IovisorModuleManager(dataBroker);
     }
 
     @Override
@@ -33,5 +36,8 @@ public class IovisorRenderer implements AutoCloseable {
         if (endPointManager != null) {
             endPointManager.close();
         }
+        if (iovisorModuleManager != null) {
+            iovisorModuleManager.close();
+        }
     }
 }
index 6593852b9009c3acad2f0c64c45b038c8dd24a3f..7a311c2e2ead557022bd7f912f9ed8cdb0c812d9 100644 (file)
@@ -33,7 +33,7 @@ public class EndpointManager implements EpRendererAugmentation, AutoCloseable {
     private EndpointListener endpointListener;
 
     public EndpointManager(DataBroker dataBroker, RpcProviderRegistry rpcProviderRegistry) {
-        LOG.debug("Initialized IOVisor EndpointManager");
+        LOG.info("Initialized IOVisor EndpointManager");
         Preconditions.checkNotNull(dataBroker, "DataBroker instance must not be null");
         Preconditions.checkNotNull(rpcProviderRegistry, "RpcProviderRegistry instance must not be null");
 
diff --git a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/module/IovisorModuleListener.java b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/module/IovisorModuleListener.java
new file mode 100644 (file)
index 0000000..93cdc7f
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2015 Inocybe Technologies 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.groupbasedpolicy.renderer.iovisor.module;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.groupbasedpolicy.renderer.iovisor.utils.IovisorIidFactory;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstanceBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstanceKey;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class IovisorModuleListener implements DataChangeListener, AutoCloseable {
+
+    private static final Logger LOG = LoggerFactory.getLogger(IovisorModuleListener.class);
+
+    private final ListenerRegistration<DataChangeListener> registerListener;
+
+    public IovisorModuleListener(DataBroker dataBroker) {
+        this.registerListener = dataBroker.registerDataChangeListener(
+                                                LogicalDatastoreType.OPERATIONAL,
+                                                IovisorIidFactory.iovisorModuleInstancesIid().child(IovisorModuleInstance.class),
+                                                this,
+                                                AsyncDataBroker.DataChangeScope.SUBTREE);
+    }
+
+    @Override
+    public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
+        created(changes.getCreatedData());
+        updated(changes.getUpdatedData());
+        removed(changes);
+    }
+
+    /**
+     * Process created events.
+     *
+     * @param created
+     *            Created data
+     */
+    private void created(Map<InstanceIdentifier<?>, DataObject> created) {
+        for (Entry<InstanceIdentifier<?>, DataObject> newEndpoint : created.entrySet()) {
+            IovisorModuleInstance iovisorModuleInstance = fromMd(newEndpoint.getKey(), (IovisorModuleInstance) newEndpoint.getValue());
+            LOG.debug("IovisorModuleInstance CREATED {}", iovisorModuleInstance);
+            //TODO process created event
+        }
+    }
+
+    /**
+     * Process updated events.
+     *
+     * @param updated
+     *            updated data
+     */
+    private void updated(Map<InstanceIdentifier<?>, DataObject> updated) {
+        for (Entry<InstanceIdentifier<?>, DataObject> updatedEndpoint : updated.entrySet()) {
+            IovisorModuleInstance iovisorModuleInstance = fromMd(updatedEndpoint.getKey(), (IovisorModuleInstance) updatedEndpoint.getValue());
+            LOG.debug("IovisorModuleInstance UPDATED {}", iovisorModuleInstance);
+            //TODO process updated event
+
+        }
+    }
+
+    /**
+     * Process REMOVED data.
+     *
+     * @param changes
+     *            Changes data
+     */
+    private void removed(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
+        for (InstanceIdentifier<?> deletedEndpointPath : changes.getRemovedPaths()) {
+            IovisorModuleInstance iovisorModuleInstance = fromMd(deletedEndpointPath, (IovisorModuleInstance) changes.getOriginalData().get(deletedEndpointPath));
+            LOG.debug("IovisorModuleInstance REMOVED {}", iovisorModuleInstance);
+            // TODO process removed event
+        }
+    }
+
+    /**
+     * Get the object from MD-SAL based on the instance identifier.
+     *
+     * @param iid
+     *            {@link InstanceIdentifier} of the related event
+     * @param iovisorModuleInstance
+     *            {@link IovisorModuleInstance} from the related event
+     * @return IovisorModuleInstance constructed from the one gathered in the related event
+     */
+    private IovisorModuleInstance fromMd(InstanceIdentifier<?> iid, IovisorModuleInstance iovisorModuleInstance) {
+        IovisorModuleInstanceBuilder result = new IovisorModuleInstanceBuilder();
+
+        final IovisorModuleInstanceKey key = iid.firstKeyOf(IovisorModuleInstance.class);
+        if (key != null) {
+            result.setKey(key);
+        }
+
+        result.setUri(iovisorModuleInstance.getUri());
+
+        return result.build();
+    }
+
+    @Override
+    public void close() throws Exception {
+        if (registerListener != null)
+            registerListener.close();
+    }
+}
diff --git a/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/module/IovisorModuleManager.java b/renderers/iovisor/src/main/java/org/opendaylight/groupbasedpolicy/renderer/iovisor/module/IovisorModuleManager.java
new file mode 100644 (file)
index 0000000..0215d3b
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2015 Inocybe Technologies 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.groupbasedpolicy.renderer.iovisor.module;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+
+public class IovisorModuleManager implements AutoCloseable {
+
+    private static final Logger LOG = LoggerFactory.getLogger(IovisorModuleManager.class);
+
+    private IovisorModuleListener iovisorModuleListener;
+
+    public IovisorModuleManager(DataBroker dataBroker) {
+        LOG.info("Initialized IOVisor IovisorModuleManager");
+        Preconditions.checkNotNull(dataBroker, "DataBroker instance must not be null");
+
+        this.iovisorModuleListener = new IovisorModuleListener(dataBroker);
+    }
+
+    @Override
+    public void close() throws Exception {
+        if (iovisorModuleListener != null) {
+            iovisorModuleListener.close();
+        }
+    }
+}