Handle policy creation and tunnel creation for multiple instance
diff --git a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
index 104a896..3b7c5c4 100644
--- a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
+++ b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
@@ -225,8 +225,8 @@
         adjacencySidTable = new HashMap<Integer,HashMap<Integer, List<Integer>>>();
         adjcencyGroupIdTable = new HashMap<String, HashMap<Integer, Integer>>();
         switchDpidListWithMastership = new ArrayList<String>();
-        policyEventHandler = new PolicyEventHandler();
-        tunnelEventHandler = new TunnelEventHandler();
+        policyEventHandler = new PolicyEventHandler(this);
+        tunnelEventHandler = new TunnelEventHandler(this);
 
         packetService.registerPacketListener(this);
         topologyService.addListener(this, false);
@@ -295,13 +295,19 @@
     private class PolicyEventHandler implements
     IEventChannelListener<Long, PolicyNotification> {
 
+        SegmentRoutingManager srManager;
+
+        public PolicyEventHandler(SegmentRoutingManager srm) {
+            this.srManager = srm;
+        }
+
         @Override
         public void entryAdded(PolicyNotification policyNotication) {
             log.debug("Policy entry {} was added", policyNotication);
             if (PolicyType.valueOf(policyNotication.getPolicyType()) ==
                     PolicyType.TUNNEL_FLOW) {
                 SegmentRoutingPolicyTunnel srPolicy =
-                        new SegmentRoutingPolicyTunnel(policyNotication);
+                        new SegmentRoutingPolicyTunnel(srManager, policyNotication);
                 policyTable.put(srPolicy.getPolicyId(), srPolicy);
             }
         }
@@ -322,11 +328,23 @@
 
     private class TunnelEventHandler implements
     IEventChannelListener<Long, TunnelNotification> {
+        SegmentRoutingManager srManager;
+
+        public TunnelEventHandler(SegmentRoutingManager srm) {
+            super();
+            this.srManager = srm;
+        }
 
         @Override
         public void entryAdded(TunnelNotification tunnelNotification) {
             SegmentRoutingTunnel srTunnel =
-                    new SegmentRoutingTunnel(tunnelNotification);
+                    new SegmentRoutingTunnel(srManager, tunnelNotification);
+            if (srTunnel.checkAndCreateTunnel()) {
+                TunnelNotification tunnelNotificationBack =
+                        new TunnelNotification(srTunnel);
+                tunnelEventChannel.updateEntry(Long.valueOf(srTunnel.getTunnelId()),
+                        tunnelNotificationBack);
+            }
             tunnelTable.put(srTunnel.getTunnelId(), srTunnel);
         }
 
@@ -337,9 +355,10 @@
         }
 
         @Override
-        public void entryUpdated(TunnelNotification value) {
-            // TODO Auto-generated method stub
-
+        public void entryUpdated(TunnelNotification tunnelNotification) {
+            SegmentRoutingTunnel srTunnel =
+                    new SegmentRoutingTunnel(srManager, tunnelNotification);
+            tunnelTable.put(srTunnel.getTunnelId(), srTunnel);
         }
 
     }
@@ -1649,9 +1668,9 @@
             boolean Bos) {
         IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
                 sw.getDpid().value());
-        if (sw13 == null) {
-            return null;
-        }
+        //if (sw13 == null) {
+        //    return null;
+        //}
         MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel), Bos);
         List<Action> actions = new ArrayList<Action>();
 
diff --git a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingPolicyTunnel.java b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingPolicyTunnel.java
index 73aa145..e6b2649 100644
--- a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingPolicyTunnel.java
+++ b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingPolicyTunnel.java
@@ -1,10 +1,8 @@
 package net.onrc.onos.apps.segmentrouting;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
-import net.floodlightcontroller.core.IOF13Switch;
 import net.onrc.onos.core.matchaction.MatchAction;
 import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
 import net.onrc.onos.core.matchaction.MatchActionOperations.Operator;
@@ -24,8 +22,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.esotericsoftware.minlog.Log;
-
 public class SegmentRoutingPolicyTunnel extends SegmentRoutingPolicy {
 
     private static final Logger log = LoggerFactory
@@ -39,8 +35,9 @@
         this.tunnelId = tid;
     }
 
-    public SegmentRoutingPolicyTunnel(PolicyNotification policyNotication) {
+    public SegmentRoutingPolicyTunnel(SegmentRoutingManager srm, PolicyNotification policyNotication) {
         super(policyNotication);
+        this.srManager = srm;
         this.tunnelId = policyNotication.getTunnelId();
     }
 
@@ -89,11 +86,14 @@
 
             MatchAction matchAction = new MatchAction(
                     srManager.getMatchActionId(),
-                    new SwitchPort((long) 0, (short) 0), match, priority,
+                    new SwitchPort((new Dpid(route.getSrcSwDpid())).value(), (long)0), match, priority,
                     actions);
             MatchActionOperationEntry maEntry =
                     new MatchActionOperationEntry(Operator.ADD, matchAction);
 
+            srManager.executeMatchActionOpEntry(maEntry);
+
+            /*
             IOF13Switch sw13 = srManager.getIOF13Switch(route.getSrcSwDpid());
 
             if (sw13 != null) {
@@ -109,6 +109,7 @@
                 Log.warn("Cannot find the target switch {}", route.getSrcSwDpid());
                 return false;
             }
+            */
         }
 
         return true;
