2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.atomic.LongAdder;
16 import javax.annotation.Nonnull;
17 import javax.annotation.concurrent.NotThreadSafe;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.Codecs;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
24 import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesSentCounters;
25 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
26 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
27 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4Route;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibOut;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
46 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
47 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Instantiated for each peer and table, listens on a particular peer's adj-rib-out,
53 * performs transcoding to BA form (message) and sends it down the channel.
56 final class AdjRibOutListener implements ClusteredDOMDataTreeChangeListener, PrefixesSentCounters {
58 private static final Logger LOG = LoggerFactory.getLogger(AdjRibOutListener.class);
60 static final QName PREFIX_QNAME = QName.create(Ipv4Route.QNAME, "prefix").intern();
61 private final YangInstanceIdentifier.NodeIdentifier routeKeyLeaf = new YangInstanceIdentifier.NodeIdentifier(PREFIX_QNAME);
63 private final ChannelOutputLimiter session;
64 private final Codecs codecs;
65 private final RIBSupport support;
66 private final boolean mpSupport;
67 private final ListenerRegistration<AdjRibOutListener> registerDataTreeChangeListener;
68 private final LongAdder routeCounter;
69 private final LongAdder prefixesSentCounter = new LongAdder();
71 private AdjRibOutListener(final PeerId peerId, final TablesKey tablesKey, final YangInstanceIdentifier ribId,
72 final CodecsRegistry registry, final RIBSupport support, final DOMDataTreeChangeService service,
73 final ChannelOutputLimiter session, final boolean mpSupport, final LongAdder routeCounter) {
74 this.session = Preconditions.checkNotNull(session);
75 this.support = Preconditions.checkNotNull(support);
76 this.codecs = registry.getCodecs(this.support);
77 this.mpSupport = mpSupport;
78 final YangInstanceIdentifier adjRibOutId = ribId.node(Peer.QNAME).node(IdentifierUtils.domPeerId(peerId)).node(AdjRibOut.QNAME).node(Tables.QNAME).node(RibSupportUtils.toYangTablesKey(tablesKey));
79 this.registerDataTreeChangeListener = service.registerDataTreeChangeListener(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, adjRibOutId), this);
80 this.routeCounter = routeCounter;
83 static AdjRibOutListener create(@Nonnull final PeerId peerId, @Nonnull final TablesKey tablesKey, @Nonnull final YangInstanceIdentifier ribId,
84 @Nonnull final CodecsRegistry registry, @Nonnull final RIBSupport support, @Nonnull final DOMDataTreeChangeService service,
85 @Nonnull final ChannelOutputLimiter session, @Nonnull final boolean mpSupport, @Nonnull final LongAdder routeCounter
87 return new AdjRibOutListener(peerId, tablesKey, ribId, registry, support, service, session, mpSupport, routeCounter);
91 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
92 LOG.debug("Data change received for AdjRibOut {}", changes);
93 for (final DataTreeCandidate tc : changes) {
94 LOG.trace("Change {} type {}", tc.getRootNode(), tc.getRootNode().getModificationType());
95 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
96 processSupportedFamilyRoutes(child);
102 private void processSupportedFamilyRoutes(final DataTreeCandidateNode child) {
103 for (final DataTreeCandidateNode route : this.support.changedRoutes(child)) {
104 processRouteChange(route);
108 private void processRouteChange(final DataTreeCandidateNode route) {
110 switch (route.getModificationType()) {
112 LOG.debug("Skipping unmodified route {}", route.getIdentifier());
116 // FIXME: we can batch deletions into a single batch
117 update = withdraw((MapEntryNode) route.getDataBefore().get());
118 LOG.debug("Withdrawing routes {}", update);
121 case SUBTREE_MODIFIED:
123 update = advertise((MapEntryNode) route.getDataAfter().get());
124 LOG.debug("Advertising routes {}", update);
127 LOG.warn("Ignoring unhandled modification type {}", route.getModificationType());
130 this.session.write(update);
133 private Attributes routeAttributes(final MapEntryNode route) {
134 if (LOG.isDebugEnabled()) {
135 LOG.debug("AdjRibOut parsing route {}", NormalizedNodes.toStringTree(route));
137 final ContainerNode advertisedAttrs = (ContainerNode) NormalizedNodes.findNode(route, this.support.routeAttributesIdentifier()).orNull();
138 return this.codecs.deserializeAttributes(advertisedAttrs);
141 private Update withdraw(final MapEntryNode route) {
142 this.routeCounter.decrement();
143 if (!this.mpSupport) {
144 return buildUpdate(Collections.emptyList(), Collections.singleton(route), routeAttributes(route));
146 return this.support.buildUpdate(Collections.emptyList(), Collections.singleton(route), routeAttributes(route));
149 private Update advertise(final MapEntryNode route) {
150 this.routeCounter.increment();
151 this.prefixesSentCounter.increment();
152 if (!this.mpSupport) {
153 return buildUpdate(Collections.singleton(route), Collections.emptyList(), routeAttributes(route));
155 return this.support.buildUpdate(Collections.singleton(route), Collections.emptyList(), routeAttributes(route));
158 private Update buildUpdate(@Nonnull final Collection<MapEntryNode> advertised, @Nonnull final Collection<MapEntryNode> withdrawn, @Nonnull final Attributes attr) {
159 final UpdateBuilder ub = new UpdateBuilder()
160 .setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(extractPrefixes(withdrawn)).build())
161 .setNlri(new NlriBuilder().setNlri(extractPrefixes(advertised)).build());
162 ub.setAttributes(attr);
166 private List<Ipv4Prefix> extractPrefixes(final Collection<MapEntryNode> routes) {
167 final List<Ipv4Prefix> prefs = new ArrayList<>(routes.size());
168 for (final MapEntryNode ipv4Route : routes) {
169 final String prefix = (String) ipv4Route.getChild(this.routeKeyLeaf).get().getValue();
170 prefs.add(new Ipv4Prefix(prefix));
175 public void close() {
176 this.registerDataTreeChangeListener.close();
179 boolean isMpSupported() {
180 return this.mpSupport;
184 public long getPrefixesSentCount() {
185 return this.prefixesSentCounter.longValue();