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 static java.util.function.Function.identity;
11 import static java.util.stream.Collectors.toMap;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Collections;
16 import java.util.EnumMap;
17 import java.util.HashMap;
18 import java.util.HashSet;
20 import java.util.Optional;
22 import java.util.stream.Collectors;
23 import javax.annotation.concurrent.GuardedBy;
24 import javax.annotation.concurrent.ThreadSafe;
25 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
26 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
27 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup;
28 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup.PeerExporTuple;
29 import org.opendaylight.protocol.concepts.AbstractRegistration;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 final class ExportPolicyPeerTrackerImpl implements ExportPolicyPeerTracker {
41 private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTrackerImpl.class);
43 private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
45 private final Map<PeerId, SendReceive> peerAddPathTables = new HashMap<>();
47 private final Set<PeerId> peerTables = new HashSet<>();
48 private final PolicyDatabase policyDatabase;
49 private final TablesKey localTableKey;
50 private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
52 ExportPolicyPeerTrackerImpl(final PolicyDatabase policyDatabase, final TablesKey localTablesKey) {
53 this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
54 this.localTableKey = localTablesKey;
57 private synchronized void createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
58 if (!peerPathRoles.isEmpty()) {
59 final Map<PeerRole, Map<PeerId, PeerExporTuple>> immutablePeers = peerPathRoles.entrySet().stream()
60 .collect(Collectors.groupingBy(Map.Entry::getValue, toMap(peer -> IdentifierUtils.peerKeyToPeerId(peer
61 .getKey()), peer -> new PeerExporTuple(peer.getKey(), peer.getValue()))));
63 this.groups = peerPathRoles.values().stream().collect(Collectors.toSet()).stream()
64 .collect(toMap(identity(), role -> new PeerExportGroupImpl(ImmutableMap.copyOf(immutablePeers.get(role)),
65 this.policyDatabase.exportPolicyForRole(role)), (oldKey, newKey) -> oldKey, () -> new EnumMap<>(PeerRole.class)));
70 public synchronized AbstractRegistration registerPeer(final PeerId peerId, final SendReceive sendReceive, final YangInstanceIdentifier peerPath,
71 final PeerRole peerRole, final Optional<SimpleRoutingPolicy> optSimpleRoutingPolicy) {
72 if (sendReceive != null) {
73 this.peerAddPathTables.put(peerId, sendReceive);
74 LOG.debug("Supported Add BestPath table {} added to peer {}", sendReceive, peerId);
76 final SimpleRoutingPolicy simpleRoutingPolicy = optSimpleRoutingPolicy.orElse(null);
77 if (SimpleRoutingPolicy.AnnounceNone != simpleRoutingPolicy) {
78 this.peerTables.add(peerId);
80 this.peerRoles.put(peerPath, peerRole);
81 LOG.debug("Supported table {} added to peer {} role {}", this.localTableKey, peerId, peerRole);
82 createGroups(this.peerRoles);
84 final Object lock = this;
85 return new AbstractRegistration() {
87 protected void removeRegistration() {
89 final SendReceive sendReceiveValue = ExportPolicyPeerTrackerImpl.this.peerAddPathTables.remove(peerId);
90 if (sendReceiveValue != null) {
91 LOG.debug("Supported Add BestPath table {} removed to peer {}", sendReceiveValue, peerId);
93 ExportPolicyPeerTrackerImpl.this.peerTables.remove(peerId);
94 LOG.debug("Removed peer {} from supported table {}", peerId, ExportPolicyPeerTrackerImpl.this.localTableKey);
95 ExportPolicyPeerTrackerImpl.this.peerRoles.remove(peerPath);
96 createGroups(ExportPolicyPeerTrackerImpl.this.peerRoles);
103 public synchronized PeerExportGroup getPeerGroup(final PeerRole role) {
104 return this.groups.get(Preconditions.checkNotNull(role));
108 public synchronized boolean isTableSupported(final PeerId peerId) {
109 return this.peerTables.contains(peerId);
113 public synchronized PeerRole getRole(final YangInstanceIdentifier peerId) {
114 return this.peerRoles.get(peerId);
118 public synchronized boolean isAddPathSupportedByPeer(final PeerId peerId) {
119 final SendReceive sendReceive = this.peerAddPathTables.get(peerId);
120 return sendReceive != null && (sendReceive.equals(SendReceive.Both) || sendReceive.equals(SendReceive.Receive));