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.Preconditions;
12 import com.google.common.collect.ArrayListMultimap;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.HashMultimap;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.Multimap;
18 import java.util.AbstractMap;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.EnumMap;
22 import java.util.HashMap;
24 import java.util.Map.Entry;
25 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.SupportedTables;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Tracks peers for adj-rib-out writeout.
39 final class ExportPolicyPeerTracker extends AbstractPeerRoleTracker {
40 private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTracker.class);
41 private static final Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>> GENERATE_PEERID = new Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>>() {
43 public Entry<PeerId, YangInstanceIdentifier> apply(final YangInstanceIdentifier input) {
44 final PeerId peerId = IdentifierUtils.peerId((NodeIdentifierWithPredicates) input.getLastPathArgument());
45 return new AbstractMap.SimpleImmutableEntry<>(peerId, input);
49 private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
50 private final HashMultimap<PeerId, NodeIdentifierWithPredicates> peerTables = HashMultimap.create();
51 private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
52 private final PolicyDatabase policyDatabase;
54 ExportPolicyPeerTracker(final PolicyDatabase policyDatabase) {
55 this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
58 private Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
59 if (peerPathRoles.isEmpty()) {
60 return Collections.emptyMap();
63 // Index things nicely for easy access
64 final Multimap<PeerRole, YangInstanceIdentifier> roleToIds = ArrayListMultimap.create(PeerRole.values().length, 2);
65 final Map<PeerId, PeerRole> idToRole = new HashMap<>();
66 for (final Entry<YangInstanceIdentifier, PeerRole> e : peerPathRoles.entrySet()) {
67 roleToIds.put(e.getValue(), e.getKey());
68 idToRole.put(IdentifierUtils.peerId((NodeIdentifierWithPredicates) e.getKey().getLastPathArgument()), e.getValue());
71 // Optimized immutable copy, reused for all PeerGroups
72 final Map<PeerId, PeerRole> allPeerRoles = ImmutableMap.copyOf(idToRole);
74 final Map<PeerRole, PeerExportGroup> ret = new EnumMap<>(PeerRole.class);
75 for (final Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
76 final AbstractExportPolicy policy = this.policyDatabase.exportPolicyForRole(e.getKey());
77 final Collection<Entry<PeerId, YangInstanceIdentifier>> peers = ImmutableList.copyOf(Collections2.transform(e.getValue(), GENERATE_PEERID));
79 ret.put(e.getKey(), new PeerExportGroup(peers, allPeerRoles, policy));
86 protected void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
88 * This is a sledgehammer approach to the problem: modify the role map first,
89 * then construct the group map from scratch.
91 final PeerRole oldRole;
93 oldRole = this.peerRoles.put(peerPath, role);
95 oldRole = this.peerRoles.remove(peerPath);
98 if (role != oldRole) {
99 LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
100 this.groups = createGroups(this.peerRoles);
104 PeerExportGroup getPeerGroup(final PeerRole role) {
105 return this.groups.get(Preconditions.checkNotNull(role));
108 boolean onTablesChanged(final PeerId peerId, final DataTreeCandidateNode node) {
109 if (node.getDataAfter().isPresent()) {
110 final NodeIdentifierWithPredicates value = (NodeIdentifierWithPredicates) node.getDataAfter().get().getIdentifier();
111 final boolean added = this.peerTables.put(peerId, value);
113 LOG.debug("Supported table {} added to peer {}", value, peerId);
117 LOG.debug("Removed tables {} from peer {}", this.peerTables.removeAll(peerId), peerId);
122 boolean isTableSupported(final PeerId peerId, final TablesKey tablesKey) {
123 return this.peerTables.get(peerId).contains(RibSupportUtils.toYangKey(SupportedTables.QNAME, tablesKey));
126 public PeerRole getRole(final YangInstanceIdentifier peerId) {
127 return this.peerRoles.get(peerId);