ec61de5ea182b3f8d82cda2030e9210e9b65299a
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ExportPolicyPeerTrackerImpl.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 static java.util.function.Function.identity;
11 import static java.util.stream.Collectors.toMap;
12
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Sets;
17 import java.util.Collections;
18 import java.util.EnumMap;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
26 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
27 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup;
28 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup.PeerExporTuple;
29 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.SupportedTables;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
35 import org.opendaylight.yangtools.yang.binding.BindingMapping;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 final class ExportPolicyPeerTrackerImpl implements ExportPolicyPeerTracker {
47     private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTrackerImpl.class);
48     private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
49     private static final NodeIdentifier SEND_RECEIVE_NID = new NodeIdentifier(SEND_RECEIVE);
50     private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
51     private final Set<PeerId> peerTables = Sets.newHashSet();
52     private final PolicyDatabase policyDatabase;
53     private final Map<PeerId, SendReceive> peerAddPathTables = new HashMap<>();
54     private final TablesKey localTableKey;
55     private volatile Map<PeerRole, PeerExportGroup> groups = Collections.emptyMap();
56
57     ExportPolicyPeerTrackerImpl(final PolicyDatabase policyDatabase, final TablesKey localTablesKey) {
58         this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
59         this.localTableKey = localTablesKey;
60     }
61
62     private Map<PeerRole, PeerExportGroup> createGroups(final Map<YangInstanceIdentifier, PeerRole> peerPathRoles) {
63         if (peerPathRoles.isEmpty()) {
64             return Collections.emptyMap();
65         }
66
67         final Map<PeerRole, Map<PeerId, PeerExporTuple>> immutablePeers = peerPathRoles.entrySet().stream()
68             .collect(Collectors.groupingBy(Map.Entry::getValue, toMap(peer -> IdentifierUtils.peerKeyToPeerId(peer
69             .getKey()), peer -> new PeerExporTuple(peer.getKey(), peer.getValue()))));
70
71         final Map<PeerRole, PeerExportGroup> ret = peerPathRoles.values().stream().collect(Collectors.toSet()).stream()
72             .collect(toMap(identity(), role -> new PeerExportGroupImpl(ImmutableMap.copyOf(immutablePeers.get(role)),
73                 this.policyDatabase.exportPolicyForRole(role)), (oldKey, newKey) -> oldKey, () -> new EnumMap<>(PeerRole.class)));
74
75         return ret;
76     }
77
78     @Override
79     public void peerRoleChanged(@Nonnull final YangInstanceIdentifier peerPath, @Nullable final PeerRole role) {
80         /*
81          * This is a sledgehammer approach to the problem: modify the role map first,
82          * then construct the group map from scratch.
83          */
84         final PeerRole oldRole;
85         if (role != null) {
86             oldRole = this.peerRoles.put(peerPath, role);
87         } else {
88             oldRole = this.peerRoles.remove(peerPath);
89         }
90
91         if (role != oldRole) {
92             LOG.debug("Peer {} changed role from {} to {}", peerPath, oldRole, role);
93             this.groups = createGroups(this.peerRoles);
94         }
95     }
96
97     @Override
98     public void onTablesChanged(final PeerId peerId, final DataTreeCandidateNode tablesChange) {
99         final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, this.localTableKey);
100         final DataTreeCandidateNode localTableNode = tablesChange.getModifiedChild(supTablesKey);
101         if (localTableNode != null) {
102             final Optional<NormalizedNode<?, ?>> dataAfter = localTableNode.getDataAfter();
103             processSupportedSendReceiveTables(localTableNode.getModifiedChild(SEND_RECEIVE_NID), peerId);
104             if (dataAfter.isPresent()) {
105                 final boolean added = this.peerTables.add(peerId);
106                 if (added) {
107                     LOG.debug("Supported table {} added to peer {}", this.localTableKey, peerId);
108                 }
109             } else {
110                 final NodeIdentifierWithPredicates value = (NodeIdentifierWithPredicates) localTableNode.getIdentifier();
111                 this.peerTables.remove(peerId);
112                 LOG.debug("Removed tables {} from peer {}", value, peerId);
113             }
114         }
115     }
116
117     @Override
118     public PeerExportGroup getPeerGroup(final PeerRole role) {
119         return this.groups.get(Preconditions.checkNotNull(role));
120     }
121
122     @Override
123     public PeerRole getRole(final YangInstanceIdentifier peerId) {
124         return this.peerRoles.get(peerId);
125     }
126
127     private void processSupportedSendReceiveTables(final DataTreeCandidateNode sendReceiveModChild, final PeerId peerId) {
128         if (sendReceiveModChild != null) {
129             if (sendReceiveModChild.getModificationType().equals(ModificationType.DELETE)) {
130                 final Optional<NormalizedNode<?, ?>> sendReceiveNode = sendReceiveModChild.getDataBefore();
131                 if (sendReceiveNode.isPresent()) {
132                     final SendReceive sendReceiveValue = SendReceive.valueOf(BindingMapping.getClassName((String) sendReceiveNode.get().getValue()));
133                     this.peerAddPathTables.remove(peerId);
134                     LOG.debug("Supported Add BestPath table {} removed to peer {}", sendReceiveValue, peerId);
135                 }
136             } else {
137                 final Optional<NormalizedNode<?, ?>> sendReceiveNode = sendReceiveModChild.getDataAfter();
138                 if (sendReceiveNode.isPresent()) {
139                     final SendReceive sendReceiveValue = SendReceive.valueOf(BindingMapping.getClassName((String) sendReceiveNode.get().getValue()));
140                     this.peerAddPathTables.put(peerId, sendReceiveValue);
141                     LOG.debug("Supported Add BestPath table {} added to peer {}", sendReceiveValue, peerId);
142                 }
143             }
144         }
145     }
146
147     @Override
148     public boolean isTableSupported(final PeerId peerId) {
149         return this.peerTables.contains(peerId);
150     }
151
152     @Override
153     public boolean isAddPathSupportedByPeer(final PeerId peerId) {
154         final SendReceive sendReceive = this.peerAddPathTables.get(peerId);
155         return sendReceive != null && (sendReceive.equals(SendReceive.Both) || sendReceive.equals(SendReceive.Receive));
156     }
157 }