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