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 com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.Optional;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
25 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
30 import org.opendaylight.protocol.bgp.openconfig.spi.BGPConfigModuleTracker;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
33 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPPeerStats;
34 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPPeerStatsImpl;
35 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
36 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
37 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
38 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
52 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
54 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 * Application Peer is a special case of BGP peer. It serves as an interface
60 * for user to advertise user routes to ODL and through ODL to other BGP peers.
62 * This peer has it's own RIB, where it stores all user routes. This RIB is
63 * located in configurational datastore. Routes are added through RESTCONF.
65 * They are then processed as routes from any other peer, through AdjRib,
66 * EffectiveRib,LocRib and if they are advertised further, through AdjRibOut.
68 * For purposed of import policies such as Best Path Selection, application
69 * peer needs to have a BGP-ID that is configurable.
71 public class ApplicationPeer implements org.opendaylight.protocol.bgp.rib.spi.Peer, ClusteredDOMDataTreeChangeListener, TransactionChainListener {
73 private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
75 private final byte[] rawIdentifier;
76 private final String name;
77 private final YangInstanceIdentifier adjRibsInId;
78 private final Ipv4Address ipAddress;
79 private final BGPConfigModuleTracker moduleTracker;
80 private final RIB rib;
81 private final YangInstanceIdentifier peerIId;
82 private DOMTransactionChain chain;
83 private DOMTransactionChain writerChain;
84 private EffectiveRibInWriter effectiveRibInWriter;
85 private AdjRibInWriter adjRibInWriter;
86 private BGPPeerStats peerStats;
87 private ListenerRegistration<ApplicationPeer> registration;
88 private final Set<NodeIdentifierWithPredicates> supportedTables = new HashSet<>();
92 interface RegisterAppPeerListener {
94 * Register Application Peer Change Listener once AdjRibIn has been successfully initialized.
99 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIB rib,
100 final BGPConfigModuleTracker moduleTracker) {
101 this.name = applicationRibId.getValue();
102 final RIB targetRib = Preconditions.checkNotNull(rib);
103 this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
104 final NodeIdentifierWithPredicates peerId = IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress));
105 this.peerIId = targetRib.getYangRibId().node(Peer.QNAME).node(peerId);
106 this.adjRibsInId = this.peerIId.node(AdjRibIn.QNAME).node(Tables.QNAME);
107 this.rib = targetRib;
108 this.ipAddress = ipAddress;
109 this.moduleTracker = moduleTracker;
112 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address bgpPeerId, final RIB targetRibDependency) {
113 this(applicationRibId, bgpPeerId, targetRibDependency, null);
116 public synchronized void instantiateServiceInstance(final DOMDataTreeChangeService dataTreeChangeService,
117 final DOMDataTreeIdentifier appPeerDOMId) {
118 this.chain = this.rib.createPeerChain(this);
119 this.writerChain = this.rib.createPeerChain(this);
121 final Optional<SimpleRoutingPolicy> simpleRoutingPolicy = Optional.of(SimpleRoutingPolicy.AnnounceNone);
122 final PeerId peerId = RouterIds.createPeerId(this.ipAddress);
123 final Set<TablesKey> localTables = this.rib.getLocalTablesKeys();
124 localTables.forEach(tablesKey -> {
125 final ExportPolicyPeerTracker exportTracker = this.rib.getExportPolicyPeerTracker(tablesKey);
126 if (exportTracker != null) {
127 exportTracker.registerPeer(peerId, null, this.peerIId, PeerRole.Internal, simpleRoutingPolicy);
129 this.supportedTables.add(RibSupportUtils.toYangTablesKey(tablesKey));
132 this.adjRibInWriter = AdjRibInWriter.create(this.rib.getYangRibId(), PeerRole.Internal, simpleRoutingPolicy, this.writerChain);
133 final RIBSupportContextRegistry context = this.rib.getRibSupportContext();
134 final RegisterAppPeerListener registerAppPeerListener = () -> {
135 synchronized (this) {
136 if(this.chain != null) {
137 this.registration = dataTreeChangeService.registerDataTreeChangeListener(appPeerDOMId, this);
141 this.adjRibInWriter = this.adjRibInWriter.transform(peerId, context, localTables, Collections.emptyMap(),
142 registerAppPeerListener);
143 this.peerStats = new BGPPeerStatsImpl(this.name, localTables);
144 this.effectiveRibInWriter = EffectiveRibInWriter.create(this.rib.getService(), this.rib.createPeerChain(this), this.peerIId,
145 this.rib.getImportPolicyPeerTracker(), context, PeerRole.Internal, this.peerStats.getEffectiveRibInRouteCounters(),
146 this.peerStats.getAdjRibInRouteCounters());
147 if (moduleTracker != null) {
148 moduleTracker.onInstanceCreate();
153 * Routes come from application RIB that is identified by (configurable) name.
154 * Each route is pushed into AdjRibsInWriter with it's whole context. In this
155 * method, it doesn't matter if the routes are removed or added, this will
156 * be determined in LocRib.
159 public synchronized void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
160 if(this.chain == null) {
161 LOG.trace("Skipping data changed called to Application Peer. Change : {}", changes);
164 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
165 LOG.debug("Received data change to ApplicationRib {}", changes);
166 for (final DataTreeCandidate tc : changes) {
167 LOG.debug("Modification Type {}", tc.getRootNode().getModificationType());
168 final YangInstanceIdentifier path = tc.getRootPath();
169 final PathArgument lastArg = path.getLastPathArgument();
170 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), path);
171 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
172 if (!this.supportedTables.contains(tableKey)) {
173 LOG.trace("Skipping received data change for non supported family {}.", tableKey);
176 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
177 final PathArgument childIdentifier = child.getIdentifier();
178 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(childIdentifier);
179 switch (child.getModificationType()) {
181 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
182 tx.delete(LogicalDatastoreType.OPERATIONAL, tableId);
187 case SUBTREE_MODIFIED:
188 if (EffectiveRibInWriter.TABLE_ROUTES.equals(childIdentifier)) {
189 processRoutesTable(child, tableId, tx, tableId);
193 if (child.getDataAfter().isPresent()) {
194 final NormalizedNode<?,?> dataAfter = child.getDataAfter().get();
195 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
196 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
197 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, dataAfter);
209 * Applies modification under table routes based on modification type instead of only put. BUG 4438
213 * @param routeTableIdentifier
215 private synchronized void processRoutesTable(final DataTreeCandidateNode node, final YangInstanceIdentifier identifier,
216 final DOMDataWriteTransaction tx, final YangInstanceIdentifier routeTableIdentifier) {
217 for (final DataTreeCandidateNode child : node.getChildNodes()) {
218 final YangInstanceIdentifier childIdentifier = identifier.node(child.getIdentifier());
219 switch (child.getModificationType()) {
221 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
222 tx.delete(LogicalDatastoreType.OPERATIONAL, childIdentifier);
227 case SUBTREE_MODIFIED:
228 //For be ables to use DELETE when we remove specific routes as we do when we remove the whole routes,
229 // we need to go deeper three levels
230 if (!routeTableIdentifier.equals(childIdentifier.getParent().getParent().getParent())) {
231 processRoutesTable(child, childIdentifier, tx, routeTableIdentifier);
235 if (child.getDataAfter().isPresent()) {
236 final NormalizedNode<?,?> dataAfter = child.getDataAfter().get();
237 LOG.trace("App peer -> AdjRibsIn path : {}", childIdentifier);
238 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
239 tx.put(LogicalDatastoreType.OPERATIONAL, childIdentifier, dataAfter);
249 public String getName() {
253 // FIXME ListenableFuture<?> should be used once closeServiceInstance uses wildcard too
255 public synchronized ListenableFuture<Void> close() {
256 if (this.registration != null) {
257 this.registration.close();
258 this.registration = null;
260 if (this.effectiveRibInWriter != null) {
261 this.effectiveRibInWriter.close();
263 final ListenableFuture<Void> future;
264 if (this.adjRibInWriter != null) {
265 future = this.adjRibInWriter.removePeer();
267 future = Futures.immediateFuture(null);
269 if (this.chain != null) {
273 if (this.writerChain != null) {
274 this.writerChain.close();
275 this.writerChain = null;
277 if (this.moduleTracker != null) {
278 this.moduleTracker.onInstanceClose();
284 public byte[] getRawIdentifier() {
285 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
289 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
290 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
291 LOG.error("Transaction chain {} failed.", transaction != null ? transaction.getIdentifier() : null, cause);
295 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
296 LOG.debug("Transaction chain {} successful.", chain);