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 com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ADJRIBIN_NID;
13 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ADJRIBOUT_NID;
14 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.PEER_NID;
15 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ROUTES_NID;
16 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
18 import com.google.common.cache.CacheBuilder;
19 import com.google.common.cache.CacheLoader;
20 import com.google.common.cache.LoadingCache;
21 import com.google.common.util.concurrent.FluentFuture;
22 import com.google.common.util.concurrent.FutureCallback;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
29 import org.opendaylight.mdsal.common.api.CommitInfo;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
36 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
37 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
40 import org.opendaylight.protocol.bgp.rib.impl.state.BGPSessionStateImpl;
41 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
42 import org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers;
43 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
44 import org.opendaylight.protocol.bgp.rib.spi.state.BGPSessionState;
45 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTimersState;
46 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTransportState;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteTarget;
54 import org.opendaylight.yangtools.concepts.ListenerRegistration;
55 import org.opendaylight.yangtools.concepts.Registration;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
59 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
60 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 * Application Peer is a special case of BGP peer. It serves as an interface
66 * for user to advertise user routes to ODL and through ODL to other BGP peers.
69 * This peer has it's own RIB, where it stores all user routes. This RIB is
70 * 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.
77 * For purposed of import policies such as Best Path Selection, application
78 * peer needs to have a BGP-ID that is configurable.
80 public class ApplicationPeer extends AbstractPeer implements ClusteredDOMDataTreeChangeListener {
81 private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
82 private static final String APP_PEER_GROUP = "application-peers";
84 private final LoadingCache<NodeIdentifierWithPredicates, YangInstanceIdentifier> tablesIId =
85 CacheBuilder.newBuilder().build(new CacheLoader<NodeIdentifierWithPredicates, YangInstanceIdentifier>() {
87 public YangInstanceIdentifier load(final NodeIdentifierWithPredicates key) {
88 return peerRibOutIId.node(RIBNodeIdentifiers.TABLES_NID).node(key);
92 private final YangInstanceIdentifier adjRibsInId;
93 private final YangInstanceIdentifier peerRibOutIId;
94 private final BGPTableTypeRegistryConsumer tableTypeRegistry;
95 private EffectiveRibInWriter effectiveRibInWriter;
96 private AdjRibInWriter adjRibInWriter;
97 private ListenerRegistration<ApplicationPeer> registration;
98 private final Set<NodeIdentifierWithPredicates> supportedTables = new HashSet<>();
99 private final BGPSessionStateImpl bgpSessionState = new BGPSessionStateImpl();
100 private Registration trackerRegistration;
101 private YangInstanceIdentifier peerPath;
104 public List<RouteTarget> getMemberships() {
109 interface RegisterAppPeerListener {
111 * Register Application Peer Change Listener once AdjRibIn has been successfully initialized.
116 public ApplicationPeer(
117 final BGPTableTypeRegistryConsumer tableTypeRegistry,
118 final ApplicationRibId applicationRibId, final Ipv4AddressNoZone ipAddress, final RIB rib) {
119 super(rib, applicationRibId.getValue(), APP_PEER_GROUP, PeerRole.Internal,
120 new IpAddressNoZone(ipAddress), Set.of());
121 this.tableTypeRegistry = requireNonNull(tableTypeRegistry);
122 final RIB targetRib = requireNonNull(rib);
123 peerId = RouterIds.createPeerId(ipAddress);
125 final YangInstanceIdentifier peerRib = targetRib.getYangRibId().node(PEER_NID)
126 .node(IdentifierUtils.domPeerId(peerId));
127 adjRibsInId = peerRib.node(ADJRIBIN_NID).node(TABLES_NID).toOptimized();
128 peerRibOutIId = peerRib.node(ADJRIBOUT_NID).node(TABLES_NID).toOptimized();
131 public synchronized void instantiateServiceInstance(final DOMDataTreeChangeService dataTreeChangeService,
132 final DOMDataTreeIdentifier appPeerDOMId) {
134 final Set<TablesKey> localTables = rib.getLocalTablesKeys();
135 localTables.forEach(tablesKey -> supportedTables.add(RibSupportUtils.toYangTablesKey(tablesKey)));
136 setAdvertizedGracefulRestartTableTypes(List.of());
139 adjRibInWriter = AdjRibInWriter.create(rib.getYangRibId(), PeerRole.Internal, this);
140 final RIBSupportContextRegistry context = rib.getRibSupportContext();
141 final RegisterAppPeerListener registerAppPeerListener = () -> {
142 synchronized (this) {
143 if (getDomChain() != null) {
144 registration = dataTreeChangeService.registerDataTreeChangeListener(appPeerDOMId, this);
148 peerPath = createPeerPath(peerId);
149 adjRibInWriter = adjRibInWriter.transform(peerId, peerPath, context, localTables,
150 Map.of(), registerAppPeerListener);
151 effectiveRibInWriter = new EffectiveRibInWriter(this, rib,
152 rib.createPeerDOMChain(this), peerPath, localTables, tableTypeRegistry,
153 new ArrayList<>(), rtCache);
154 effectiveRibInWriter.init();
155 bgpSessionState.registerMessagesCounter(this);
156 trackerRegistration = rib.getPeerTracker().registerPeer(this);
160 public void onInitialData() {
161 // FIXME: we really want to (under a synchronized block) to ensure adj-rib-in is completely empty here.
162 // Unfortunately that bit is already being done somewhere else. The entire the tables are being created
163 // elsewhere and therefore we need to reign in lifecycle first
167 * Routes come from application RIB that is identified by (configurable) name.
168 * Each route is pushed into AdjRibsInWriter with it's whole context. In this
169 * method, it doesn't matter if the routes are removed or added, this will
170 * be determined in LocRib.
173 public synchronized void onDataTreeChanged(final List<DataTreeCandidate> changes) {
174 final DOMTransactionChain chain = getDomChain();
176 LOG.trace("Skipping data changed called to Application Peer. Change : {}", changes);
179 final DOMDataTreeWriteTransaction tx = chain.newWriteOnlyTransaction();
180 LOG.debug("Received data change to ApplicationRib {}", changes);
181 for (final DataTreeCandidate tc : changes) {
182 LOG.debug("Modification Type {}", tc.getRootNode().getModificationType());
183 final YangInstanceIdentifier path = tc.getRootPath();
184 final PathArgument lastArg = path.getLastPathArgument();
185 verify(lastArg instanceof NodeIdentifierWithPredicates,
186 "Unexpected type %s in path %s", lastArg.getClass(), path);
187 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
188 if (!supportedTables.contains(tableKey)) {
189 LOG.trace("Skipping received data change for non supported family {}.", tableKey);
192 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
193 final PathArgument childIdentifier = child.getIdentifier();
194 final YangInstanceIdentifier tableId = adjRibsInId.node(tableKey).node(childIdentifier);
195 switch (child.getModificationType()) {
198 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
199 tx.delete(LogicalDatastoreType.OPERATIONAL, tableId);
204 case SUBTREE_MODIFIED:
205 if (ROUTES_NID.equals(childIdentifier)) {
206 processRoutesTable(child, tableId, tx, tableId);
208 processWrite(child, tableId, tx);
213 processWrite(child, tableId, tx);
220 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
222 public void onSuccess(final CommitInfo result) {
223 LOG.trace("Successful commit");
227 public void onFailure(final Throwable trw) {
228 LOG.error("Failed commit", trw);
230 }, MoreExecutors.directExecutor());
233 private static void processWrite(final DataTreeCandidateNode child, final YangInstanceIdentifier tableId,
234 final DOMDataTreeWriteTransaction tx) {
235 child.getDataAfter().ifPresent(dataAfter -> {
236 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
237 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
238 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, dataAfter);
242 private synchronized void processRoutesTable(final DataTreeCandidateNode node,
243 final YangInstanceIdentifier identifier, final DOMDataTreeWriteTransaction tx,
244 final YangInstanceIdentifier routeTableIdentifier) {
245 for (final DataTreeCandidateNode child : node.getChildNodes()) {
246 final YangInstanceIdentifier childIdentifier = identifier.node(child.getIdentifier());
247 switch (child.getModificationType()) {
249 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
250 tx.delete(LogicalDatastoreType.OPERATIONAL, childIdentifier);
255 case SUBTREE_MODIFIED:
256 // For be ables to use DELETE when we remove specific routes as we do when we remove the whole
257 // routes, we need to go deeper three levels
258 if (!routeTableIdentifier.equals(childIdentifier.getParent().getParent().getParent())) {
259 processRoutesTable(child, childIdentifier, tx, routeTableIdentifier);
261 processRouteWrite(child, childIdentifier, tx);
265 processRouteWrite(child, childIdentifier, tx);
273 private static void processRouteWrite(final DataTreeCandidateNode child,
274 final YangInstanceIdentifier childIdentifier, final DOMDataTreeWriteTransaction tx) {
275 child.getDataAfter().ifPresent(dataAfter -> {
276 LOG.trace("App peer -> AdjRibsIn path : {}", childIdentifier);
277 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
278 tx.put(LogicalDatastoreType.OPERATIONAL, childIdentifier, dataAfter);
283 public synchronized FluentFuture<? extends CommitInfo> close() {
285 if (registration != null) {
286 registration.close();
289 if (adjRibInWriter != null) {
290 adjRibInWriter.releaseChain();
292 if (effectiveRibInWriter != null) {
293 effectiveRibInWriter.close();
295 final FluentFuture<? extends CommitInfo> future = removePeer(peerPath);
297 if (trackerRegistration != null) {
298 trackerRegistration.close();
299 trackerRegistration = null;
305 public boolean supportsAddPathSupported(final TablesKey tableKey) {
310 public SendReceive getSupportedAddPathTables(final TablesKey tableKey) {
315 public boolean supportsTable(final TablesKey tableKey) {
316 return rib.supportsTable(tableKey);
320 public YangInstanceIdentifier getRibOutIId(final NodeIdentifierWithPredicates tablekey) {
321 return tablesIId.getUnchecked(tablekey);
325 public void onTransactionChainFailed(final DOMTransactionChain chain, final DOMDataTreeTransaction transaction,
326 final Throwable cause) {
327 LOG.error("Transaction chain {} failed.", transaction != null ? transaction.getIdentifier() : null, cause);
331 public BGPSessionState getBGPSessionState() {
332 return bgpSessionState;
336 public BGPTimersState getBGPTimersState() {
337 return bgpSessionState;
341 public BGPTransportState getBGPTransportState() {
342 return bgpSessionState;