BGPCEP-754: Fix NPE and rework
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPPeerTrackerImpl.java
index 642d27fbdee0ee2a80a94d30bac579dee59afd9c..aea5facf73141ca76e9a086a0c0956681a3abb32 100644 (file)
@@ -7,82 +7,42 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
-import java.util.ArrayList;
+import com.google.common.collect.ImmutableList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
 import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker;
 import org.opendaylight.protocol.bgp.rib.spi.Peer;
 import org.opendaylight.protocol.concepts.AbstractRegistration;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
 public final class BGPPeerTrackerImpl implements BGPPeerTracker {
     @GuardedBy("this")
-    private final List<Peer> peers = new ArrayList<>();
-    private Map<PeerRole, List<PeerId>> rolePerPeerId;
+    private Map<PeerId, Peer> peers = new HashMap<>();
+    private ImmutableList<Peer> peersList;
 
     @Override
     public synchronized AbstractRegistration registerPeer(final Peer peer) {
-        this.peers.add(peer);
-        this.rolePerPeerId = this.peers.stream().collect(Collectors.groupingBy(Peer::getRole,
-                Collectors.mapping(Peer::getPeerId, Collectors.toList())));
+        this.peers.put(peer.getPeerId(), peer);
+        this.peersList = ImmutableList.copyOf(this.peers.values());
         return new AbstractRegistration() {
             @Override
             protected void removeRegistration() {
                 synchronized (BGPPeerTrackerImpl.this) {
-                    BGPPeerTrackerImpl.this.peers.remove(peer);
-                    BGPPeerTrackerImpl.this.rolePerPeerId = BGPPeerTrackerImpl.this.peers
-                            .stream().collect(Collectors.groupingBy(Peer::getRole,
-                                    Collectors.mapping(Peer::getPeerId, Collectors.toList())));
+                    BGPPeerTrackerImpl.this.peers.remove(peer.getPeerId());
                 }
             }
         };
     }
 
     @Override
-    public Peer getPeer(final PeerId peerId) {
-        synchronized (this.peers) {
-            return this.peers.stream().filter(peer -> peer.getPeerId().equals(peerId)).findFirst().orElse(null);
-        }
+    public synchronized Peer getPeer(final PeerId peerId) {
+        return this.peers.get(peerId);
     }
 
     @Override
-    public boolean supportsTable(final PeerId peerIdOfNewPeer, final TablesKey tableKey) {
-        final Peer peer = getPeer(peerIdOfNewPeer);
-        return peer != null && peer.supportsTable(tableKey);
-    }
-
-    @Override
-    public synchronized Map<PeerRole, List<PeerId>> getRoles() {
-        return this.rolePerPeerId;
-    }
-
-    @Override
-    public boolean supportsAddPathSupported(final PeerId toPeer, final TablesKey localTK) {
-        final Peer peer = getPeer(toPeer);
-        return peer != null && peer.supportsAddPathSupported(localTK);
-    }
-
-    @Override
-    public PeerRole getRole(final PeerId peerId) {
-        synchronized (this.peers) {
-            final Optional<Peer> peerOptional = this.peers.stream()
-                    .filter(peer -> peer.getPeerId().equals(peerId)).findFirst();
-            if (peerOptional.isPresent()) {
-                return peerOptional.get().getRole();
-            }
-        }
-        return null;
-    }
-
-    @Override
-    public YangInstanceIdentifier getPeerRibInstanceIdentifier(final PeerId peerId) {
-        final Peer peer = getPeer(peerId);
-        return peer == null ? null : peer.getPeerRibInstanceIdentifier();
+    public synchronized List<Peer> getPeers() {
+        return this.peersList;
     }
 }
\ No newline at end of file