AbstractServiceInstance is fully self-suffient to program the default flow-rules... 51/10551/2
authorMadhu Venugopal <mavenugo@gmail.com>
Sun, 31 Aug 2014 18:10:50 +0000 (11:10 -0700)
committerMadhu Venugopal <mavenugo@gmail.com>
Sun, 31 Aug 2014 18:47:21 +0000 (11:47 -0700)
Changes includes :
- Register as Notification listener with the MD-SAL data-store.
- Process the NodeUpdate and identify openflow nodes before submitting for further processing
- Process Notifications in its own Thread
- Program the default catch-all rule

Change-Id: I302dbcd95de29cc2d7ff55791305f36a8f07456b
Signed-off-by: Madhu Venugopal <mavenugo@gmail.com>
openstack/net-virt-providers/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/providers/openflow13/AbstractServiceInstance.java
openstack/net-virt-providers/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/providers/openflow13/MdsalConsumer.java
openstack/net-virt-providers/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/providers/openflow13/MdsalConsumerImpl.java
openstack/net-virt-providers/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/providers/openflow13/services/L2RewriteService.java

index b977875bdf0f2dc7c72107f0fd0d3fe93735478d..e32d454d4e1dfed38eb331c205b1c319870314ea 100644 (file)
 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
 
 import java.util.List;
+import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.LinkedBlockingDeque;
 
+import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
@@ -29,17 +36,29 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.M
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
 
 /**
  * Any ServiceInstance class that extends AbstractServiceInstance to be a part of the pipeline
@@ -49,15 +68,30 @@ import com.google.common.util.concurrent.CheckedFuture;
  *    use it in any matching flows that needs to be further processed by next service in the pipeline.
  *
  */
-public abstract class AbstractServiceInstance {
+public abstract class AbstractServiceInstance implements OpendaylightInventoryListener, Runnable, TransactionChainListener {
     public static final String SERVICE_PROPERTY ="serviceProperty";
-    private Service service;
+    private static final Logger logger = LoggerFactory.getLogger(AbstractServiceInstance.class);
+
+    // OSGi Services that we are dependent on.
+    private volatile MdsalConsumer mdsalConsumer;
     private volatile PipelineOrchestrator orchestrator;
 
+    // Concrete Service that this AbstractServiceInstance represent
+    private Service service;
+
+    private BindingTransactionChain txChain;
+
+    // Process Notification in its own thread
+    Thread thread = null;
+    private final BlockingQueue<String> queue = new LinkedBlockingDeque<>();
+
     public AbstractServiceInstance (Service service) {
         this.service = service;
     }
 
+    // Let the Concrete service instance class decide if a Bride is part of the pipeline or not.
+    public abstract boolean isBridgeInPipeline (String nodeId);
+
     public int getTable() {
         return service.getTable();
     }
@@ -70,6 +104,21 @@ public abstract class AbstractServiceInstance {
         this.service = service;
     }
 
+    public void start() {
+        // Register for OpenFlow bridge/node Creation notification.
+        NotificationProviderService notificationService = mdsalConsumer.getNotificationService();
+        if (notificationService != null) {
+            notificationService.registerNotificationListener(this);
+        }
+        this.txChain =  mdsalConsumer.getDataBroker().createTransactionChain(this);
+
+        // Never block a Notification thread. Process the notification in its own Thread.
+        thread = new Thread(this);
+        thread.setDaemon(true);
+        thread.setName("AbstractServiceInstance-"+service.toString());
+        thread.start();
+    }
+
     private NodeBuilder createNodeBuilder(String nodeId) {
         NodeBuilder builder = new NodeBuilder();
         builder.setId(new NodeId(nodeId));
@@ -94,10 +143,7 @@ public abstract class AbstractServiceInstance {
         }
     }
 
-    private static final Logger logger = LoggerFactory.getLogger(AbstractServiceInstance.class);
-    private volatile MdsalConsumer mdsalConsumer;
-
-    public void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
+    private void writeFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
         Preconditions.checkNotNull(mdsalConsumer);
         if (mdsalConsumer == null) {
             logger.error("ERROR finding MDSAL Service. Its possible that writeFlow is called too soon ?");
@@ -170,4 +216,88 @@ public abstract class AbstractServiceInstance {
         writeFlow(flowBuilder, nodeBuilder);
     }
 
+    @Override
+    public void onNodeConnectorRemoved(NodeConnectorRemoved nodeConector) {
+    }
+
+    @Override
+    public void onNodeConnectorUpdated(NodeConnectorUpdated nodeConnector) {
+    }
+
+    @Override
+    public void onNodeRemoved(NodeRemoved node) {
+    }
+
+
+    @Override
+    public void run() {
+        try {
+            for (; ; ) {
+                String nodeId = queue.take();
+                this.programDefaultPipelineRule(nodeId);
+            }
+        } catch (InterruptedException e) {
+            logger.warn("Processing interrupted, terminating", e);
+        }
+
+        while (!queue.isEmpty()) {
+            queue.poll();
+        }
+
+    }
+
+    void enqueue(final String nodeId) {
+        try {
+            queue.put(nodeId);
+        } catch (InterruptedException e) {
+            logger.warn("Failed to enqueue operation {}", nodeId, e);
+        }
+    }
+
+    /**
+     * Process the Node update notification. Check for Openflow node and make sure if the bridge is part of the Pipeline before
+     * programming the Pipeline specific flows.
+     */
+    @Override
+    public void onNodeUpdated(NodeUpdated nodeUpdated) {
+        NodeRef ref = nodeUpdated.getNodeRef();
+        InstanceIdentifier<Node> identifier = (InstanceIdentifier<Node>) ref.getValue();
+        logger.info("GOT NOTIFICATION FOR "+identifier.toString());
+        final NodeKey key = identifier.firstKeyOf(Node.class, NodeKey.class);
+        final String nodeId = key.getId().getValue();
+        if (!this.isBridgeInPipeline(nodeId)) {
+            logger.debug("Bridge {} is not in pipeline", nodeId);
+            return;
+        }
+        if (key != null && key.getId().getValue().contains("openflow")) {
+            InstanceIdentifierBuilder<Node> builder = ((InstanceIdentifier<Node>) ref.getValue()).builder();
+            InstanceIdentifierBuilder<FlowCapableNode> augmentation = builder.augmentation(FlowCapableNode.class);
+            final InstanceIdentifier<FlowCapableNode> path = augmentation.build();
+            CheckedFuture readFuture = txChain.newReadWriteTransaction().read(LogicalDatastoreType.OPERATIONAL, path);
+            Futures.addCallback(readFuture, new FutureCallback<Optional<? extends DataObject>>() {
+                @Override
+                public void onSuccess(Optional<? extends DataObject> optional) {
+                    if (!optional.isPresent()) {
+                        enqueue(nodeId);
+                    }
+                }
+
+                @Override
+                public void onFailure(Throwable throwable) {
+                    logger.debug(String.format("Can't retrieve node data for node %s. Writing node data with table0.", nodeId));
+                    enqueue(nodeId);
+                }
+            });
+        }
+    }
+
+    @Override
+    public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
+            final Throwable cause) {
+        logger.error("Failed to export Flow Capable Inventory, Transaction {} failed.",transaction.getIdentifier(),cause);
+    }
+
+    @Override
+    public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
+    }
 }
