BUG-6747: Race condition on peer connection
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / spi / AbstractRouteEntry.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.mode.spi;
10
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
13 import org.opendaylight.protocol.bgp.mode.api.RouteEntry;
14 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
15 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup;
16 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
17 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibOut;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public abstract class AbstractRouteEntry implements RouteEntry {
33     protected static final NodeIdentifier ROUTES_IDENTIFIER = new NodeIdentifier(Routes.QNAME);
34     private static final Logger LOG = LoggerFactory.getLogger(AbstractRouteEntry.class);
35
36     protected final void fillLocRib(final YangInstanceIdentifier routeTarget, final NormalizedNode<?, ?> value, final DOMDataWriteTransaction tx) {
37         if (value != null) {
38             LOG.debug("Write route to LocRib {}", value);
39             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
40         } else {
41             LOG.debug("Delete route from LocRib {}", routeTarget);
42             tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);
43         }
44     }
45
46     protected final boolean writeRoute(final PeerId destPeer, final YangInstanceIdentifier routeTarget, final ContainerNode effAttrib,
47         final NormalizedNode<?, ?> value, final RIBSupport ribSup, final DOMDataWriteTransaction tx) {
48         if (effAttrib != null && value != null) {
49             LOG.debug("Write route {} to peer AdjRibsOut {}", value, destPeer);
50             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
51             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(ribSup.routeAttributesIdentifier()), effAttrib);
52             return true;
53         }
54         return false;
55     }
56
57     protected final boolean filterRoutes(final PeerId rootPeer, final PeerId destPeer, final ExportPolicyPeerTracker peerPT,        final TablesKey localTK, final PeerRole destPeerRole) {
58         return !rootPeer.equals(destPeer) && isTableSupported(destPeer, peerPT, localTK) && !PeerRole.Internal.equals(destPeerRole);
59     }
60
61     private boolean isTableSupported(final PeerId destPeer, final ExportPolicyPeerTracker peerPT, final TablesKey localTK) {
62         if (!peerPT.isTableSupported(destPeer)) {
63             LOG.trace("Route rejected, peer {} does not support this table type {}", destPeer, localTK);
64             return false;
65         }
66         return true;
67     }
68
69     protected final YangInstanceIdentifier getAdjRibOutYII(final RIBSupport ribSup, final YangInstanceIdentifier rootPath, final PathArgument routeId,
70         final TablesKey localTK) {
71         return ribSup.routePath(rootPath.node(AdjRibOut.QNAME).node(Tables.QNAME).node(RibSupportUtils.toYangTablesKey(localTK))
72             .node(ROUTES_IDENTIFIER), routeId);
73     }
74
75     protected PeerRole getRoutePeerIdRole(final ExportPolicyPeerTracker peerPT, final PeerId routePeerId) {
76         for (final PeerRole role : PeerRole.values()) {
77             final PeerExportGroup peerGroup = peerPT.getPeerGroup(role);
78             if(peerGroup != null && peerGroup.containsPeer(routePeerId)) {
79                 return role;
80             }
81         }
82         return null;
83     }
84 }