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.controller.md.sal.dom.api.DOMDataTreeChangeService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Tracks peers for adj-rib-out writeout.
35 final class ExportPolicyPeerTracker extends AbstractPeerRoleTracker {
36 private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTracker.class);
37 private static final Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>> GENERATE_PEERID = new Function<YangInstanceIdentifier, Entry<PeerId, YangInstanceIdentifier>>() {
39 public Entry<PeerId, YangInstanceIdentifier> apply(final YangInstanceIdentifier input) {
40 final PeerId peerId = IdentifierUtils.peerId((NodeIdentifierWithPredicates) input.getLastPathArgument());
41 return new AbstractMap.SimpleImmutableEntry<>(peerId, input);
45 private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
46 private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
47 private final PolicyDatabase policyDatabase;
49 ExportPolicyPeerTracker(final DOMDataTreeChangeService service, final YangInstanceIdentifier ribId, final PolicyDatabase policyDatabase) {
50 super(service, ribId);
51 this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
54 private Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
55 if (peerPathRoles.isEmpty()) {
56 return Collections.emptyMap();
59 // Index things nicely for easy access
60 final Multimap<PeerRole, YangInstanceIdentifier> roleToIds = ArrayListMultimap.create(PeerRole.values().length, 2);
61 final Map<PeerId, PeerRole> idToRole = new HashMap<>();
62 for (Entry<YangInstanceIdentifier, PeerRole> e : peerPathRoles.entrySet()) {
63 roleToIds.put(e.getValue(), e.getKey());
64 idToRole.put(IdentifierUtils.peerId((NodeIdentifierWithPredicates) e.getKey().getLastPathArgument()), e.getValue());
67 // Optimized immutable copy, reused for all PeerGroups
68 final Map<PeerId, PeerRole> peerRoles = ImmutableMap.copyOf(idToRole);
70 final Map<PeerRole, PeerExportGroup> ret = new EnumMap<>(PeerRole.class);
71 for (Entry<PeerRole, Collection<YangInstanceIdentifier>> e : roleToIds.asMap().entrySet()) {
72 final AbstractExportPolicy policy = policyDatabase.exportPolicyForRole(e.getKey());
73 final Collection<Entry<PeerId, YangInstanceIdentifier>> peers = ImmutableList.copyOf(Collections2.transform(e.getValue(), GENERATE_PEERID));
75 ret.put(e.getKey(), new PeerExportGroup(peers, peerRoles, policy));
82 protected void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
84 * This is a sledgehammer approach to the problem: modify the role map first,
85 * then construct the group map from scratch.
87 final PeerRole oldRole;
89 oldRole = peerRoles.put(peerPath, role);
91 oldRole = peerRoles.remove(peerPath);
94 if (role != oldRole) {
95 LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
96 groups = createGroups(peerRoles);
100 PeerExportGroup getPeerGroup(final PeerRole role) {
101 return groups.get(Preconditions.checkNotNull(role));