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.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ArrayListMultimap;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.Multimap;
18 import com.google.common.collect.Sets;
19 import java.util.AbstractMap;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.EnumMap;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
27 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
28 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
29 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup;
30 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.SupportedTables;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
36 import org.opendaylight.yangtools.yang.binding.BindingMapping;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 final class ExportPolicyPeerTrackerImpl extends AbstractPeerRoleTracker implements ExportPolicyPeerTracker {
48 private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTrackerImpl.class);
49 private static final Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>> GENERATE_PEER_ID = new Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>>() {
51 public Entry<PeerId, YangInstanceIdentifier> apply(final YangInstanceIdentifier input) {
52 final PeerId peerId = IdentifierUtils.peerId((NodeIdentifierWithPredicates) input.getLastPathArgument());
53 return new AbstractMap.SimpleImmutableEntry<>(peerId, input);
56 private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
57 private static final NodeIdentifier SEND_RECEIVE_NID = new YangInstanceIdentifier.NodeIdentifier(SEND_RECEIVE);
58 private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
59 private final Set<PeerId> peerTables = Sets.newHashSet();
60 private final PolicyDatabase policyDatabase;
61 private final Map<PeerId, SendReceive> peerAddPathTables = new HashMap<>();
62 private final TablesKey localTableKey;
63 private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
65 ExportPolicyPeerTrackerImpl(final PolicyDatabase policyDatabase, final TablesKey localTablesKey) {
66 this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
67 this.localTableKey = localTablesKey;
70 private Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
71 if (peerPathRoles.isEmpty()) {
72 return Collections.emptyMap();
75 // Index things nicely for easy access
76 final Multimap<PeerRole, YangInstanceIdentifier> roleToIds = ArrayListMultimap.create(PeerRole.values().length, 2);
77 final Map<PeerId, PeerRole> idToRole = new HashMap<>();
78 for (final Entry<YangInstanceIdentifier, PeerRole> e : peerPathRoles.entrySet()) {
79 roleToIds.put(e.getValue(), e.getKey());
80 idToRole.put(IdentifierUtils.peerId((NodeIdentifierWithPredicates) e.getKey().getLastPathArgument()), e.getValue());
83 // Optimized immutable copy, reused for all PeerGroups
84 final Map<PeerId, PeerRole> allPeerRoles = ImmutableMap.copyOf(idToRole);
86 final Map<PeerRole, PeerExportGroup> ret = new EnumMap<>(PeerRole.class);
87 for (final Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
88 final AbstractExportPolicy policy = this.policyDatabase.exportPolicyForRole(e.getKey());
89 final Collection<Entry<PeerId, YangInstanceIdentifier>> peers = ImmutableList.copyOf(Collections2.transform(e.getValue(), GENERATE_PEER_ID));
91 ret.put(e.getKey(), new PeerExportGroupImpl(peers, allPeerRoles, policy));
98 protected void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
100 * This is a sledgehammer approach to the problem: modify the role map first,
101 * then construct the group map from scratch.
103 final PeerRole oldRole;
105 oldRole = this.peerRoles.put(peerPath, role);
107 oldRole = this.peerRoles.remove(peerPath);
110 if (role != oldRole) {
111 LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
112 this.groups = createGroups(this.peerRoles);
117 public void onTablesChanged(final PeerId peerId, final DataTreeCandidateNode tablesChange) {
118 final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, this.localTableKey);
119 final DataTreeCandidateNode localTableNode = tablesChange.getModifiedChild(supTablesKey);
120 if (localTableNode != null) {
121 final Optional<NormalizedNode<?, ?>> dataAfter = localTableNode.getDataAfter();
122 processSupportedSendReceiveTables(localTableNode.getModifiedChild(SEND_RECEIVE_NID), peerId);
123 if (dataAfter.isPresent()) {
124 final boolean added = this.peerTables.add(peerId);
126 LOG.debug("Supported table {} added to peer {}", this.localTableKey, peerId);
129 final NodeIdentifierWithPredicates value = (NodeIdentifierWithPredicates) localTableNode.getIdentifier();
130 this.peerTables.remove(peerId);
131 LOG.debug("Removed tables {} from peer {}", value, peerId);
137 public PeerExportGroup getPeerGroup(final PeerRole role) {
138 return this.groups.get(Preconditions.checkNotNull(role));
142 public PeerRole getRole(final YangInstanceIdentifier peerId) {
143 return this.peerRoles.get(peerId);
146 private void processSupportedSendReceiveTables(final DataTreeCandidateNode sendReceiveModChild, final PeerId peerId) {
147 if (sendReceiveModChild != null) {
148 if (sendReceiveModChild.getModificationType().equals(ModificationType.DELETE)) {
149 final Optional<NormalizedNode<?, ?>> sendReceiveNode = sendReceiveModChild.getDataBefore();
150 if (sendReceiveNode.isPresent()) {
151 final SendReceive sendReceiveValue = SendReceive.valueOf(BindingMapping.getClassName((String) sendReceiveNode.get().getValue()));
152 this.peerAddPathTables.remove(peerId);
153 LOG.debug("Supported Add BestPath table {} removed to peer {}", sendReceiveValue, peerId);
156 final Optional<NormalizedNode<?, ?>> sendReceiveNode = sendReceiveModChild.getDataAfter();
157 if (sendReceiveNode.isPresent()) {
158 final SendReceive sendReceiveValue = SendReceive.valueOf(BindingMapping.getClassName((String) sendReceiveNode.get().getValue()));
159 this.peerAddPathTables.put(peerId, sendReceiveValue);
160 LOG.debug("Supported Add BestPath table {} added to peer {}", sendReceiveValue, peerId);
167 public boolean isTableSupported(final PeerId peerId) {
168 return this.peerTables.contains(peerId);
172 public boolean isAddPathSupportedByPeer(final PeerId peerId) {
173 final SendReceive sendReceive = this.peerAddPathTables.get(peerId);
174 return sendReceive != null && (sendReceive.equals(SendReceive.Both) || sendReceive.equals(SendReceive.Receive));