Bump to odlparent-3.0.2 and yangtools-2.0.0
[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.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import io.netty.buffer.ByteBuf;
14 import java.util.Optional;
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         return (Long) NormalizedNodes.findNode(data, pathNii).map(NormalizedNode::getValue).orElse(null);
66     }
67
68     /**
69      * Create a Add Path PathArgument Key(prefix+pathId).
70      *
71      * @param pathId        Path Id value
72      * @param routeId       Route Id value
73      * @param routeQname    route QName provided per each RibSupport
74      * @param pathidQname   Path Id QName provided per each RibSupport
75      * @param routeKeyQname Prefix QName provided per each RibSupport
76      * @return Route Key Nid
77      */
78     public static NodeIdentifierWithPredicates createNidKey(final long pathId, final PathArgument routeId,
79         final QName routeQname,
80             final QName pathidQname, final QName routeKeyQname) {
81         return createNodeIdentifierWithPredicates(routeQname, pathidQname, pathId, routeKeyQname,
82             getObjectKey(routeId, routeKeyQname));
83     }
84
85     /**
86      * Get route key object ( prefgit stat  ix / key-value/ .. ).
87      *
88      * @param routeId PathArgument containing the key
89      * @param routeKeyQname routeKey Qname
90      * @return key
91      */
92     public static Object getObjectKey(final PathArgument routeId, final QName routeKeyQname) {
93         return ((NodeIdentifierWithPredicates) routeId).getKeyValues().get(routeKeyQname);
94     }
95
96     public static NodeIdentifierWithPredicates createNodeIdentifierWithPredicates(final QName routeQname, final QName pathidQname, final Object pathId,
97         final QName routeKeyQname, final Object keyObject) {
98         final ImmutableMap<QName, Object> keyValues = ImmutableMap.of(pathidQname, pathId, routeKeyQname, keyObject);
99         return new NodeIdentifierWithPredicates(routeQname, keyValues);
100     }
101
102     /**
103      * Build Path Id.
104      *
105      * @param routesCont route container
106      * @param pathIdNii path Id node Identifier
107      * @return PathId or null in case is not the container
108      */
109     public static PathId buildPathId(final DataContainerNode<? extends PathArgument> routesCont,
110             final NodeIdentifier pathIdNii) {
111         final Long pathIdVal = PathIdUtil.extractPathId(routesCont, pathIdNii);
112         return pathIdVal == null ? null : new PathId(pathIdVal);
113     }
114
115     /**
116      * Build Route Key for supporting mp.
117      * Key is composed by 2 elements (route-key + path Id).
118      *
119      * @param routeQname route Qname
120      * @param routeKeyQname route key Qname
121      * @param pathIdQname path Id Qname
122      * @param routeKeyValue route key value
123      * @param maybePathIdLeaf path id container, it might me supported or not, in that case default 0 value will
124      * be
125      *                        assigned
126      * @return Route Key Nid
127      */
128     public static NodeIdentifierWithPredicates createNidKey(final QName routeQname, final QName routeKeyQname,
129             final QName pathIdQname, final Object routeKeyValue,
130             final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf) {
131         // FIXME: a cache here would mean we instantiate the same identifier for each route making comparison quicker.
132         final Object pathId = maybePathIdLeaf.isPresent() ? maybePathIdLeaf.get().getValue() : NON_PATH_ID;
133         return createNodeIdentifierWithPredicates(routeQname, pathIdQname, pathId, routeKeyQname, routeKeyValue);
134     }
135 }