/* * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.protocol.bgp.rib.impl; import com.google.common.collect.ImmutableList; import java.util.HashMap; import java.util.List; import java.util.Map; 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; public final class BGPPeerTrackerImpl implements BGPPeerTracker { @GuardedBy("this") private Map peers = new HashMap<>(); private ImmutableList peersList; @Override public synchronized AbstractRegistration registerPeer(final Peer peer) { 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.getPeerId()); } } }; } @Override public synchronized Peer getPeer(final PeerId peerId) { return this.peers.get(peerId); } @Override public synchronized List getPeers() { return this.peersList; } }