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.rib.impl.spi.RIB;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
27 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
28 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
29 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Application Peer is a special case of BGP peer. It serves as an interface
50 * for user to advertise user routes to ODL and through ODL to other BGP peers.
52 * This peer has it's own RIB, where it stores all user routes. This RIB is
53 * located in configurational datastore. Routes are added through RESTCONF.
55 * They are then processed as routes from any other peer, through AdjRib,
56 * EffectiveRib,LocRib and if they are advertised further, through AdjRibOut.
58 * For purposed of import policies such as Best Path Selection, application
59 * peer needs to have a BGP-ID that is configurable.
61 public class ApplicationPeer implements AutoCloseable, org.opendaylight.protocol.bgp.rib.spi.Peer, ClusteredDOMDataTreeChangeListener, TransactionChainListener {
63 private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
65 private final byte[] rawIdentifier;
66 private final String name;
67 private final YangInstanceIdentifier adjRibsInId;
68 private final Ipv4Address ipAddress;
69 private final RIB rib;
70 private final YangInstanceIdentifier peerIId;
71 private DOMTransactionChain chain;
72 private DOMTransactionChain writerChain;
73 private EffectiveRibInWriter effectiveRibInWriter;
74 private AdjRibInWriter adjRibInWriter;
76 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIB rib) {
77 this.name = applicationRibId.getValue();
78 final RIB targetRib = Preconditions.checkNotNull(rib);
79 this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
80 final NodeIdentifierWithPredicates peerId = IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress));
81 this.peerIId = targetRib.getYangRibId().node(Peer.QNAME).node(peerId);
82 this.adjRibsInId = this.peerIId.node(AdjRibIn.QNAME).node(Tables.QNAME);
84 this.ipAddress = ipAddress;
87 public void instantiateServiceInstance() {
88 this.chain = this.rib.createPeerChain(this);
89 this.writerChain = this.rib.createPeerChain(this);
91 final Optional<SimpleRoutingPolicy> simpleRoutingPolicy = Optional.of(SimpleRoutingPolicy.AnnounceNone);
92 final PeerId peerId = RouterIds.createPeerId(this.ipAddress);
93 final Set<TablesKey> localTables = this.rib.getLocalTablesKeys();
94 localTables.forEach(tablesKey -> {
95 final ExportPolicyPeerTracker exportTracker = this.rib.getExportPolicyPeerTracker(tablesKey);
96 if (exportTracker != null) {
97 exportTracker.registerPeer(peerId, null, this.peerIId, PeerRole.Internal, simpleRoutingPolicy);
101 this.adjRibInWriter = AdjRibInWriter.create(this.rib.getYangRibId(), PeerRole.Internal, simpleRoutingPolicy, this.writerChain);
102 final RIBSupportContextRegistry context = this.rib.getRibSupportContext();
103 this.adjRibInWriter = this.adjRibInWriter.transform(peerId, context, localTables, Collections.emptyMap());
104 //TODO need to create effective rib in adjRibInWriter with route counter here
105 this.effectiveRibInWriter = EffectiveRibInWriter.create(this.rib.getService(), this.rib.createPeerChain(this), this.peerIId,
106 this.rib.getImportPolicyPeerTracker(), context, PeerRole.Internal);
110 * Routes come from application RIB that is identified by (configurable) name.
111 * Each route is pushed into AdjRibsInWriter with it's whole context. In this
112 * method, it doesn't matter if the routes are removed or added, this will
113 * be determined in LocRib.
116 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
117 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
118 LOG.debug("Received data change to ApplicationRib {}", changes);
119 for (final DataTreeCandidate tc : changes) {
120 LOG.debug("Modification Type {}", tc.getRootNode().getModificationType());
121 final YangInstanceIdentifier path = tc.getRootPath();
122 final PathArgument lastArg = path.getLastPathArgument();
123 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), path);
124 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
125 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
126 final PathArgument childIdentifier = child.getIdentifier();
127 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(childIdentifier);
128 switch (child.getModificationType()) {
130 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
131 tx.delete(LogicalDatastoreType.OPERATIONAL, tableId);
136 case SUBTREE_MODIFIED:
137 if (EffectiveRibInWriter.TABLE_ROUTES.equals(childIdentifier)) {
138 processRoutesTable(child, tableId, tx, tableId);
142 if (child.getDataAfter().isPresent()) {
143 final NormalizedNode<?,?> dataAfter = child.getDataAfter().get();
144 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
145 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
146 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, dataAfter);
158 * Applies modification under table routes based on modification type instead of only put. BUG 4438
162 * @param routeTableIdentifier
164 private void processRoutesTable(final DataTreeCandidateNode node, final YangInstanceIdentifier identifier,
165 final DOMDataWriteTransaction tx, final YangInstanceIdentifier routeTableIdentifier) {
166 for (final DataTreeCandidateNode child : node.getChildNodes()) {
167 final YangInstanceIdentifier childIdentifier = identifier.node(child.getIdentifier());
168 switch (child.getModificationType()) {
170 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
171 tx.delete(LogicalDatastoreType.OPERATIONAL, childIdentifier);
176 case SUBTREE_MODIFIED:
177 //For be ables to use DELETE when we remove specific routes as we do when we remove the whole routes,
178 // we need to go deeper three levels
179 if (!routeTableIdentifier.equals(childIdentifier.getParent().getParent().getParent())) {
180 processRoutesTable(child, childIdentifier, tx, routeTableIdentifier);
184 if (child.getDataAfter().isPresent()) {
185 final NormalizedNode<?,?> dataAfter = child.getDataAfter().get();
186 LOG.trace("App peer -> AdjRibsIn path : {}", childIdentifier);
187 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
188 tx.put(LogicalDatastoreType.OPERATIONAL, childIdentifier, dataAfter);
198 public String getName() {
203 public void close() {
204 if(this.effectiveRibInWriter != null) {
205 this.effectiveRibInWriter.close();
207 if(this.adjRibInWriter != null) {
208 this.adjRibInWriter.removePeer();
210 if(this.chain != null) {
213 if(this.writerChain != null) {
214 this.writerChain.close();
219 public byte[] getRawIdentifier() {
220 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
224 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
225 final Throwable cause) {
226 LOG.error("Transaction chain failed.", cause);
230 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
231 LOG.debug("Transaction chain {} successfull.", chain);