Minimize use of ByteBufWriteUtil in bgp-parser-spi
[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 package org.opendaylight.protocol.bgp.parser.spi;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import java.util.Optional;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
14 import org.opendaylight.yangtools.util.ImmutableOffsetMapTemplate;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.Uint32;
17 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
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 Uint32 NON_PATH_ID_VALUE = Uint32.ZERO;
28     public static final PathId NON_PATH_ID = new PathId(NON_PATH_ID_VALUE);
29
30     private PathIdUtil() {
31         // Hidden on purpose
32     }
33
34     /**
35      * Writes path-id value into the buffer when 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) {
42             final int value = pathId.getValue().intValue();
43             if (value != 0) {
44                 buffer.writeInt(value);
45             }
46         }
47     }
48
49     /**
50      * Reads Path Identifier (4 bytes) from buffer.
51      *
52      * @param buffer Input buffer.
53      * @return Decoded PathId.
54      */
55     public static PathId readPathId(final ByteBuf buffer) {
56         Preconditions.checkArgument(buffer != null && buffer.isReadable(Integer.BYTES));
57         return new PathId(ByteBufUtils.readUint32(buffer));
58     }
59
60     /**
61      * Extract PathId from route change received.
62      *
63      * @param data    Data containing the path Id
64      * @param pathNii Path Id NodeIdentifier specific per each Rib support
65      * @return The path identifier from data change
66      */
67     public static Uint32 extractPathId(final NormalizedNode<?, ?> data, final NodeIdentifier pathNii) {
68         return (Uint32) NormalizedNodes.findNode(data, pathNii).map(NormalizedNode::getValue).orElse(null);
69     }
70
71     /**
72      * Build Path Id.
73      *
74      * @param routesCont route container
75      * @param pathIdNii  path Id node Identifier
76      * @return PathId or null in case is not the container
77      */
78     public static PathId buildPathId(final DataContainerNode<? extends PathArgument> routesCont,
79             final NodeIdentifier pathIdNii) {
80         final Uint32 pathIdVal = PathIdUtil.extractPathId(routesCont, pathIdNii);
81         return pathIdVal == null ? null : new PathId(pathIdVal);
82     }
83
84     /**
85      * Build Route Key for supporting mp.
86      * Key is composed by 2 elements (route-key + path Id).
87      *
88      * @param routeQName       route QName
89      * @param routeKeyTemplate route key template
90      * @param routeKeyValue    route key value
91      * @param maybePathIdLeaf  path id container, it might me supported or not, in that case default 0 value will
92      *                         be assigned
93      * @return Route Key Nid
94      */
95     public static NodeIdentifierWithPredicates createNidKey(final QName routeQName,
96             final ImmutableOffsetMapTemplate<QName> routeKeyTemplate, final Object routeKeyValue,
97             final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf) {
98         // FIXME: a cache here would mean we instantiate the same identifier for each route making comparison quicker.
99         final Object pathId = maybePathIdLeaf.isPresent() ? maybePathIdLeaf.get().getValue() : NON_PATH_ID_VALUE;
100         return NodeIdentifierWithPredicates.of(routeQName,
101             routeKeyTemplate.instantiateWithValues(pathId, routeKeyValue));
102     }
103 }