Handle correctly peer group name 66/73566/1
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Thu, 28 Jun 2018 16:19:59 +0000 (18:19 +0200)
committerClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Thu, 28 Jun 2018 16:37:15 +0000 (18:37 +0200)
when removing peer

JIRA: BGPCEP-804
Change-Id: I11b1e6d222900be5f2986274ad7c0f92c4ba2c35
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/BGPClusterSingletonService.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/OpenConfigMappingUtil.java

index 2bffcbf57a7dc5f38c738b98f9cf4c4986481b44..fcf3dbaebce0f89827c9a08b58d62bde26d13364 100644 (file)
@@ -8,6 +8,8 @@
 
 package org.opendaylight.protocol.bgp.rib.impl.config;
 
+import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.APPLICATION_PEER_GROUP_NAME;
+import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.APPLICATION_PEER_GROUP_NAME_OPT;
 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getNeighborInstanceIdentifier;
 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getNeighborInstanceName;
 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getRibInstanceName;
@@ -23,6 +25,7 @@ import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.stream.Collectors;
@@ -251,7 +254,6 @@ public final class BGPClusterSingletonService implements ClusterSingletonService
         }
     }
 
-
     private synchronized void onNeighborCreated(final Neighbor neighbor) {
         LOG.debug("Creating Peer instance with configuration: {}", neighbor);
         final PeerBean bgpPeer;
@@ -265,24 +267,24 @@ public final class BGPClusterSingletonService implements ClusterSingletonService
         initiatePeerInstance(neighborInstanceIdentifier, neighbor, bgpPeer);
         this.peers.put(neighborInstanceIdentifier, bgpPeer);
 
-        final String groupName= getPeerGroupName(neighbor.getConfig());
-        String peerGroupName = null;
-        if (groupName != null) {
-            peerGroupName = StringUtils.substringBetween(groupName, "=\"", "\"");
-        }
-
-        if (peerGroupName != null) {
-            this.peersGroups.computeIfAbsent(peerGroupName, k -> new ArrayList<>()).add(bgpPeer);
-        }
+        final Optional<String> peerGroupName= getPeerGroupName(neighbor.getConfig());
+        peerGroupName.ifPresent(s -> this.peersGroups.computeIfAbsent(s, k -> new ArrayList<>()).add(bgpPeer));
         LOG.debug("Peer instance created {}", bgpPeer);
     }
 
-    private static String getPeerGroupName(final Config config) {
+    private static Optional<String> getPeerGroupName(final Config config) {
         if (config == null) {
-            return null;
+            return Optional.empty();
         }
         final NeighborPeerGroupConfig aug = config.augmentation(NeighborPeerGroupConfig.class);
-        return aug == null ? null : aug.getPeerGroup();
+        if(aug == null || aug.getPeerGroup() == null) {
+            return Optional.empty();
+        }
+        final String peerGroupName =  aug.getPeerGroup();
+        if (peerGroupName.equals(APPLICATION_PEER_GROUP_NAME)) {
+            return APPLICATION_PEER_GROUP_NAME_OPT;
+        }
+        return Optional.of(StringUtils.substringBetween(peerGroupName, "=\"", "\""));
     }
 
     private synchronized void onNeighborUpdated(final PeerBean bgpPeer, final Neighbor neighbor) {
@@ -311,13 +313,11 @@ public final class BGPClusterSingletonService implements ClusterSingletonService
         LOG.debug("Removing Peer instance: {}", neighbor);
         final PeerBean bgpPeer = this.peers.remove(getNeighborInstanceIdentifier(this.bgpIid, neighbor.key()));
 
-        final String groupName = getPeerGroupName(neighbor.getConfig());
-        if (groupName != null) {
-            this.peersGroups.computeIfPresent(groupName, (k, peers) -> {
-                peers.remove(bgpPeer);
-                return peers.isEmpty() ? null : peers;
-            });
-        }
+        final Optional<String> groupName = getPeerGroupName(neighbor.getConfig());
+        groupName.ifPresent(s -> this.peersGroups.computeIfPresent(s, (k, peers) -> {
+            peers.remove(bgpPeer);
+            return peers.isEmpty() ? null : peers;
+        }));
         closePeer(bgpPeer);
     }
 
@@ -353,7 +353,7 @@ public final class BGPClusterSingletonService implements ClusterSingletonService
         }
     }
 
-    public synchronized void restartNeighbors(final String peerGroupName) {
+    synchronized void restartNeighbors(final String peerGroupName) {
         final List<PeerBean> peerGroup = this.peersGroups.get(peerGroupName);
         if (peerGroup == null) {
             return;
index a5e38d50fc4f4a7f6fcdf90366a17f7a5570aefe..c606be9cd0556c1563a91e5ac4cdf373d16e9b8b 100644 (file)
@@ -67,7 +67,8 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
 public final class OpenConfigMappingUtil {
 
-    static final String APPLICATION_PEER_GROUP_NAME = "application-peers";
+    public static final String APPLICATION_PEER_GROUP_NAME = "application-peers";
+    public static final Optional<String> APPLICATION_PEER_GROUP_NAME_OPT = Optional.of(APPLICATION_PEER_GROUP_NAME);
     static final int HOLDTIMER = 90;
     private static final AfiSafi IPV4_AFISAFI = new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class).build();
     private static final List<AfiSafi> DEFAULT_AFISAFI = ImmutableList.of(IPV4_AFISAFI);