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 static java.util.Objects.requireNonNull;
12 import com.google.common.base.Verify;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache;
16 import com.google.common.net.InetAddresses;
17 import com.google.common.util.concurrent.FluentFuture;
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
27 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
31 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
32 import org.opendaylight.mdsal.common.api.CommitInfo;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
34 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
35 import org.opendaylight.protocol.bgp.rib.impl.state.BGPSessionStateImpl;
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.protocol.bgp.rib.spi.state.BGPSessionState;
40 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTimersState;
41 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTransportState;
42 import org.opendaylight.protocol.concepts.AbstractRegistration;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.PeerKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibIn;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibOut;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
54 import org.opendaylight.yangtools.concepts.ListenerRegistration;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
62 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
67 * Application Peer is a special case of BGP peer. It serves as an interface
68 * for user to advertise user routes to ODL and through ODL to other BGP peers.
70 * This peer has it's own RIB, where it stores all user routes. This RIB is
71 * located in configurational datastore. Routes are added through RESTCONF.
73 * They are then processed as routes from any other peer, through AdjRib,
74 * EffectiveRib,LocRib and if they are advertised further, through AdjRibOut.
76 * For purposed of import policies such as Best Path Selection, application
77 * peer needs to have a BGP-ID that is configurable.
79 public class ApplicationPeer extends AbstractPeer implements ClusteredDOMDataTreeChangeListener {
81 private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
83 private static final String APP_PEER_GROUP = "application-peers";
84 private final YangInstanceIdentifier adjRibsInId;
85 private final InstanceIdentifier<AdjRibOut> peerRibOutIId;
86 private final KeyedInstanceIdentifier<Peer, PeerKey> peerIId;
87 private DOMTransactionChain chain;
88 private DOMTransactionChain writerChain;
89 private EffectiveRibInWriter effectiveRibInWriter;
90 private AdjRibInWriter adjRibInWriter;
91 private ListenerRegistration<ApplicationPeer> registration;
92 private final Set<NodeIdentifierWithPredicates> supportedTables = new HashSet<>();
93 private final BGPSessionStateImpl bgpSessionState = new BGPSessionStateImpl();
94 private final LoadingCache<TablesKey, KeyedInstanceIdentifier<Tables, TablesKey>> tablesIId
95 = CacheBuilder.newBuilder()
96 .build(new CacheLoader<TablesKey, KeyedInstanceIdentifier<Tables, TablesKey>>() {
98 public KeyedInstanceIdentifier<Tables, TablesKey> load(final TablesKey tablesKey) {
99 return ApplicationPeer.this.peerRibOutIId.child(Tables.class, tablesKey);
102 private AbstractRegistration trackerRegistration;
103 private YangInstanceIdentifier peerPath;
106 interface RegisterAppPeerListener {
108 * Register Application Peer Change Listener once AdjRibIn has been successfully initialized.
113 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIB rib) {
114 super(rib, applicationRibId.getValue(), APP_PEER_GROUP, PeerRole.Internal,
115 new IpAddress(ipAddress), Collections.emptySet());
116 final RIB targetRib = requireNonNull(rib);
117 this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
118 this.adjRibsInId = targetRib.getYangRibId().node(Peer.QNAME)
119 .node(IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress)))
120 .node(AdjRibIn.QNAME).node(Tables.QNAME);
121 this.peerId = RouterIds.createPeerId(ipAddress);
122 this.peerIId = getInstanceIdentifier().child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
123 .yang.bgp.rib.rev180329.bgp.rib.rib.Peer.class, new PeerKey(this.peerId));
124 this.peerRibOutIId = this.peerIId.child(AdjRibOut.class);
127 public synchronized void instantiateServiceInstance(final DOMDataTreeChangeService dataTreeChangeService,
128 final DOMDataTreeIdentifier appPeerDOMId) {
130 this.chain = this.rib.createPeerDOMChain(this);
131 this.writerChain = this.rib.createPeerDOMChain(this);
133 final Set<TablesKey> localTables = this.rib.getLocalTablesKeys();
134 localTables.forEach(tablesKey -> this.supportedTables.add(RibSupportUtils.toYangTablesKey(tablesKey)));
135 setAdvertizedGracefulRestartTableTypes(Collections.emptyList());
137 this.adjRibInWriter = AdjRibInWriter.create(this.rib.getYangRibId(), PeerRole.Internal, this.writerChain);
138 final RIBSupportContextRegistry context = this.rib.getRibSupportContext();
139 final RegisterAppPeerListener registerAppPeerListener = () -> {
140 synchronized (this) {
141 if (this.chain != null) {
142 this.registration = dataTreeChangeService.registerDataTreeChangeListener(appPeerDOMId, this);
146 this.peerPath = createPeerPath();
147 this.adjRibInWriter = this.adjRibInWriter.transform(this.peerId, this.peerPath, context, localTables,
148 Collections.emptyMap(), registerAppPeerListener);
149 this.effectiveRibInWriter = new EffectiveRibInWriter(this, this.rib,
150 this.rib.createPeerChain(this), this.peerIId, localTables);
151 this.effectiveRibInWriter.init();
152 this.bgpSessionState.registerMessagesCounter(this);
153 this.trackerRegistration = this.rib.getPeerTracker().registerPeer(this);
157 * Routes come from application RIB that is identified by (configurable) name.
158 * Each route is pushed into AdjRibsInWriter with it's whole context. In this
159 * method, it doesn't matter if the routes are removed or added, this will
160 * be determined in LocRib.
163 public synchronized void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
164 if (this.chain == null) {
165 LOG.trace("Skipping data changed called to Application Peer. Change : {}", changes);
168 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
169 LOG.debug("Received data change to ApplicationRib {}", changes);
170 for (final DataTreeCandidate tc : changes) {
171 LOG.debug("Modification Type {}", tc.getRootNode().getModificationType());
172 final YangInstanceIdentifier path = tc.getRootPath();
173 final PathArgument lastArg = path.getLastPathArgument();
174 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates,
175 "Unexpected type %s in path %s", lastArg.getClass(), path);
176 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
177 if (!this.supportedTables.contains(tableKey)) {
178 LOG.trace("Skipping received data change for non supported family {}.", tableKey);
181 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
182 final PathArgument childIdentifier = child.getIdentifier();
183 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(childIdentifier);
184 switch (child.getModificationType()) {
186 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
187 tx.delete(LogicalDatastoreType.OPERATIONAL, tableId);
192 case SUBTREE_MODIFIED:
193 if (EffectiveRibInWriter.TABLE_ROUTES.equals(childIdentifier)) {
194 processRoutesTable(child, tableId, tx, tableId);
198 if (child.getDataAfter().isPresent()) {
199 final NormalizedNode<?, ?> dataAfter = child.getDataAfter().get();
200 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
201 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
202 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, dataAfter);
210 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
212 public void onSuccess(final CommitInfo result) {
213 LOG.trace("Successful commit");
217 public void onFailure(final Throwable trw) {
218 LOG.error("Failed commit", trw);
220 }, MoreExecutors.directExecutor());
223 private synchronized void processRoutesTable(final DataTreeCandidateNode node,
224 final YangInstanceIdentifier identifier, final DOMDataWriteTransaction tx,
225 final YangInstanceIdentifier routeTableIdentifier) {
226 for (final DataTreeCandidateNode child : node.getChildNodes()) {
227 final YangInstanceIdentifier childIdentifier = identifier.node(child.getIdentifier());
228 switch (child.getModificationType()) {
230 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
231 tx.delete(LogicalDatastoreType.OPERATIONAL, childIdentifier);
236 case SUBTREE_MODIFIED:
237 //For be ables to use DELETE when we remove specific routes as we do when we remove the whole routes,
238 // we need to go deeper three levels
239 if (!routeTableIdentifier.equals(childIdentifier.getParent().getParent().getParent())) {
240 processRoutesTable(child, childIdentifier, tx, routeTableIdentifier);
244 if (child.getDataAfter().isPresent()) {
245 final NormalizedNode<?, ?> dataAfter = child.getDataAfter().get();
246 LOG.trace("App peer -> AdjRibsIn path : {}", childIdentifier);
247 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
248 tx.put(LogicalDatastoreType.OPERATIONAL, childIdentifier, dataAfter);
258 public synchronized FluentFuture<? extends CommitInfo> close() {
260 if (this.registration != null) {
261 this.registration.close();
262 this.registration = null;
264 if (this.effectiveRibInWriter != null) {
265 this.effectiveRibInWriter.close();
267 final FluentFuture<? extends CommitInfo> future;
268 if (this.chain != null) {
269 future = removePeer(this.chain, this.peerPath);
273 future = CommitInfo.emptyFluentFuture();
275 if (this.writerChain != null) {
276 this.writerChain.close();
277 this.writerChain = null;
279 if (this.trackerRegistration != null) {
280 this.trackerRegistration.close();
281 this.trackerRegistration = null;
287 public boolean supportsAddPathSupported(final TablesKey tableKey) {
292 public SendReceive getSupportedAddPathTables(final TablesKey tableKey) {
297 public boolean supportsTable(final TablesKey tableKey) {
298 return this.rib.supportsTable(tableKey);
302 public KeyedInstanceIdentifier<Tables, TablesKey> getRibOutIId(final TablesKey tablesKey) {
303 return this.tablesIId.getUnchecked(tablesKey);
307 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
308 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
309 LOG.error("Transaction chain {} failed.", transaction != null ? transaction.getIdentifier() : null, cause);
313 public BGPSessionState getBGPSessionState() {
314 return this.bgpSessionState;
318 public BGPTimersState getBGPTimersState() {
319 return this.bgpSessionState;
323 public BGPTransportState getBGPTransportState() {
324 return this.bgpSessionState;