2 * Copyright (c) 2014 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 com.google.common.base.Verify;
12 import com.google.common.net.InetAddresses;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Optional;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
22 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
25 import org.opendaylight.protocol.bgp.openconfig.spi.BGPConfigModuleTracker;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
28 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPPeerStats;
29 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPPeerStatsImpl;
30 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
31 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
32 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
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.PeerRole;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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 * Application Peer is a special case of BGP peer. It serves as an interface
53 * for user to advertise user routes to ODL and through ODL to other BGP peers.
55 * This peer has it's own RIB, where it stores all user routes. This RIB is
56 * located in configurational datastore. Routes are added through RESTCONF.
58 * They are then processed as routes from any other peer, through AdjRib,
59 * EffectiveRib,LocRib and if they are advertised further, through AdjRibOut.
61 * For purposed of import policies such as Best Path Selection, application
62 * peer needs to have a BGP-ID that is configurable.
64 public class ApplicationPeer implements AutoCloseable, org.opendaylight.protocol.bgp.rib.spi.Peer, ClusteredDOMDataTreeChangeListener, TransactionChainListener {
66 private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
68 private final byte[] rawIdentifier;
69 private final String name;
70 private final YangInstanceIdentifier adjRibsInId;
71 private final Ipv4Address ipAddress;
72 private final BGPConfigModuleTracker moduleTracker;
73 private final RIB rib;
74 private final YangInstanceIdentifier peerIId;
75 private DOMTransactionChain chain;
76 private DOMTransactionChain writerChain;
77 private EffectiveRibInWriter effectiveRibInWriter;
78 private AdjRibInWriter adjRibInWriter;
79 private BGPPeerStats peerStats;
81 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIB rib,
82 final BGPConfigModuleTracker moduleTracker) {
83 this.name = applicationRibId.getValue();
84 final RIB targetRib = Preconditions.checkNotNull(rib);
85 this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
86 final NodeIdentifierWithPredicates peerId = IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress));
87 this.peerIId = targetRib.getYangRibId().node(Peer.QNAME).node(peerId);
88 this.adjRibsInId = this.peerIId.node(AdjRibIn.QNAME).node(Tables.QNAME);
90 this.ipAddress = ipAddress;
91 this.moduleTracker = moduleTracker;
94 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address bgpPeerId, final RIB targetRibDependency) {
95 this(applicationRibId, bgpPeerId, targetRibDependency, null);
98 public void instantiateServiceInstance() {
99 this.chain = this.rib.createPeerChain(this);
100 this.writerChain = this.rib.createPeerChain(this);
102 final Optional<SimpleRoutingPolicy> simpleRoutingPolicy = Optional.of(SimpleRoutingPolicy.AnnounceNone);
103 final PeerId peerId = RouterIds.createPeerId(this.ipAddress);
104 final Set<TablesKey> localTables = this.rib.getLocalTablesKeys();
105 localTables.forEach(tablesKey -> {
106 final ExportPolicyPeerTracker exportTracker = this.rib.getExportPolicyPeerTracker(tablesKey);
107 if (exportTracker != null) {
108 exportTracker.registerPeer(peerId, null, this.peerIId, PeerRole.Internal, simpleRoutingPolicy);
112 this.adjRibInWriter = AdjRibInWriter.create(this.rib.getYangRibId(), PeerRole.Internal, simpleRoutingPolicy, this.writerChain);
113 final RIBSupportContextRegistry context = this.rib.getRibSupportContext();
114 this.adjRibInWriter = this.adjRibInWriter.transform(peerId, context, localTables, Collections.emptyMap());
115 this.peerStats = new BGPPeerStatsImpl(this.name, localTables);
116 this.effectiveRibInWriter = EffectiveRibInWriter.create(this.rib.getService(), this.rib.createPeerChain(this), this.peerIId,
117 this.rib.getImportPolicyPeerTracker(), context, PeerRole.Internal, this.peerStats.getEffectiveRibInRouteCounters(),
118 this.peerStats.getAdjRibInRouteCounters());
119 if (moduleTracker != null) {
120 moduleTracker.onInstanceCreate();
125 * Routes come from application RIB that is identified by (configurable) name.
126 * Each route is pushed into AdjRibsInWriter with it's whole context. In this
127 * method, it doesn't matter if the routes are removed or added, this will
128 * be determined in LocRib.
131 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
132 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
133 LOG.debug("Received data change to ApplicationRib {}", changes);
134 for (final DataTreeCandidate tc : changes) {
135 LOG.debug("Modification Type {}", tc.getRootNode().getModificationType());
136 final YangInstanceIdentifier path = tc.getRootPath();
137 final PathArgument lastArg = path.getLastPathArgument();
138 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), path);
139 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
140 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
141 final PathArgument childIdentifier = child.getIdentifier();
142 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(childIdentifier);
143 switch (child.getModificationType()) {
145 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
146 tx.delete(LogicalDatastoreType.OPERATIONAL, tableId);
151 case SUBTREE_MODIFIED:
152 if (EffectiveRibInWriter.TABLE_ROUTES.equals(childIdentifier)) {
153 processRoutesTable(child, tableId, tx, tableId);
157 if (child.getDataAfter().isPresent()) {
158 final NormalizedNode<?,?> dataAfter = child.getDataAfter().get();
159 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
160 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
161 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, dataAfter);
173 * Applies modification under table routes based on modification type instead of only put. BUG 4438
177 * @param routeTableIdentifier
179 private void processRoutesTable(final DataTreeCandidateNode node, final YangInstanceIdentifier identifier,
180 final DOMDataWriteTransaction tx, final YangInstanceIdentifier routeTableIdentifier) {
181 for (final DataTreeCandidateNode child : node.getChildNodes()) {
182 final YangInstanceIdentifier childIdentifier = identifier.node(child.getIdentifier());
183 switch (child.getModificationType()) {
185 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
186 tx.delete(LogicalDatastoreType.OPERATIONAL, childIdentifier);
191 case SUBTREE_MODIFIED:
192 //For be ables to use DELETE when we remove specific routes as we do when we remove the whole routes,
193 // we need to go deeper three levels
194 if (!routeTableIdentifier.equals(childIdentifier.getParent().getParent().getParent())) {
195 processRoutesTable(child, childIdentifier, tx, routeTableIdentifier);
199 if (child.getDataAfter().isPresent()) {
200 final NormalizedNode<?,?> dataAfter = child.getDataAfter().get();
201 LOG.trace("App peer -> AdjRibsIn path : {}", childIdentifier);
202 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
203 tx.put(LogicalDatastoreType.OPERATIONAL, childIdentifier, dataAfter);
213 public String getName() {
218 public void close() {
219 if(this.effectiveRibInWriter != null) {
220 this.effectiveRibInWriter.close();
222 if(this.adjRibInWriter != null) {
223 this.adjRibInWriter.removePeer();
225 if(this.chain != null) {
228 if(this.writerChain != null) {
229 this.writerChain.close();
231 if (this.moduleTracker != null) {
232 this.moduleTracker.onInstanceClose();
237 public byte[] getRawIdentifier() {
238 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
242 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
243 final Throwable cause) {
244 LOG.error("Transaction chain failed.", cause);
248 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
249 LOG.debug("Transaction chain {} successfull.", chain);