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.HashMap;
13 import java.util.function.BiConsumer;
14 import javax.annotation.concurrent.GuardedBy;
15 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerExportGroupRegistry;
16 import org.opendaylight.protocol.concepts.AbstractRegistration;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 final class PeerExportGroupImpl implements PeerExportGroupRegistry {
24 private final Map<PeerId, PeerExporTuple> peers = new HashMap<>();
25 private final AbstractExportPolicy policy;
27 public PeerExportGroupImpl(final AbstractExportPolicy policy) {
28 this.policy = Preconditions.checkNotNull(policy);
32 public ContainerNode effectiveAttributes(final PeerRole role, final ContainerNode attributes) {
33 return attributes == null || role == null ? null : this.policy.effectiveAttributes(role, attributes);
37 public boolean containsPeer(final PeerId routePeerId) {
38 synchronized (this.peers) {
39 return this.peers.containsKey(routePeerId);
44 public void forEach(final BiConsumer<PeerId, YangInstanceIdentifier> action) {
45 synchronized (this.peers) {
46 for (final Map.Entry<PeerId, PeerExporTuple> pid : this.peers.entrySet()) {
47 action.accept(pid.getKey(), pid.getValue().getYii());
53 public AbstractRegistration registerPeer(final PeerId peerId, final PeerExporTuple peerExporTuple) {
54 synchronized (this.peers) {
55 this.peers.put(peerId, peerExporTuple);
58 return new AbstractRegistration() {
60 protected void removeRegistration() {
61 synchronized (PeerExportGroupImpl.this.peers) {
62 PeerExportGroupImpl.this.peers.remove(peerId);
69 public boolean isEmpty() {
70 synchronized (this.peers) {
71 return this.peers.isEmpty();