index 41f3e160c026bdd021d51afb8ae20605b99d22d8..8c57d8dcf1fb300bc3d9cc0f745b32688512201c 100644 (file)
@@ -12,8 +12,10 @@ package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 
 public interface MdsalConsumer {
     public ConsumerContext getConsumerContext();
     public DataBroker getDataBroker();
+    public NotificationProviderService getNotificationService();
 }
index 5b575592b8c24fa4cd46050b042954721e3c505a..4bf626417b3b6c55df2f8d62a0fcffd775b3a83f 100644 (file)
 
 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
 
+import java.util.Collection;
+
+import org.apache.felix.dm.Component;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
-
-import org.apache.felix.dm.Component;
+import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.yangtools.yang.binding.RpcService;
 import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-public class MdsalConsumerImpl implements BindingAwareConsumer, MdsalConsumer {
+public class MdsalConsumerImpl implements BindingAwareConsumer, MdsalConsumer, BindingAwareProvider {
 
     private BundleContext ctx = null;
     private volatile BindingAwareBroker broker;
     private ConsumerContext consumerContext = null;
     private DataBroker dataBroker;
+    private NotificationProviderService notificationService;
 
     static final Logger logger = LoggerFactory.getLogger(MdsalConsumerImpl.class);
 
@@ -32,6 +38,7 @@ public class MdsalConsumerImpl implements BindingAwareConsumer, MdsalConsumer {
         this.ctx = c.getDependencyManager().getBundleContext();
         logger.info("Open vSwitch OpenFlow 1.3 Neutron Networking Provider Registered with MD-SAL");
         broker.registerConsumer(this, this.ctx);
+        broker.registerProvider(this, this.ctx);
     }
 
     void destroy() {
@@ -64,4 +71,25 @@ public class MdsalConsumerImpl implements BindingAwareConsumer, MdsalConsumer {
     public DataBroker getDataBroker() {
         return dataBroker;
     }
+    @Override
+    public NotificationProviderService getNotificationService() {
+        return notificationService;
+    }
+
+    @Override
+    public Collection<? extends ProviderFunctionality> getFunctionality() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public Collection<? extends RpcService> getImplementations() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void onSessionInitiated(ProviderContext session) {
+        notificationService = session.getSALService(NotificationProviderService.class);
+    }
 }
index 29a95b28c3b3fcd71e06beef069bd3338c6c057d..1b68a94f39db77d290e62470df9ae99c18f3b0fe 100644 (file)
@@ -20,4 +20,9 @@ public class L2RewriteService extends AbstractServiceInstance {
     public L2RewriteService(Service service) {
         super(service);
     }
-}
+
+    @Override
+    public boolean isBridgeInPipeline (String nodeId) {
+        return true;
+    }
+}
\ No newline at end of file