Bug-6562: Support add-path in base BGP NLRI
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / PathIdUtil.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.parser.spi;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableMap;
14 import io.netty.buffer.ByteBuf;
15 import org.opendaylight.protocol.util.ByteBufWriteUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
22 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
25
26 public final class PathIdUtil {
27     public static final long NON_PATH_ID = 0;
28
29     private PathIdUtil() {
30         throw new UnsupportedOperationException();
31     }
32
33     /**
34      * Writes path-id value into the buffer when
35      * the path-id is not null or does not equal to zero.
36      *
37      * @param pathId The NLRI Path Identifier.
38      * @param buffer The ByteBuf where path-id value can be written.
39      */
40     public static void writePathId(final PathId pathId, final ByteBuf buffer) {
41         if (pathId != null && pathId.getValue() != 0) {
42             ByteBufWriteUtil.writeUnsignedInt(pathId.getValue(), buffer);
43         }
44     }
45
46     /**
47      * Reads Path Identifier (4 bytes) from buffer.
48      *
49      * @param buffer Input buffer.
50      * @return Decoded PathId.
51      */
52     public static PathId readPathId(final ByteBuf buffer) {
53         Preconditions.checkArgument(buffer != null && buffer.isReadable(ByteBufWriteUtil.INT_BYTES_LENGTH));
54         return new PathId(buffer.readUnsignedInt());
55     }
56
57     /**
58      * Extract PathId from route change received
59      *
60      * @param data Data containing the path Id
61      * @param pathNii Path Id NodeIdentifier specific per each Rib support
62      * @return The path identifier from data change
63      */
64     public static Long extractPathId(final NormalizedNode<?, ?> data, final NodeIdentifier pathNii) {
65         final NormalizedNode<?, ?> pathId = NormalizedNodes.findNode(data, pathNii).orNull();
66         if (pathId == null) {
67             return null;
68         }
69         return (Long) pathId.getValue();
70     }
71
72     /**
73      * Create a Add Path PathArgument Key(prefix+pathId)
74      *
75      * @param pathId Path Id value
76      * @param routeId Route Id value
77      * @param routeQname route QName provided per each RibSupport
78      * @param pathidQname Path Id QName provided per each RibSupport
79      * @param routeKeyQname Prefix QName provided per each RibSupport
80      * @return Route Key Nid
81      */
82     public static PathArgument createNidKey(final long pathId, final PathArgument routeId, final QName routeQname, final QName pathidQname,
83         final QName routeKeyQname) {
84         return createNodeIdentifierWithPredicates(routeQname, pathidQname, pathId, routeKeyQname, getObjectKey(routeId, routeKeyQname));
85     }
86
87     /**
88      * Get route key object ( prefgit stat  ix / key-value/ .. )
89      *
90      * @param routeId PathArgument containing the key
91      * @param routeKeyQname routeKey Qname
92      * @return key
93      */
94     public static Object getObjectKey(final PathArgument routeId, final QName routeKeyQname) {
95         return (((NodeIdentifierWithPredicates) routeId).getKeyValues()).get(routeKeyQname);
96     }
97
98     public static NodeIdentifierWithPredicates createNodeIdentifierWithPredicates(final QName routeQname, final QName pathidQname, final Object pathId,
99         final QName routeKeyQname, final Object keyObject) {
100         final ImmutableMap<QName, Object> keyValues = ImmutableMap.of(pathidQname, pathId, routeKeyQname, keyObject);
101         return new NodeIdentifierWithPredicates(routeQname, keyValues);
102     }
103
104     /**
105      * Build Path Id
106      *
107      * @param routesCont route container
108      * @param pathIdNii path Id node Identifier
109      * @return PathId or null in case is not the container
110      */
111     public static PathId buildPathId(final DataContainerNode<? extends PathArgument> routesCont, final NodeIdentifier pathIdNii) {
112         final Long pathIdVal = PathIdUtil.extractPathId(routesCont, pathIdNii);
113         return pathIdVal == null ? null : new PathId(pathIdVal);
114     }
115
116     /**
117      * Build Route Key for supporting mp
118      * Key is composed by 2 elements (route-key + path Id)
119      *
120      * @param routeQname route Qname
121      * @param routeKeyQname route key Qname
122      * @param pathIdQname path Id Qname
123      * @param routeKeyValue route key value
124      * @param maybePathIdLeaf path id container, it might me supported or not, in that case default 0 value will be assigned
125      * @return Route Key Nid
126      */
127     public static NodeIdentifierWithPredicates createNidKey(final QName routeQname, final QName routeKeyQname, final QName pathIdQname,
128         final Object routeKeyValue, final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf) {
129         // FIXME: a cache here would mean we instantiate the same identifier for each route making comparison quicker.
130         final Object pathId = maybePathIdLeaf.isPresent() ? (maybePathIdLeaf.get()).getValue() : NON_PATH_ID;
131         return createNodeIdentifierWithPredicates(routeQname, pathIdQname, pathId, routeKeyQname, routeKeyValue);
132     }
133 }