Several fixes for Segment Routing

1. In getSubnetNextObjectiveId, do not create a broadcast group if it does not exist
       The creation is done when device is up and when config is added
2. Check if broadcast group exists before creating one
3. Put subnet in a Set instead of a List to avoid duplicated elements

Change-Id: Ifafd8e783a9d98aa62b5e299f560897458b90bb0
diff --git a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/grouphandler/DefaultGroupHandler.java b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/grouphandler/DefaultGroupHandler.java
index 2606f48..69a0d86 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/grouphandler/DefaultGroupHandler.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/grouphandler/DefaultGroupHandler.java
@@ -334,8 +334,8 @@
     }
 
     /**
-     * Returns the next objective associated with the neighborset.
-     * If there is no next objective for this neighborset, this API
+     * Returns the next objective associated with the subnet.
+     * If there is no next objective for this subnet, this API
      * would create a next objective and return.
      *
      * @param prefix subnet information
@@ -344,31 +344,8 @@
     public int getSubnetNextObjectiveId(IpPrefix prefix) {
         Integer nextId = subnetNextObjStore.
                 get(new SubnetNextObjectiveStoreKey(deviceId, prefix));
-        if (nextId == null) {
-            log.trace("getSubnetNextObjectiveId in device{}: Next objective id "
-                              + "not found for {} and creating", deviceId, prefix);
-            log.trace("getSubnetNextObjectiveId: subnetNextObjStore contents for device {}: {}",
-                      deviceId,
-                      subnetNextObjStore.entrySet()
-                              .stream()
-                              .filter((subnetStoreEntry) ->
-                                              (subnetStoreEntry.getKey().deviceId().equals(deviceId)))
-                              .collect(Collectors.toList()));
-            createGroupsFromSubnetConfig();
-            nextId = subnetNextObjStore.
-                    get(new SubnetNextObjectiveStoreKey(deviceId, prefix));
-            if (nextId == null) {
-                log.warn("subnetNextObjStore: unable to create next objective");
-                return -1;
-            } else {
-                log.debug("subnetNextObjStore in device{}: Next objective id {} "
-                                  + "created for {}", deviceId, nextId, prefix);
-            }
-        } else {
-            log.trace("subnetNextObjStore in device{}: Next objective id {} "
-                              + "found for {}", deviceId, nextId, prefix);
-        }
-        return nextId;
+
+        return (nextId != null) ? nextId : -1;
     }
 
     /**
@@ -541,6 +518,15 @@
 
         // Construct a broadcast group for each subnet
         subnetPortMap.forEach((subnet, ports) -> {
+            SubnetNextObjectiveStoreKey key =
+                    new SubnetNextObjectiveStoreKey(deviceId, subnet);
+
+            if (subnetNextObjStore.containsKey(key)) {
+                log.debug("Broadcast group for device {} and subnet {} exists",
+                          deviceId, subnet);
+                return;
+            }
+
             int nextId = flowObjectiveService.allocateNextId();
 
             NextObjective.Builder nextObjBuilder = DefaultNextObjective
@@ -558,8 +544,7 @@
             log.debug("createGroupFromSubnetConfig: Submited "
                               + "next objective {} in device {}",
                       nextId, deviceId);
-            SubnetNextObjectiveStoreKey key =
-                    new SubnetNextObjectiveStoreKey(deviceId, subnet);
+
             subnetNextObjStore.put(key, nextId);
         });
     }