BUG-2383: split out policies into separate files
[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, 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 (final 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 = this.routeEntries.get(routeId);
72             if (tc.getRootNode().getDataAfter().isPresent()) {
73                 if (entry == null) {
74                     entry = new RouteEntry();
75                     this.routeEntries.put(routeId, entry);
76                 }
77
78                 entry.addRoute(routerId, (ContainerNode) tc.getRootNode().getDataAfter().get());
79             } else if (entry != null && entry.removeRoute(routerId)) {
80                 this.routeEntries.remove(routeId);
81                 entry = null;
82             }
83
84             toUpdate.put(new RouteUpdateKey(peerId, routeId), entry);
85         }
86
87         final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
88
89         // Now walk all updated entries
90         for (final Entry<RouteUpdateKey, RouteEntry> e : toUpdate.entrySet()) {
91             final RouteEntry entry = e.getValue();
92             final NormalizedNode<?, ?> value;
93
94             if (entry != null) {
95                 if (!entry.selectBest(this.ourAs)) {
96                     // Best path has not changed, no need to do anything else. Proceed to next route.
97                     continue;
98                 }
99
100                 value = entry.bestValue(e.getKey().getRouteId());
101             } else {
102                 value = null;
103             }
104
105             if (value != null) {
106                 tx.put(LogicalDatastoreType.OPERATIONAL, this.target.node(e.getKey().getRouteId()), value);
107             } else {
108                 tx.delete(LogicalDatastoreType.OPERATIONAL, this.target.node(e.getKey().getRouteId()));
109             }
110
111             /*
112              * We need to keep track of routers and populate adj-ribs-out, too. If we do not, we need to
113              * expose from which client a particular route was learned from in the local RIB, and have
114              * the listener perform filtering.
115              *
116              * We walk the policy set in order to minimize the amount of work we do for multiple peers:
117              * if we have two eBGP peers, for example, there is no reason why we should perform the translation
118              * multiple times.
119              */
120             for (PeerRole role : PeerRole.values()) {
121                 final PeerExportGroup peerGroup = peerPolicyTracker.getPeerGroup(role);
122                 if (peerGroup != null) {
123                     final ContainerNode attributes = null;
124                     final PeerId peerId = e.getKey().getPeerId();
125                     final ContainerNode effectiveAttributes = peerGroup.effectiveAttributes(peerId, attributes);
126
127                     for (final Entry<PeerId, YangInstanceIdentifier> pid : peerGroup.getPeers()) {
128                         // This points to adj-rib-out for a particular peer/table combination
129                         final YangInstanceIdentifier routeTarget = pid.getValue().node(e.getKey().getRouteId());
130
131                         if (effectiveAttributes != null && value != null && !peerId.equals(pid.getKey())) {
132                             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
133                             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(attributesIdentifier), effectiveAttributes);
134                         } else {
135                             tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);
136                         }
137                     }
138                 }
139             }
140         }
141
142         tx.submit();
143     }
144 }