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.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.util.Arrays;
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.common.api.data.TransactionChainListener;
28 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
31 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
32 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
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.BGPPeerStateImpl;
36 import org.opendaylight.protocol.bgp.rib.impl.state.BGPSessionStateImpl;
37 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
38 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
39 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
40 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
41 import org.opendaylight.protocol.bgp.rib.spi.state.BGPAfiSafiState;
42 import org.opendaylight.protocol.bgp.rib.spi.state.BGPErrorHandlingState;
43 import org.opendaylight.protocol.bgp.rib.spi.state.BGPSessionState;
44 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTimersState;
45 import org.opendaylight.protocol.bgp.rib.spi.state.BGPTransportState;
46 import org.opendaylight.protocol.concepts.AbstractRegistration;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.SendReceive;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.PeerKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibIn;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibOut;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
61 import org.opendaylight.yangtools.concepts.ListenerRegistration;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
67 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
69 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
74 * Application Peer is a special case of BGP peer. It serves as an interface
75 * for user to advertise user routes to ODL and through ODL to other BGP peers.
77 * This peer has it's own RIB, where it stores all user routes. This RIB is
78 * located in configurational datastore. Routes are added through RESTCONF.
80 * They are then processed as routes from any other peer, through AdjRib,
81 * EffectiveRib,LocRib and if they are advertised further, through AdjRibOut.
83 * For purposed of import policies such as Best Path Selection, application
84 * peer needs to have a BGP-ID that is configurable.
86 public class ApplicationPeer extends BGPPeerStateImpl implements org.opendaylight.protocol.bgp.rib.spi.Peer,
87 BGPRouteEntryImportParameters, ClusteredDOMDataTreeChangeListener, TransactionChainListener {
89 private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
91 private final byte[] rawIdentifier;
92 private final String name;
93 private final YangInstanceIdentifier adjRibsInId;
94 private final RIB rib;
95 private final InstanceIdentifier<AdjRibOut> peerRibOutIId;
96 private final KeyedInstanceIdentifier<Peer, PeerKey> peerIId;
97 private DOMTransactionChain chain;
98 private DOMTransactionChain writerChain;
99 private EffectiveRibInWriter effectiveRibInWriter;
100 private AdjRibInWriter adjRibInWriter;
101 private ListenerRegistration<ApplicationPeer> registration;
102 private final Set<NodeIdentifierWithPredicates> supportedTables = new HashSet<>();
103 private final BGPSessionStateImpl bgpSessionState = new BGPSessionStateImpl();
104 private final LoadingCache<TablesKey, KeyedInstanceIdentifier<Tables, TablesKey>> tablesIId
105 = CacheBuilder.newBuilder()
106 .build(new CacheLoader<TablesKey, KeyedInstanceIdentifier<Tables, TablesKey>>() {
108 public KeyedInstanceIdentifier<Tables, TablesKey> load(final TablesKey tablesKey) {
109 return ApplicationPeer.this.peerRibOutIId.child(Tables.class, tablesKey);
112 private final PeerId peerId;
113 private AbstractRegistration trackerRegistration;
116 interface RegisterAppPeerListener {
118 * Register Application Peer Change Listener once AdjRibIn has been successfully initialized.
123 public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIB rib) {
124 super(rib.getInstanceIdentifier(), "application-peers", new IpAddress(ipAddress),
125 rib.getLocalTablesKeys(), Collections.emptySet());
126 this.name = applicationRibId.getValue();
127 final RIB targetRib = requireNonNull(rib);
128 this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
129 final NodeIdentifierWithPredicates peerIId = IdentifierUtils.domPeerId(RouterIds.createPeerId(ipAddress));
130 this.adjRibsInId = targetRib.getYangRibId().node(Peer.QNAME).node(peerIId)
131 .node(AdjRibIn.QNAME).node(Tables.QNAME);
132 this.rib = targetRib;
133 this.peerId = RouterIds.createPeerId(ipAddress);
134 this.peerIId = getInstanceIdentifier().child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
135 .yang.bgp.rib.rev180329.bgp.rib.rib.Peer.class, new PeerKey(this.peerId));
136 this.peerRibOutIId = this.peerIId.child(AdjRibOut.class);
139 public synchronized void instantiateServiceInstance(final DOMDataTreeChangeService dataTreeChangeService,
140 final DOMDataTreeIdentifier appPeerDOMId) {
142 this.chain = this.rib.createPeerDOMChain(this);
143 this.writerChain = this.rib.createPeerDOMChain(this);
145 final Set<TablesKey> localTables = this.rib.getLocalTablesKeys();
146 localTables.forEach(tablesKey -> {
147 this.supportedTables.add(RibSupportUtils.toYangTablesKey(tablesKey));
149 setAdvertizedGracefulRestartTableTypes(Collections.emptyList());
151 this.adjRibInWriter = AdjRibInWriter.create(this.rib.getYangRibId(), PeerRole.Internal, this.writerChain);
152 final RIBSupportContextRegistry context = this.rib.getRibSupportContext();
153 final RegisterAppPeerListener registerAppPeerListener = () -> {
154 synchronized (this) {
155 if (this.chain != null) {
156 this.registration = dataTreeChangeService.registerDataTreeChangeListener(appPeerDOMId, this);
160 this.adjRibInWriter = this.adjRibInWriter.transform(this.peerId, context, localTables, Collections.emptyMap(),
161 registerAppPeerListener);
162 this.effectiveRibInWriter = EffectiveRibInWriter
163 .create(this, this.rib, this.rib.createPeerChain(this), this.peerIId, localTables);
164 this.bgpSessionState.registerMessagesCounter(this);
165 this.trackerRegistration = this.rib.getPeerTracker().registerPeer(this);
169 * Routes come from application RIB that is identified by (configurable) name.
170 * Each route is pushed into AdjRibsInWriter with it's whole context. In this
171 * method, it doesn't matter if the routes are removed or added, this will
172 * be determined in LocRib.
175 public synchronized void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
176 if (this.chain == null) {
177 LOG.trace("Skipping data changed called to Application Peer. Change : {}", changes);
180 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
181 LOG.debug("Received data change to ApplicationRib {}", changes);
182 for (final DataTreeCandidate tc : changes) {
183 LOG.debug("Modification Type {}", tc.getRootNode().getModificationType());
184 final YangInstanceIdentifier path = tc.getRootPath();
185 final PathArgument lastArg = path.getLastPathArgument();
186 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates,
187 "Unexpected type %s in path %s", lastArg.getClass(), path);
188 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
189 if (!this.supportedTables.contains(tableKey)) {
190 LOG.trace("Skipping received data change for non supported family {}.", tableKey);
193 for (final DataTreeCandidateNode child : tc.getRootNode().getChildNodes()) {
194 final PathArgument childIdentifier = child.getIdentifier();
195 final YangInstanceIdentifier tableId = this.adjRibsInId.node(tableKey).node(childIdentifier);
196 switch (child.getModificationType()) {
198 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
199 tx.delete(LogicalDatastoreType.OPERATIONAL, tableId);
204 case SUBTREE_MODIFIED:
205 if (EffectiveRibInWriter.TABLE_ROUTES.equals(childIdentifier)) {
206 processRoutesTable(child, tableId, tx, tableId);
210 if (child.getDataAfter().isPresent()) {
211 final NormalizedNode<?, ?> dataAfter = child.getDataAfter().get();
212 LOG.trace("App peer -> AdjRibsIn path : {}", tableId);
213 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
214 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, dataAfter);
225 private synchronized void processRoutesTable(final DataTreeCandidateNode node,
226 final YangInstanceIdentifier identifier, final DOMDataWriteTransaction tx,
227 final YangInstanceIdentifier routeTableIdentifier) {
228 for (final DataTreeCandidateNode child : node.getChildNodes()) {
229 final YangInstanceIdentifier childIdentifier = identifier.node(child.getIdentifier());
230 switch (child.getModificationType()) {
232 LOG.trace("App peer -> AdjRibsIn path delete: {}", childIdentifier);
233 tx.delete(LogicalDatastoreType.OPERATIONAL, childIdentifier);
238 case SUBTREE_MODIFIED:
239 //For be ables to use DELETE when we remove specific routes as we do when we remove the whole routes,
240 // we need to go deeper three levels
241 if (!routeTableIdentifier.equals(childIdentifier.getParent().getParent().getParent())) {
242 processRoutesTable(child, childIdentifier, tx, routeTableIdentifier);
246 if (child.getDataAfter().isPresent()) {
247 final NormalizedNode<?, ?> dataAfter = child.getDataAfter().get();
248 LOG.trace("App peer -> AdjRibsIn path : {}", childIdentifier);
249 LOG.trace("App peer -> AdjRibsIn data : {}", dataAfter);
250 tx.put(LogicalDatastoreType.OPERATIONAL, childIdentifier, dataAfter);
260 public String getName() {
264 // FIXME ListenableFuture<?> should be used once closeServiceInstance uses wildcard too
266 public synchronized ListenableFuture<Void> close() {
268 if (this.registration != null) {
269 this.registration.close();
270 this.registration = null;
272 if (this.effectiveRibInWriter != null) {
273 this.effectiveRibInWriter.close();
275 final ListenableFuture<Void> future;
276 if (this.adjRibInWriter != null) {
277 future = this.adjRibInWriter.removePeer();
279 future = Futures.immediateFuture(null);
281 if (this.chain != null) {
285 if (this.writerChain != null) {
286 this.writerChain.close();
287 this.writerChain = null;
289 if (this.trackerRegistration != null) {
290 this.trackerRegistration.close();
291 this.trackerRegistration = null;
297 public byte[] getRawIdentifier() {
298 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
302 public PeerId getPeerId() {
307 public boolean supportsAddPathSupported(final TablesKey tableKey) {
312 public SendReceive getSupportedAddPathTables(final TablesKey tableKey) {
317 public boolean supportsTable(final TablesKey tableKey) {
318 return this.rib.supportsTable(tableKey);
322 public PeerRole getRole() {
323 return PeerRole.Internal;
327 public ClusterIdentifier getClusterId() {
332 public AsNumber getLocalAs() {
337 public KeyedInstanceIdentifier<Tables, TablesKey> getRibOutIId(final TablesKey tablesKey) {
338 return this.tablesIId.getUnchecked(tablesKey);
342 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
343 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
344 LOG.error("Transaction chain {} failed.", transaction != null ? transaction.getIdentifier() : null, cause);
348 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
349 LOG.debug("Transaction chain {} successful.", chain);
353 public BGPErrorHandlingState getBGPErrorHandlingState() {
358 public BGPAfiSafiState getBGPAfiSafiState() {
363 public BGPSessionState getBGPSessionState() {
364 return this.bgpSessionState;
368 public BGPTimersState getBGPTimersState() {
369 return this.bgpSessionState;
373 public BGPTransportState getBGPTransportState() {
374 return this.bgpSessionState;
379 public PeerRole getFromPeerRole() {
384 public PeerId getFromPeerId() {
389 public ClusterIdentifier getFromClusterId() {
390 return getClusterId();
394 public AsNumber getFromPeerLocalAs() {