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