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.EnumMap;
12 import java.util.HashMap;
13 import java.util.HashSet;
15 import java.util.Optional;
17 import javax.annotation.concurrent.GuardedBy;
18 import javax.annotation.concurrent.ThreadSafe;
19 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerExportGroupRegistry;
20 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
21 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup;
22 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup.PeerExporTuple;
23 import org.opendaylight.protocol.concepts.AbstractRegistration;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 final class ExportPolicyPeerTrackerImpl implements ExportPolicyPeerTracker {
35 private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTrackerImpl.class);
37 private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
39 private final Map<PeerId, SendReceive> peerAddPathTables = new HashMap<>();
41 private final Set<PeerId> peerTables = new HashSet<>();
42 private final PolicyDatabase policyDatabase;
43 private final TablesKey localTableKey;
45 private final Map<PeerRole, PeerExportGroupRegistry> groups = new EnumMap<>(PeerRole.class);
47 ExportPolicyPeerTrackerImpl(final PolicyDatabase policyDatabase, final TablesKey localTablesKey) {
48 this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
49 this.localTableKey = localTablesKey;
52 private synchronized AbstractRegistration addToExportGroups(final PeerId peerId,
53 final YangInstanceIdentifier peerPath, final PeerRole peerRole) {
54 final PeerExportGroupRegistry peerExp = this.groups.computeIfAbsent(peerRole,
55 k -> new PeerExportGroupImpl(this.policyDatabase.exportPolicyForRole(peerRole)));
57 final AbstractRegistration registration = peerExp.registerPeer(peerId, new PeerExporTuple(peerPath, peerRole));
59 return new AbstractRegistration() {
61 protected void removeRegistration() {
63 if(ExportPolicyPeerTrackerImpl.this.groups.get(peerRole).isEmpty()) {
64 ExportPolicyPeerTrackerImpl.this.groups.remove(peerRole);
71 public synchronized AbstractRegistration registerPeer(final PeerId peerId, final SendReceive sendReceive,
72 final YangInstanceIdentifier peerPath, final PeerRole peerRole,
73 final Optional<SimpleRoutingPolicy> optSimpleRoutingPolicy) {
74 if (sendReceive != null) {
75 this.peerAddPathTables.put(peerId, sendReceive);
76 LOG.debug("Supported Add BestPath table {} added to peer {}", sendReceive, peerId);
78 final SimpleRoutingPolicy simpleRoutingPolicy = optSimpleRoutingPolicy.orElse(null);
79 if (SimpleRoutingPolicy.AnnounceNone != simpleRoutingPolicy) {
80 this.peerTables.add(peerId);
82 this.peerRoles.put(peerPath, peerRole);
83 LOG.debug("Supported table {} added to peer {} role {}", this.localTableKey, peerId, peerRole);
84 final AbstractRegistration registration = addToExportGroups(peerId, peerPath, peerRole);
86 final Object lock = this;
87 return new AbstractRegistration() {
89 protected void removeRegistration() {
91 final SendReceive sendReceiveValue = ExportPolicyPeerTrackerImpl.this.peerAddPathTables.remove(peerId);
92 if (sendReceiveValue != null) {
93 LOG.debug("Supported Add BestPath table {} removed to peer {}", sendReceiveValue, peerId);
95 ExportPolicyPeerTrackerImpl.this.peerTables.remove(peerId);
96 LOG.debug("Removed peer {} from supported table {}", peerId, ExportPolicyPeerTrackerImpl.this.localTableKey);
97 ExportPolicyPeerTrackerImpl.this.peerRoles.remove(peerPath);
105 public synchronized PeerExportGroup getPeerGroup(final PeerRole role) {
106 return this.groups.get(Preconditions.checkNotNull(role));
110 public synchronized boolean isTableSupported(final PeerId peerId) {
111 return this.peerTables.contains(peerId);
115 public synchronized PeerRole getRole(final YangInstanceIdentifier peerId) {
116 return this.peerRoles.get(peerId);
120 public synchronized boolean isAddPathSupportedByPeer(final PeerId peerId) {
121 final SendReceive sendReceive = this.peerAddPathTables.get(peerId);
122 return sendReceive != null && (sendReceive.equals(SendReceive.Both) || sendReceive.equals(SendReceive.Receive));