Merge "BUG-2383: implement EffectiveRibInWriter"
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / LocRibWriter.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.Preconditions;
11 import com.google.common.primitives.UnsignedInteger;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
21 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
31
32 // FIXME: instantiate for each table, listen on wildcard peer and routes
33 @NotThreadSafe
34 final class LocRibWriter implements AutoCloseable, DOMDataTreeChangeListener {
35     private final Map<PathArgument, RouteEntry> routeEntries = new HashMap<>();
36     private final YangInstanceIdentifier target;
37     private final DOMTransactionChain chain;
38     private final ExportPolicyPeerTracker peerPolicyTracker;
39     private final NodeIdentifier attributesIdentifier;
40     private final Long ourAs;
41
42     LocRibWriter(final RIBSupport ribSupport, final DOMTransactionChain chain, final YangInstanceIdentifier target, final Long ourAs) {
43         this.chain = Preconditions.checkNotNull(chain);
44         this.target = Preconditions.checkNotNull(target);
45         this.ourAs = Preconditions.checkNotNull(ourAs);
46         this.attributesIdentifier = ribSupport.routeAttributesIdentifier();
47
48         // FIXME: proper values
49         this.peerPolicyTracker = new ExportPolicyPeerTracker(null, null);
50     }
51
52     @Override
53     public void close() {
54         peerPolicyTracker.close();
55     }
56
57     @Override
58     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
59         /*
60          * We use two-stage processing here in hopes that we avoid duplicate
61          * calculations when multiple peers have changed a particular entry.
62          */
63         final Map<RouteUpdateKey, RouteEntry> toUpdate = new HashMap<>();
64         for (DataTreeCandidate tc : changes) {
65             final YangInstanceIdentifier path = tc.getRootPath();
66             final PathArgument routeId = path.getLastPathArgument();
67             final NodeIdentifierWithPredicates peerKey = IdentifierUtils.peerKey(path);
68             final PeerId peerId = IdentifierUtils.peerId(peerKey);
69             final UnsignedInteger routerId = RouterIds.routerIdForPeerId(peerId);
70
71             RouteEntry entry = routeEntries.get(routeId);
72             if (tc.getRootNode().getDataAfter().isPresent()) {
73                 if (entry == null) {
74                     entry = new RouteEntry();
75                     routeEntries.put(routeId, entry);
76                 }
77
78                 entry.addRoute(routerId, (ContainerNode) tc.getRootNode().getDataAfter().get());
79             } else if (entry != null) {
80                 if (entry.removeRoute(routerId)) {
81                     routeEntries.remove(routeId);
82                     entry = null;
83                 }
84             }
85
86             toUpdate.put(new RouteUpdateKey(peerId, routeId), entry);
87         }
88
89         final DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
90
91         // Now walk all updated entries
92         for (Entry<RouteUpdateKey, RouteEntry> e : toUpdate.entrySet()) {
93             final RouteEntry entry = e.getValue();
94             final NormalizedNode<?, ?> value;
95
96             if (entry != null) {
97                 if (!entry.selectBest(ourAs)) {
98                     // Best path has not changed, no need to do anything else. Proceed to next route.
99                     continue;
100                 }
101
102                 value = entry.bestValue(e.getKey().getRouteId());
103             } else {
104                 value = null;
105             }
106
107             if (value != null) {
108                 tx.put(LogicalDatastoreType.OPERATIONAL, target.node(e.getKey().getRouteId()), value);
109             } else {
110                 tx.delete(LogicalDatastoreType.OPERATIONAL, target.node(e.getKey().getRouteId()));
111             }
112
113             /*
114              * We need to keep track of routers and populate adj-ribs-out, too. If we do not, we need to
115              * expose from which client a particular route was learned from in the local RIB, and have
116              * the listener perform filtering.
117              *
118              * We walk the policy set in order to minimize the amount of work we do for multiple peers:
119              * if we have two eBGP peers, for example, there is no reason why we should perform the translation
120              * multiple times.
121              */
122             for (PeerRole role : PeerRole.values()) {
123                 final PeerExportGroup peerGroup = peerPolicyTracker.getPeerGroup(role);
124                 if (peerGroup != null) {
125                     final ContainerNode attributes = null;
126                     final PeerId peerId = e.getKey().getPeerId();
127                     final ContainerNode effectiveAttributes = peerGroup.effectiveAttributes(peerId, attributes);
128
129                     for (Entry<PeerId, YangInstanceIdentifier> pid : peerGroup.getPeers()) {
130                         // This points to adj-rib-out for a particular peer/table combination
131                         final YangInstanceIdentifier routeTarget = pid.getValue().node(e.getKey().getRouteId());
132
133                         if (effectiveAttributes != null && value != null && !peerId.equals(pid.getKey())) {
134                             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
135                             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(attributesIdentifier), effectiveAttributes);
136                         } else {
137                             tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);
138                         }
139                     }
140                 }
141             }
142         }
143
144         tx.submit();
145     }
146 }