Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / mdsalutil / mdsalutil-impl / src / main / java / org / opendaylight / vpnservice / mdsalutil / internal / MDSALManager.java
index e31ad2d002d79af309a7eae048a3e51228c80a00..08a1e657334ac684750cdb0917ebb5307f55a455 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. 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,11 +11,17 @@ package org.opendaylight.vpnservice.mdsalutil.internal;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
 import org.opendaylight.vpnservice.mdsalutil.ActionType;
 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
 import org.opendaylight.vpnservice.mdsalutil.GroupEntity;
 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
@@ -36,11 +42,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 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.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.opendaylight.vpnservice.mdsalutil.*;
 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.DataChangeScope;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
@@ -57,6 +67,11 @@ public class MDSALManager implements AutoCloseable {
     private DataBroker m_dataBroker;
 
     private PacketProcessingService m_packetProcessingService;
+    private ListenerRegistration<DataChangeListener> groupListenerRegistration;
+    private ListenerRegistration<DataChangeListener> flowListenerRegistration;
+    private ConcurrentMap<FlowInfoKey, Runnable> flowMap = new ConcurrentHashMap<FlowInfoKey, Runnable>();
+    private ConcurrentMap<GroupInfoKey, Runnable> groupMap = new ConcurrentHashMap<GroupInfoKey, Runnable> ();
+    private ExecutorService executorService = Executors.newSingleThreadExecutor();
 
     /**
      * Writes the flows and Groups to the MD SAL DataStore
@@ -70,14 +85,39 @@ public class MDSALManager implements AutoCloseable {
     public MDSALManager(final DataBroker db, PacketProcessingService pktProcService) {
         m_dataBroker = db;
         m_packetProcessingService = pktProcService;
+        registerListener(db);
         s_logger.info( "MDSAL Manager Initialized ") ;
     }
 
     @Override
     public void close() throws Exception {
+        groupListenerRegistration.close();
+        flowListenerRegistration.close();
         s_logger.info("MDSAL Manager Closed");
     }
 
+    private void registerListener(DataBroker db) {
+        try {
+            flowListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, getWildCardFlowPath(),
+                                                                        new FlowListener(),
+                                                                        DataChangeScope.SUBTREE);
+            groupListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, getWildCardGroupPath(),
+                                                                        new GroupListener(),
+                                                                        DataChangeScope.SUBTREE);
+        } catch (final Exception e) {
+            s_logger.error("GroupEventHandler: DataChange listener registration fail!", e);
+            throw new IllegalStateException("GroupEventHandler: registration Listener failed.", e);
+        }
+    }
+
+    private InstanceIdentifier<Group> getWildCardGroupPath() {
+        return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class).child(Group.class);
+    }
+
+    private InstanceIdentifier<Flow> getWildCardFlowPath() {
+        return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
+    }
+
     public void installFlow(FlowEntity flowEntity) {
 
         try {
@@ -124,10 +164,21 @@ public class MDSALManager implements AutoCloseable {
                 }
             });
         } catch (Exception e) {
-            s_logger.error("Could not install flow: {}, exception: {}", flowEntity, e);
+            s_logger.error("Could not install flow: {}", flowEntity, e);
         }
     }
 
+    public CheckedFuture<Void,TransactionCommitFailedException> installFlow(BigInteger dpId, Flow flow) {
+        FlowKey flowKey = new FlowKey( new FlowId(flow.getId()) );
+        Node nodeDpn = buildDpnNode(dpId);
+        InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
+                .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
+                .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class,flowKey).build();
+        WriteTransaction modification = m_dataBroker.newWriteOnlyTransaction();
+        modification.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
+        return modification.submit();
+    }
+
     public void installGroup(GroupEntity groupEntity) {
         try {
             Group group = groupEntity.getGroupBuilder().build();
@@ -165,13 +216,14 @@ public class MDSALManager implements AutoCloseable {
                 }
              });
            } catch (Exception e) {
-            s_logger.error("Could not install Group: {}, exception: {}", groupEntity, e);
+            s_logger.error("Could not install Group: {}", groupEntity, e);
             throw e;
         }
     }
 
     public void removeFlow(FlowEntity flowEntity) {
         try {
+            s_logger.debug("Remove flow {}",flowEntity);
             Node nodeDpn = buildDpnNode(flowEntity.getDpnId());
             FlowKey flowKey = new FlowKey(new FlowId(flowEntity.getFlowId()));
             InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
@@ -180,7 +232,7 @@ public class MDSALManager implements AutoCloseable {
 
 
                 WriteTransaction modification = m_dataBroker.newWriteOnlyTransaction();
-                modification.delete(LogicalDatastoreType.CONFIGURATION,flowInstanceId );
+                modification.delete(LogicalDatastoreType.CONFIGURATION,flowInstanceId);
 
                 CheckedFuture<Void,TransactionCommitFailedException> submitFuture  = modification.submit();
 
@@ -205,10 +257,23 @@ public class MDSALManager implements AutoCloseable {
 
                 });
         } catch (Exception e) {
-            s_logger.error("Could not remove Flow: {}, exception: {}", flowEntity, e);
+            s_logger.error("Could not remove Flow: {}", flowEntity, e);
         }
     }
 
+    public CheckedFuture<Void,TransactionCommitFailedException> removeFlowNew(BigInteger dpnId, Flow flowEntity) {
+        s_logger.debug("Remove flow {}",flowEntity);
+        Node nodeDpn = buildDpnNode(dpnId);
+               //FlowKey flowKey = new FlowKey(new FlowId(flowEntity.getId()));
+        FlowKey flowKey = new FlowKey(flowEntity.getId());
+        InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
+                    .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
+                    .child(Table.class, new TableKey(flowEntity.getTableId())).child(Flow.class, flowKey).build();
+        WriteTransaction  modification = m_dataBroker.newWriteOnlyTransaction();
+        modification.delete(LogicalDatastoreType.CONFIGURATION,flowInstanceId );
+        return modification.submit();
+    }
+
     public void removeGroup(GroupEntity groupEntity) {
         try {
             Node nodeDpn = buildDpnNode(groupEntity.getDpnId());
@@ -242,7 +307,7 @@ public class MDSALManager implements AutoCloseable {
                 }
             });
         } catch (Exception e) {
-            s_logger.error("Could not remove Group: {}, exception: {}", groupEntity, e);
+            s_logger.error("Could not remove Group: {}", groupEntity, e);
         }
     }
 
@@ -251,23 +316,23 @@ public class MDSALManager implements AutoCloseable {
         installGroup(groupEntity);
     }
 
-    public void sendPacketOut(long lDpnId, int groupId, byte[] payload) {
+    public void sendPacketOut(BigInteger dpnId, int groupId, byte[] payload) {
 
         List<ActionInfo> actionInfos = new ArrayList<ActionInfo>();
         actionInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId) }));
 
-        sendPacketOutWithActions(lDpnId, groupId, payload, actionInfos);
+        sendPacketOutWithActions(dpnId, groupId, payload, actionInfos);
     }
 
-    public void sendPacketOutWithActions(long lDpnId, long groupId, byte[] payload, List<ActionInfo> actionInfos) {
+    public void sendPacketOutWithActions(BigInteger dpnId, long groupId, byte[] payload, List<ActionInfo> actionInfos) {
 
-        m_packetProcessingService.transmitPacket(MDSALUtil.getPacketOut(actionInfos, payload, lDpnId,
-                getNodeConnRef("openflow:" + lDpnId, "0xfffffffd")));
+        m_packetProcessingService.transmitPacket(MDSALUtil.getPacketOut(actionInfos, payload, dpnId,
+                getNodeConnRef("openflow:" + dpnId, "0xfffffffd")));
     }
 
-    public void sendARPPacketOutWithActions(long lDpnId, byte[] payload, List<ActionInfo> actions) {
-        m_packetProcessingService.transmitPacket(MDSALUtil.getPacketOut(actions, payload, lDpnId,
-                getNodeConnRef("openflow:" + lDpnId, "0xfffffffd")));
+    public void sendARPPacketOutWithActions(BigInteger dpnId, byte[] payload, List<ActionInfo> actions) {
+        m_packetProcessingService.transmitPacket(MDSALUtil.getPacketOut(actions, payload, dpnId,
+                getNodeConnRef("openflow:" + dpnId, "0xfffffffd")));
     }
 
     public InstanceIdentifier<Node> nodeToInstanceId(Node node) {
@@ -293,11 +358,168 @@ public class MDSALManager implements AutoCloseable {
         return _nodeConnectorRef;
     }
 
-    private Node buildDpnNode(long lDpnId) {
-        NodeId nodeId = new NodeId("openflow:" + lDpnId);
+    private Node buildDpnNode(BigInteger dpnId) {
+        NodeId nodeId = new NodeId("openflow:" + dpnId);
         Node nodeDpn = new NodeBuilder().setId(nodeId).setKey(new NodeKey(nodeId)).build();
 
         return nodeDpn;
     }
 
+    public void syncSetUpFlow(FlowEntity flowEntity, long delay, boolean isRemove) {
+        s_logger.trace("syncSetUpFlow for flowEntity {} ", flowEntity);
+        if (flowEntity.getCookie() == null) {
+            flowEntity.setCookie(new BigInteger("0110000", 16));
+        }
+        Flow flow = flowEntity.getFlowBuilder().build();
+        String flowId = flowEntity.getFlowId();
+        BigInteger dpId = flowEntity.getDpnId();
+        short tableId = flowEntity.getTableId();
+        Match matches = flow.getMatch();
+        FlowKey flowKey = new FlowKey( new FlowId(flowId));
+        Node nodeDpn = buildDpnNode(dpId);
+        InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
+                .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
+                .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flowKey).build();
+        Runnable notifyTask = new NotifyTask();
+        FlowInfoKey flowInfoKey = new FlowInfoKey(dpId, tableId, matches, flowId);
+        synchronized (flowInfoKey.toString().intern()) {
+            flowMap.put(flowInfoKey, notifyTask);
+            if (isRemove) {
+                MDSALUtil.syncDelete(m_dataBroker, LogicalDatastoreType.CONFIGURATION, flowInstanceId);
+            } else {
+                MDSALUtil.syncWrite(m_dataBroker, LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow);
+            }
+            synchronized (notifyTask) {
+                try {
+                    notifyTask.wait(delay);
+                } catch (InterruptedException e){}
+            }
+        }
+    }
+
+    public void syncSetUpGroup(GroupEntity groupEntity, long delayTime, boolean isRemove) {
+        s_logger.trace("syncSetUpGroup for groupEntity {} ", groupEntity);
+        Group group = groupEntity.getGroupBuilder().build();
+        BigInteger dpId = groupEntity.getDpnId();
+        Node nodeDpn = buildDpnNode(dpId);
+        long groupId = groupEntity.getGroupId();
+        GroupKey groupKey = new GroupKey(new GroupId(groupId));
+        InstanceIdentifier<Group> groupInstanceId = InstanceIdentifier.builder(Nodes.class)
+                .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
+                .child(Group.class, groupKey).build();
+        Runnable notifyTask = new NotifyTask();
+        GroupInfoKey groupInfoKey = new GroupInfoKey(dpId, groupId);
+        synchronized (groupInfoKey.toString().intern()) {
+            s_logger.trace("syncsetupGroupKey groupKey {}", groupInfoKey);
+            groupMap.put(groupInfoKey, notifyTask);
+            if (isRemove) {
+                MDSALUtil.syncDelete(m_dataBroker, LogicalDatastoreType.CONFIGURATION, groupInstanceId);
+            } else {
+                MDSALUtil.syncWrite(m_dataBroker, LogicalDatastoreType.CONFIGURATION, groupInstanceId, group);
+            }
+            synchronized (notifyTask) {
+                try {
+                    notifyTask.wait(delayTime);
+                } catch (InterruptedException e){}
+            }
+        }
+    }
+
+    public void syncSetUpGroup(BigInteger dpId, Group group, long delayTime, boolean isRemove) {
+        s_logger.trace("syncSetUpGroup for group {} ", group);
+        Node nodeDpn = buildDpnNode(dpId);
+        long groupId = group.getGroupId().getValue();
+        GroupKey groupKey = new GroupKey(new GroupId(groupId));
+        InstanceIdentifier<Group> groupInstanceId = InstanceIdentifier.builder(Nodes.class)
+                .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
+                .child(Group.class, groupKey).build();
+        Runnable notifyTask = new NotifyTask();
+        GroupInfoKey groupInfoKey = new GroupInfoKey(dpId, groupId);
+        synchronized (groupInfoKey.toString().intern()) {
+            s_logger.trace("syncsetupGroupKey groupKey {}", groupInfoKey);
+            groupMap.put(groupInfoKey, notifyTask);
+            if (isRemove) {
+                MDSALUtil.syncDelete(m_dataBroker, LogicalDatastoreType.CONFIGURATION, groupInstanceId);
+            } else {
+                MDSALUtil.syncWrite(m_dataBroker, LogicalDatastoreType.CONFIGURATION, groupInstanceId, group);
+            }
+            synchronized (notifyTask) {
+                try {
+                    notifyTask.wait(delayTime);
+                } catch (InterruptedException e){}
+            }
+        }
+    }
+
+    class GroupListener extends AbstractDataChangeListener<Group> {
+
+        public GroupListener() {
+            super(Group.class);
+        }
+
+        @Override
+        protected void remove(InstanceIdentifier<Group> identifier, Group del) {
+            BigInteger dpId = getDpnFromString(identifier.firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+            executeNotifyTaskIfRequired(dpId, del);
+        }
+
+        private void executeNotifyTaskIfRequired(BigInteger dpId, Group group) {
+            GroupInfoKey groupKey = new GroupInfoKey(dpId, group.getGroupId().getValue());
+            Runnable notifyTask = groupMap.remove(groupKey);
+            if (notifyTask == null) {
+                return;
+            }
+            executorService.execute(notifyTask);
+        }
+
+        @Override
+        protected void update(InstanceIdentifier<Group> identifier, Group original, Group update) {
+            BigInteger dpId = getDpnFromString(identifier.firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+            executeNotifyTaskIfRequired(dpId, update);
+        }
+
+        @Override
+        protected void add(InstanceIdentifier<Group> identifier, Group add) {
+            BigInteger dpId = getDpnFromString(identifier.firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+            executeNotifyTaskIfRequired(dpId, add);
+        }
+    }
+    
+    class FlowListener extends AbstractDataChangeListener<Flow> {
+
+        public FlowListener() {
+            super(Flow.class);
+        }
+
+        @Override
+        protected void remove(InstanceIdentifier<Flow> identifier, Flow del) {
+            BigInteger dpId = getDpnFromString(identifier.firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+            notifyTaskIfRequired(dpId, del);
+        }
+
+        private void notifyTaskIfRequired(BigInteger dpId, Flow flow) {
+            FlowInfoKey flowKey = new FlowInfoKey(dpId, flow.getTableId(), flow.getMatch(), flow.getId().getValue());
+            Runnable notifyTask = flowMap.remove(flowKey);
+            if (notifyTask == null) {
+                return;
+            }
+            executorService.execute(notifyTask);
+        }
+
+        @Override
+        protected void update(InstanceIdentifier<Flow> identifier, Flow original, Flow update) {
+        }
+
+        @Override
+        protected void add(InstanceIdentifier<Flow> identifier, Flow add) {
+            BigInteger dpId = getDpnFromString(identifier.firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+            notifyTaskIfRequired(dpId, add);
+        }
+    }
+    
+    private BigInteger getDpnFromString(String dpnString) {
+        String[] split = dpnString.split(":");
+        return new BigInteger(split[1]);
+    }
+
 }