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.HashSet;
17 import java.util.Optional;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
23 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
28 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
30 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPPeerStats;
31 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPPeerStatsImpl;
32 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
33 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
34 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
35 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * Application Peer is a special case of BGP peer. It serves as an interface
57 * for user to advertise user routes to ODL and through ODL to other BGP peers.
59 * This peer has it's own RIB, where it stores all user routes. This RIB is
60 * located in configurational datastore. Routes are added through RESTCONF.
62 * They are then processed as routes from any other peer, through AdjRib,
63 * EffectiveRib,LocRib and if they are advertised further, through AdjRibOut.
65 * For purposed of import policies such as Best Path Selection, application
66 * peer needs to have a BGP-ID that is configurable.
68 public class ApplicationPeer implements AutoCloseable, org.opendaylight.protocol.bgp.rib.spi.Peer, ClusteredDOMDataTreeChangeListener, TransactionChainListener {
70 private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
72 private final byte[] rawIdentifier;
73 private final String name;
74 private final YangInstanceIdentifier adjRibsInId;
75 private final Ipv4Address ipAddress;
76 private final RIB rib;
77 private final YangInstanceIdentifier peerIId;
78 private DOMTransactionChain chain;
79 private DOMTransactionChain writerChain;
80 private EffectiveRibInWriter effectiveRibInWriter;
81 private AdjRibInWriter adjRibInWriter;
82 private BGPPeerStats peerStats;
83 private ListenerRegistration<ApplicationPeer> registration;
84 private final Set<NodeIdentifierWithPredicates> supportedTables = new HashSet<>();
88 interface RegisterAppPeerListener {
90 * Register Application Peer Change Listener once AdjRibIn has been successfully initialized.
95 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIB rib) {
96 this.name = applicationRibId.getValue();
97 final RIB targetRib = Preconditions.checkNotNull(rib);
98 this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
99 final NodeIdentifierWithPredicates peerId = IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress));
100 this.peerIId = targetRib.getYangRibId().node(Peer.QNAME).node(peerId);
101 this.adjRibsInId = this.peerIId.node(AdjRibIn.QNAME).node(Tables.QNAME);
102 this.rib = targetRib;
103 this.ipAddress = ipAddress;
106 public synchronized void instantiateServiceInstance(final DOMDataTreeChangeService dataTreeChangeService,
107 final DOMDataTreeIdentifier appPeerDOMId) {
108 this.chain = this.rib.createPeerChain(this);
109 this.writerChain = this.rib.createPeerChain(this);
111 final Optional<SimpleRoutingPolicy> simpleRoutingPolicy = Optional.of(SimpleRoutingPolicy.AnnounceNone);
112 final PeerId peerId = RouterIds.createPeerId(this.ipAddress);
113 final Set<TablesKey> localTables = this.rib.getLocalTablesKeys();
114 localTables.forEach(tablesKey -> {
115 final ExportPolicyPeerTracker exportTracker = this.rib.getExportPolicyPeerTracker(tablesKey);
116 if (exportTracker != null) {
117 exportTracker.registerPeer(peerId, null, this.peerIId, PeerRole.Internal, simpleRoutingPolicy);
119 this.supportedTables.add(RibSupportUtils.toYangTablesKey(tablesKey));
122 this.adjRibInWriter = AdjRibInWriter.create(this.rib.getYangRibId(), PeerRole.Internal, simpleRoutingPolicy, this.writerChain);
123 final RIBSupportContextRegistry context = this.rib.getRibSupportContext();
124 final RegisterAppPeerListener registerAppPeerListener = () -> {
125 synchronized (this) {
126 if(this.chain != null) {
127 this.registration = dataTreeChangeService.registerDataTreeChangeListener(appPeerDOMId, this);
131 this.adjRibInWriter = this.adjRibInWriter.transform(peerId, context, localTables, Collections.emptyMap(),
132 registerAppPeerListener);
133 this.peerStats = new BGPPeerStatsImpl(this.name, localTables);
134 this.effectiveRibInWriter = EffectiveRibInWriter.create(this.rib.getService(), this.rib.createPeerChain(this), this.peerIId,
135 this.rib.getImportPolicyPeerTracker(), context, PeerRole.Internal, this.peerStats.getEffectiveRibInRouteCounters(),
136 this.peerStats.getAdjRibInRouteCounters());
140 * Routes come from application RIB that is identified by (configurable) name.
141 * Each route is pushed into AdjRibsInWriter with it's whole context. In this
142 * method, it doesn't matter if the routes are removed or added, this will
143 * be determined in LocRib.
146 public synchronized void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
147 if(this.chain == null) {
148 LOG.trace("Skipping data changed called to Application Peer. Change : {}", changes);
151 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
152 LOG.debug("Received data change to ApplicationRib {}", changes);
153 for (final DataTreeCandidate tc : changes) {
154 LOG.debug("Modification Type {}", tc.getRootNode().getModificationType());
155 final YangInstanceIdentifier path = tc.getRootPath();
156 final PathArgument lastArg = path.getLastPathArgument();
157 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), path);
158 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
159 if (!this.supportedTables.contains(tableKey)) {
160 LOG.trace("Skipping received data change for non supported family {}.", tableKey);
163 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
164 final PathArgument childIdentifier = child.getIdentifier();
165 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(childIdentifier);
166 switch (child.getModificationType()) {
168 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
169 tx.delete(LogicalDatastoreType.OPERATIONAL, tableId);
174 case SUBTREE_MODIFIED:
175 if (EffectiveRibInWriter.TABLE_ROUTES.equals(childIdentifier)) {
176 processRoutesTable(child, tableId, tx, tableId);
180 if (child.getDataAfter().isPresent()) {
181 final NormalizedNode<?,?> dataAfter = child.getDataAfter().get();
182 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
183 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
184 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, dataAfter);
196 * Applies modification under table routes based on modification type instead of only put. BUG 4438
200 * @param routeTableIdentifier
202 private synchronized void processRoutesTable(final DataTreeCandidateNode node, final YangInstanceIdentifier identifier,
203 final DOMDataWriteTransaction tx, final YangInstanceIdentifier routeTableIdentifier) {
204 for (final DataTreeCandidateNode child : node.getChildNodes()) {
205 final YangInstanceIdentifier childIdentifier = identifier.node(child.getIdentifier());
206 switch (child.getModificationType()) {
208 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
209 tx.delete(LogicalDatastoreType.OPERATIONAL, childIdentifier);
214 case SUBTREE_MODIFIED:
215 //For be ables to use DELETE when we remove specific routes as we do when we remove the whole routes,
216 // we need to go deeper three levels
217 if (!routeTableIdentifier.equals(childIdentifier.getParent().getParent().getParent())) {
218 processRoutesTable(child, childIdentifier, tx, routeTableIdentifier);
222 if (child.getDataAfter().isPresent()) {
223 final NormalizedNode<?,?> dataAfter = child.getDataAfter().get();
224 LOG.trace("App peer -> AdjRibsIn path : {}", childIdentifier);
225 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
226 tx.put(LogicalDatastoreType.OPERATIONAL, childIdentifier, dataAfter);
236 public String getName() {
241 public synchronized void close() {
242 if (this.registration != null) {
243 this.registration.close();
244 this.registration = null;
246 if (this.effectiveRibInWriter != null) {
247 this.effectiveRibInWriter.close();
249 if (this.adjRibInWriter != null) {
250 this.adjRibInWriter.removePeer();
252 if (this.chain != null) {
256 if (this.writerChain != null) {
257 this.writerChain.close();
258 this.writerChain = null;
263 public byte[] getRawIdentifier() {
264 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
268 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
269 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
270 LOG.error("Transaction chain {} failed.", transaction != null ? transaction.getIdentifier() : null, cause);
274 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
275 LOG.debug("Transaction chain {} successful.", chain);