@@ -123,12 +124,14 @@
         groupAction.setGroupId(gropuId);
         actions.add(groupAction);
 
+        /*
         MatchAction matchAction = new MatchAction(
                 srManager.getMatchActionId(),
                 new SwitchPort((long) 0, (short) 0), match, priority,
                 actions);
         MatchActionOperationEntry maEntry =
                 new MatchActionOperationEntry(Operator.REMOVE, matchAction);
+        */
 
         SegmentRoutingTunnel tunnel = srManager.getTunnelInfo(tunnelId);
         if (tunnel == null) {
@@ -139,6 +142,17 @@
         List<TunnelRouteInfo> routes = tunnel.getRoutes();
 
         for (TunnelRouteInfo route : routes) {
+
+            MatchAction matchAction = new MatchAction(
+                    srManager.getMatchActionId(),
+                    new SwitchPort((new Dpid(route.getSrcSwDpid())).value(), (long)0), match, priority,
+                    actions);
+            MatchActionOperationEntry maEntry =
+                    new MatchActionOperationEntry(Operator.REMOVE, matchAction);
+
+            srManager.executeMatchActionOpEntry(maEntry);
+
+            /*
             IOF13Switch sw13 = srManager.getIOF13Switch(route.getSrcSwDpid());
             if (sw13 == null) {
                 log.warn("Cannt find the switch {}", route.getSrcSwDpid());
@@ -154,6 +168,7 @@
                     return false;
                 }
             }
+            */
         }
 
         return true;
diff --git a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingTunnel.java b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingTunnel.java
index 6ad05bc..ad5e0f1 100644
--- a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingTunnel.java
+++ b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingTunnel.java
@@ -41,12 +41,38 @@
         this.routes = new ArrayList<TunnelRouteInfo>();
     }
 
-    public SegmentRoutingTunnel(TunnelNotification tunnelNotification) {
+    public SegmentRoutingTunnel(SegmentRoutingManager srm, TunnelNotification tunnelNotification) {
+        this.srManager = srm;
         this.tunnelId = tunnelNotification.getTunnelId();
         this.labelIds = tunnelNotification.getLabelIds();
         this.routes = tunnelNotification.getRouteInfo();
     }
 
+    public boolean checkAndCreateTunnel() {
+
+        boolean modified = false;
+        for (TunnelRouteInfo route: routes) {
+            if (srManager.getIOF13Switch(route.getSrcSwDpid()) != null) {
+                if (route.getGroupId() == -1) {
+                    NeighborSet ns = new NeighborSet();
+                    for (Dpid dpid: route.getFwdSwDpid())
+                        ns.addDpid(dpid);
+
+                    printTunnelInfo(route.srcSwDpid, tunnelId, route.getRoute(), ns);
+                    int groupId = -1;
+                    if ((groupId =createGroupsForTunnel(tunnelId, route, ns)) < 0) {
+                        log.debug("Failed to create a tunnel at driver.");
+                        return false;
+                    }
+                    route.setGroupId(groupId);
+                    modified = true;
+                }
+            }
+        }
+
+        return modified;
+    }
+
     /**
      * Get tunnel ID
      *
@@ -104,17 +130,23 @@
         checkAndSplitLabels(stitchingRule);
 
         for (TunnelRouteInfo route: stitchingRule) {
-            NeighborSet ns = new NeighborSet();
-            for (Dpid dpid: route.getFwdSwDpid())
-                ns.addDpid(dpid);
 
-            printTunnelInfo(route.srcSwDpid, tunnelId, route.getRoute(), ns);
-            int groupId = -1;
-            if ((groupId =createGroupsForTunnel(tunnelId, route, ns)) < 0) {
-                log.debug("Failed to create a tunnel at driver.");
-                return false;
+            if (srManager.getIOF13Switch(route.getSrcSwDpid()) != null) {
+                NeighborSet ns = new NeighborSet();
+                for (Dpid dpid: route.getFwdSwDpid())
+                    ns.addDpid(dpid);
+
+                printTunnelInfo(route.srcSwDpid, tunnelId, route.getRoute(), ns);
+                int groupId = -1;
+                if ((groupId =createGroupsForTunnel(tunnelId, route, ns)) < 0) {
+                    log.debug("Failed to create a tunnel at driver.");
+                    return false;
+                }
+                route.setGroupId(groupId);
             }
-            route.setGroupId(groupId);
+            else {
+                route.setGroupId(-1);
+            }
         }
 
         this.routes = stitchingRule;
@@ -197,11 +229,7 @@
 
         for (TunnelRouteInfo route: routes) {
             IOF13Switch sw13 = srManager.getIOF13Switch(route.srcSwDpid);
-            if (sw13 == null) {
-                log.warn("Cannot find the switch", route.srcSwDpid);
-                return false;
-            }
-            else {
+            if (sw13 != null) {
                 if (!sw13.removeGroup(route.getGroupId())) {
                     log.warn("Faied to remove the tunnel {} at driver",
                             tunnelId);