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.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Multimap;
17 import java.util.AbstractMap;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.EnumMap;
21 import java.util.HashMap;
23 import java.util.Map.Entry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Tracks peers for adj-rib-out writeout.
34 final class ExportPolicyPeerTracker extends AbstractPeerRoleTracker {
35 private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTracker.class);
36 private static final Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>> GENERATE_PEERID = new Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>>() {
38 public Entry<PeerId, YangInstanceIdentifier> apply(final YangInstanceIdentifier input) {
39 final PeerId peerId = IdentifierUtils.peerId((NodeIdentifierWithPredicates) input.getLastPathArgument());
40 return new AbstractMap.SimpleImmutableEntry<>(peerId, input);
44 private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
45 private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
46 private final PolicyDatabase policyDatabase;
48 ExportPolicyPeerTracker(final PolicyDatabase policyDatabase) {
49 this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
52 private Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
53 if (peerPathRoles.isEmpty()) {
54 return Collections.emptyMap();
57 // Index things nicely for easy access
58 final Multimap<PeerRole, YangInstanceIdentifier> roleToIds = ArrayListMultimap.create(PeerRole.values().length, 2);
59 final Map<PeerId, PeerRole> idToRole = new HashMap<>();
60 for (final Entry<YangInstanceIdentifier, PeerRole> e : peerPathRoles.entrySet()) {
61 roleToIds.put(e.getValue(), e.getKey());
62 idToRole.put(IdentifierUtils.peerId((NodeIdentifierWithPredicates) e.getKey().getLastPathArgument()), e.getValue());
65 // Optimized immutable copy, reused for all PeerGroups
66 final Map<PeerId, PeerRole> allPeerRoles = ImmutableMap.copyOf(idToRole);
68 final Map<PeerRole, PeerExportGroup> ret = new EnumMap<>(PeerRole.class);
69 for (final Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
70 final AbstractExportPolicy policy = this.policyDatabase.exportPolicyForRole(e.getKey());
71 final Collection<Entry<PeerId, YangInstanceIdentifier>> peers = ImmutableList.copyOf(Collections2.transform(e.getValue(), GENERATE_PEERID));
73 ret.put(e.getKey(), new PeerExportGroup(peers, allPeerRoles, policy));
80 protected void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
82 * This is a sledgehammer approach to the problem: modify the role map first,
83 * then construct the group map from scratch.
85 final PeerRole oldRole;
87 oldRole = this.peerRoles.put(peerPath, role);
89 oldRole = this.peerRoles.remove(peerPath);
92 if (role != oldRole) {
93 LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
94 this.groups = createGroups(this.peerRoles);
98 PeerExportGroup getPeerGroup(final PeerRole role) {
99 return this.groups.get(Preconditions.checkNotNull(role));