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.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Sets;
17 import java.util.Collections;
18 import java.util.EnumMap;
19 import java.util.HashMap;
22 import java.util.stream.Collectors;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
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.bgp.rib.spi.RibSupportUtils;
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.bgp.rib.rib.peer.SupportedTables;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
35 import org.opendaylight.yangtools.yang.binding.BindingMapping;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 final class ExportPolicyPeerTrackerImpl implements ExportPolicyPeerTracker {
47 private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTrackerImpl.class);
48 private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
49 private static final NodeIdentifier SEND_RECEIVE_NID = new NodeIdentifier(SEND_RECEIVE);
50 private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
51 private final Set<PeerId> peerTables = Sets.newHashSet();
52 private final PolicyDatabase policyDatabase;
53 private final Map<PeerId, SendReceive> peerAddPathTables = new HashMap<>();
54 private final TablesKey localTableKey;
55 private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
57 ExportPolicyPeerTrackerImpl(final PolicyDatabase policyDatabase, final TablesKey localTablesKey) {
58 this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
59 this.localTableKey = localTablesKey;
62 private Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
63 if (peerPathRoles.isEmpty()) {
64 return Collections.emptyMap();
67 final Map<PeerId, PeerExporTuple> immutablePeers = ImmutableMap.copyOf(peerPathRoles.entrySet().stream()
68 .collect(toMap(peer -> IdentifierUtils.peerKeyToPeerId(peer.getKey()), peer -> new PeerExporTuple(peer.getKey(), peer.getValue()))));
70 final Map<PeerRole, PeerExportGroup> ret = peerPathRoles.values().stream().collect(Collectors.toSet()).stream().filter(role -> role != PeerRole.Internal)
71 .collect(toMap(identity(), role -> new PeerExportGroupImpl(immutablePeers, this.policyDatabase.exportPolicyForRole(role)),
72 (oldKey, newKey) -> oldKey, () -> new EnumMap<>(PeerRole.class)));
78 public void peerRoleChanged(@Nonnull final YangInstanceIdentifier peerPath, @Nullable final PeerRole role) {
80 * This is a sledgehammer approach to the problem: modify the role map first,
81 * then construct the group map from scratch.
83 final PeerRole oldRole;
85 oldRole = this.peerRoles.put(peerPath, role);
87 oldRole = this.peerRoles.remove(peerPath);
90 if (role != oldRole) {
91 LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
92 this.groups = createGroups(this.peerRoles);
97 public void onTablesChanged(final PeerId peerId, final DataTreeCandidateNode tablesChange) {
98 final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, this.localTableKey);
99 final DataTreeCandidateNode localTableNode = tablesChange.getModifiedChild(supTablesKey);
100 if (localTableNode != null) {
101 final Optional<NormalizedNode<?, ?>> dataAfter = localTableNode.getDataAfter();
102 processSupportedSendReceiveTables(localTableNode.getModifiedChild(SEND_RECEIVE_NID), peerId);
103 if (dataAfter.isPresent()) {
104 final boolean added = this.peerTables.add(peerId);
106 LOG.debug("Supported table {} added to peer {}", this.localTableKey, peerId);
109 final NodeIdentifierWithPredicates value = (NodeIdentifierWithPredicates) localTableNode.getIdentifier();
110 this.peerTables.remove(peerId);
111 LOG.debug("Removed tables {} from peer {}", value, peerId);
117 public PeerExportGroup getPeerGroup(final PeerRole role) {
118 return this.groups.get(Preconditions.checkNotNull(role));
122 public PeerRole getRole(final YangInstanceIdentifier peerId) {
123 return this.peerRoles.get(peerId);
126 private void processSupportedSendReceiveTables(final DataTreeCandidateNode sendReceiveModChild, final PeerId peerId) {
127 if (sendReceiveModChild != null) {
128 if (sendReceiveModChild.getModificationType().equals(ModificationType.DELETE)) {
129 final Optional<NormalizedNode<?, ?>> sendReceiveNode = sendReceiveModChild.getDataBefore();
130 if (sendReceiveNode.isPresent()) {
131 final SendReceive sendReceiveValue = SendReceive.valueOf(BindingMapping.getClassName((String) sendReceiveNode.get().getValue()));
132 this.peerAddPathTables.remove(peerId);
133 LOG.debug("Supported Add BestPath table {} removed to peer {}", sendReceiveValue, peerId);
136 final Optional<NormalizedNode<?, ?>> sendReceiveNode = sendReceiveModChild.getDataAfter();
137 if (sendReceiveNode.isPresent()) {
138 final SendReceive sendReceiveValue = SendReceive.valueOf(BindingMapping.getClassName((String) sendReceiveNode.get().getValue()));
139 this.peerAddPathTables.put(peerId, sendReceiveValue);
140 LOG.debug("Supported Add BestPath table {} added to peer {}", sendReceiveValue, peerId);
147 public boolean isTableSupported(final PeerId peerId) {
148 return this.peerTables.contains(peerId);
152 public boolean isAddPathSupportedByPeer(final PeerId peerId) {
153 final SendReceive sendReceive = this.peerAddPathTables.get(peerId);
154 return sendReceive != null && (sendReceive.equals(SendReceive.Both) || sendReceive.equals(SendReceive.Receive));