When removing policies and tunnels, check if they exist or not.

Change-Id: I1336a0a2ed9b14efe754e19c7e389152cd872219
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 1b8a450..aa7c6dc 100644
--- a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
+++ b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
@@ -1023,6 +1023,39 @@
     }
 
     /**
+     * Return router DPIDs for the tunnel
+     *
+     * @param tid tunnel ID
+     * @return List of DPID
+     */
+    public List<Dpid> getTunnelInfo(String tid) {
+        TunnelInfo tunnelInfo =  tunnelTable.get(tid);
+        return tunnelInfo.dpids;
+
+    }
+
+    /**
+     * Get the first group ID for the tunnel for specific source router
+     * If Segment Stitching was required to create the tunnel, there are
+     * mutiple source routers.
+     *
+     * @param tunnelId ID for the tunnel
+     * @param dpid source router DPID
+     * @return the first group ID of the tunnel
+     */
+    public int getTunnelGroupId(String tunnelId, String dpid) {
+        IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
+                getSwId(dpid));
+
+        if (sw13 == null) {
+            return -1;
+        }
+        else {
+            return sw13.getTunnelGroupId(tunnelId);
+        }
+    }
+
+    /**
      * Create a tunnel for policy routing
      * It delivers the node IDs of tunnels to driver.
      * Split the node IDs if number of IDs exceeds the limit for stitching.
@@ -1076,18 +1109,6 @@
     }
 
     /**
-     * Return router DPIDs for the tunnel
-     *
-     * @param tid tunnel ID
-     * @return List of DPID
-     */
-    public List<Dpid> getTunnelInfo(String tid) {
-        TunnelInfo tunnelInfo =  tunnelTable.get(tid);
-        return tunnelInfo.dpids;
-
-    }
-
-    /**
      * Set policy table for policy routing
      *
      * @param sw
@@ -1268,6 +1289,8 @@
      */
     public boolean removePolicy(String pid) {
         PolicyInfo policyInfo =  policyTable.get(pid);
+        if (policyInfo == null)
+            return false;
         PacketMatch policyMatch = policyInfo.match;
         String tid = policyInfo.tunnelId;
         int priority = policyInfo.priority;
@@ -1286,13 +1309,18 @@
                 new MatchActionOperationEntry(Operator.REMOVE, matchAction);
 
         TunnelInfo tunnelInfo = tunnelTable.get(tid);
+        if (tunnelInfo == null)
+            return false;
         List<TunnelRouteInfo> routes = tunnelInfo.routes;
 
         for (TunnelRouteInfo route : routes) {
             IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
                     getSwId(route.srcSwDpid));
 
-            if (sw13 != null) {
+            if (sw13 == null) {
+                return false;
+            }
+            else {
                 printMatchActionOperationEntry(sw13, maEntry);
                 try {
                     sw13.pushFlow(maEntry);
@@ -1310,27 +1338,6 @@
     }
 
     /**
-     * Get the first group ID for the tunnel for specific source router
-     * If Segment Stitching was required to create the tunnel, there are
-     * mutiple source routers.
-     *
-     * @param tunnelId ID for the tunnel
-     * @param dpid source router DPID
-     * @return the first group ID of the tunnel
-     */
-    public int getTunnelGroupId(String tunnelId, String dpid) {
-        IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
-                getSwId(dpid));
-
-        if (sw13 == null) {
-            return -1;
-        }
-        else {
-            return sw13.getTunnelGroupId(tunnelId);
-        }
-    }
-
-    /**
      * Remove a tunnel
      * It removes all groups for the tunnel if the tunnel is not used for any
      * policy.
@@ -1349,13 +1356,18 @@
         }
 
         TunnelInfo tunnelInfo = tunnelTable.get(tunnelId);
+        if (tunnelInfo == null)
+            return false;
 
         List<TunnelRouteInfo> routes = tunnelInfo.routes;
         for (TunnelRouteInfo route: routes) {
             IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
                     getSwId(route.srcSwDpid));
 
-            if (sw13 != null) {
+            if (sw13 == null) {
+                return false;
+            }
+            else {
                 sw13.removeTunnel(tunnelId);
             }
         }