Reduce number of parameters for path selection
[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.BestPath;
14 import org.opendaylight.protocol.bgp.mode.api.RouteEntry;
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.rev171207.PeerId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.bgp.rib.rib.peer.AdjRibOut;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.Tables;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.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.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class AbstractRouteEntry<T extends BestPath> implements RouteEntry {
36     protected static final NodeIdentifier ROUTES_IDENTIFIER = new NodeIdentifier(Routes.QNAME);
37     private static final Logger LOG = LoggerFactory.getLogger(AbstractRouteEntry.class);
38
39     /**
40      * Create value.
41      *
42      * @param routeId router ID pathArgument
43      * @param path    BestPath
44      * @return MapEntryNode
45      */
46     public abstract MapEntryNode createValue(NodeIdentifierWithPredicates routeId, T path);
47
48     protected static void fillLocRib(final YangInstanceIdentifier routeTarget, final NormalizedNode<?, ?> value,
49             final DOMDataWriteTransaction tx) {
50         if (value != null) {
51             LOG.debug("Write route to LocRib {}", value);
52             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
53         } else {
54             LOG.debug("Delete route from LocRib {}", routeTarget);
55             tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);
56         }
57     }
58
59     protected static void update(final PeerId destPeer, final YangInstanceIdentifier routeTarget,
60             final ContainerNode effAttr, final NormalizedNode<?, ?> value, final RIBSupport ribSup,
61             final DOMDataWriteTransaction tx) {
62         if (!writeRoute(destPeer, routeTarget, effAttr, value, ribSup, tx)) {
63             deleteRoute(destPeer, routeTarget, tx);
64         }
65     }
66
67     protected static boolean writeRoute(final PeerId destPeer, final YangInstanceIdentifier routeTarget,
68             final ContainerNode effAttrib, final NormalizedNode<?, ?> value, final RIBSupport ribSup,
69             final DOMDataWriteTransaction tx) {
70         if (effAttrib != null && value != null) {
71             LOG.debug("Write route {} to peer AdjRibsOut {}", value, destPeer);
72             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
73             tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(ribSup.routeAttributesIdentifier()), effAttrib);
74             return true;
75         }
76         return false;
77     }
78
79     private static void deleteRoute(final PeerId destPeer, final YangInstanceIdentifier routeTarget,
80             final DOMDataWriteTransaction tx) {
81         LOG.trace("Removing {} from transaction for peer {}", routeTarget, destPeer);
82         tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);
83     }
84
85     protected static boolean filterRoutes(final PeerId rootPeer, final PeerId destPeer,
86             final ExportPolicyPeerTracker peerPT, final TablesKey localTK, final PeerRole destPeerRole) {
87         return !rootPeer.equals(destPeer) && isTableSupportedAndReady(destPeer, peerPT, localTK)
88                 && !PeerRole.Internal.equals(destPeerRole);
89     }
90
91     private static boolean isTableSupportedAndReady(final PeerId destPeer, final ExportPolicyPeerTracker peerPT,
92             final TablesKey localTK) {
93         if (!peerPT.isTableSupported(destPeer) || !peerPT.isTableStructureInitialized(destPeer)) {
94             LOG.trace("Route rejected, peer {} does not support this table type {}", destPeer, localTK);
95             return false;
96         }
97         return true;
98     }
99
100     protected static YangInstanceIdentifier getAdjRibOutYII(final RIBSupport ribSup,
101             final YangInstanceIdentifier rootPath, final PathArgument routeId, final TablesKey localTK) {
102         return ribSup.routePath(rootPath.node(AdjRibOut.QNAME).node(Tables.QNAME)
103                 .node(RibSupportUtils.toYangTablesKey(localTK)).node(ROUTES_IDENTIFIER), routeId);
104     }
105
106     protected static PeerRole getRoutePeerIdRole(final ExportPolicyPeerTracker peerPT, final PeerId routePeerId) {
107         for (final PeerRole role : PeerRole.values()) {
108             final PeerExportGroup peerGroup = peerPT.getPeerGroup(role);
109             if (peerGroup != null && peerGroup.containsPeer(routePeerId)) {
110                 return role;
111             }
112         }
113         return null;
114     }
115 }