BUG-4939: Remove BGP Peer structure from DS once session is close
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ExportPolicyPeerTracker.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
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;
23 import java.util.Map;
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;
35
36 /**
37  * Tracks peers for adj-rib-out writeout.
38  */
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>>() {
42         @Override
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);
46         }
47     };
48
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;
53
54     ExportPolicyPeerTracker(final PolicyDatabase policyDatabase) {
55         this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
56     }
57
58     private Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
59         if (peerPathRoles.isEmpty()) {
60             return Collections.emptyMap();
61         }
62
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());
69         }
70
71         // Optimized immutable copy, reused for all PeerGroups
72         final Map<PeerId, PeerRole> allPeerRoles = ImmutableMap.copyOf(idToRole);
73
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));
78
79             ret.put(e.getKey(), new PeerExportGroup(peers, allPeerRoles, policy));
80         }
81
82         return ret;
83     }
84
85     @Override
86     protected void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
87         /*
88          * This is a sledgehammer approach to the problem: modify the role map first,
89          * then construct the group map from scratch.
90          */
91         final PeerRole oldRole;
92         if (role != null) {
93             oldRole = this.peerRoles.put(peerPath, role);
94         } else {
95             oldRole = this.peerRoles.remove(peerPath);
96         }
97
98         if (role != oldRole) {
99             LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
100             this.groups = createGroups(this.peerRoles);
101         }
102     }
103
104     PeerExportGroup getPeerGroup(final PeerRole role) {
105         return this.groups.get(Preconditions.checkNotNull(role));
106     }
107
108     void 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);
112             if (added) {
113                 LOG.debug("Supported table {} added to peer {}", value, peerId);
114             }
115         } else {
116             LOG.debug("Removed tables {} from peer {}", this.peerTables.removeAll(peerId), peerId);
117         }
118     }
119
120     boolean isTableSupported(final PeerId peerId, final TablesKey tablesKey) {
121         return this.peerTables.get(peerId).contains(RibSupportUtils.toYangKey(SupportedTables.QNAME, tablesKey));
122     }
123 }