2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.HashMap;
14 import java.util.function.BiConsumer;
15 import javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerExportGroupRegistry;
17 import org.opendaylight.protocol.concepts.AbstractRegistration;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 final class PeerExportGroupImpl implements PeerExportGroupRegistry {
25 private final Map<PeerId, PeerExporTuple> peers = new HashMap<>();
26 private final AbstractExportPolicy policy;
28 public PeerExportGroupImpl(final AbstractExportPolicy policy) {
29 this.policy = Preconditions.checkNotNull(policy);
33 public ContainerNode effectiveAttributes(final PeerRole role, final ContainerNode attributes) {
34 return attributes == null || role == null ? null : this.policy.effectiveAttributes(role, attributes);
38 public Collection<Map.Entry<PeerId, PeerExporTuple>> getPeers() {
39 synchronized (this.peers) {
40 return this.peers.entrySet();
45 public boolean containsPeer(final PeerId routePeerId) {
46 synchronized (this.peers) {
47 return this.peers.containsKey(routePeerId);
52 public void forEach(final BiConsumer<PeerId, YangInstanceIdentifier> action) {
53 synchronized (this.peers) {
54 for (final Map.Entry<PeerId, PeerExporTuple> pid : this.peers.entrySet()) {
55 action.accept(pid.getKey(), pid.getValue().getYii());
61 public AbstractRegistration registerPeer(final PeerId peerId, final PeerExporTuple peerExporTuple) {
62 synchronized (this.peers) {
63 this.peers.put(peerId, peerExporTuple);
66 return new AbstractRegistration() {
68 protected void removeRegistration() {
69 synchronized (PeerExportGroupImpl.this.peers) {
70 PeerExportGroupImpl.this.peers.remove(peerId);
77 public boolean isEmpty() {
78 synchronized (this.peers) {
79 return this.peers.isEmpty();