2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.parser.spi;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import java.util.Optional;
14 import org.opendaylight.protocol.util.ByteBufUtils;
15 import org.opendaylight.protocol.util.ByteBufWriteUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
17 import org.opendaylight.yangtools.util.ImmutableOffsetMapTemplate;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.Uint32;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
28 public final class PathIdUtil {
29 public static final Uint32 NON_PATH_ID_VALUE = Uint32.ZERO;
30 public static final PathId NON_PATH_ID = new PathId(NON_PATH_ID_VALUE);
32 private PathIdUtil() {
33 throw new UnsupportedOperationException();
37 * Writes path-id value into the buffer when
38 * the path-id is not null or does not equal to zero.
40 * @param pathId The NLRI Path Identifier.
41 * @param buffer The ByteBuf where path-id value can be written.
43 public static void writePathId(final PathId pathId, final ByteBuf buffer) {
45 final Uint32 value = pathId.getValue();
46 if (value.toJava() != 0) {
47 ByteBufWriteUtil.writeUnsignedInt(value, buffer);
53 * Reads Path Identifier (4 bytes) from buffer.
55 * @param buffer Input buffer.
56 * @return Decoded PathId.
58 public static PathId readPathId(final ByteBuf buffer) {
59 Preconditions.checkArgument(buffer != null && buffer.isReadable(ByteBufWriteUtil.INT_BYTES_LENGTH));
60 return new PathId(ByteBufUtils.readUint32(buffer));
64 * Extract PathId from route change received.
66 * @param data Data containing the path Id
67 * @param pathNii Path Id NodeIdentifier specific per each Rib support
68 * @return The path identifier from data change
70 public static Uint32 extractPathId(final NormalizedNode<?, ?> data, final NodeIdentifier pathNii) {
71 return (Uint32) NormalizedNodes.findNode(data, pathNii).map(NormalizedNode::getValue).orElse(null);
77 * @param routesCont route container
78 * @param pathIdNii path Id node Identifier
79 * @return PathId or null in case is not the container
81 public static PathId buildPathId(final DataContainerNode<? extends PathArgument> routesCont,
82 final NodeIdentifier pathIdNii) {
83 final Uint32 pathIdVal = PathIdUtil.extractPathId(routesCont, pathIdNii);
84 return pathIdVal == null ? null : new PathId(pathIdVal);
88 * Build Route Key for supporting mp.
89 * Key is composed by 2 elements (route-key + path Id).
91 * @param routeQName route QName
92 * @param routeKeyTemplate route key template
93 * @param routeKeyValue route key value
94 * @param maybePathIdLeaf path id container, it might me supported or not, in that case default 0 value will
96 * @return Route Key Nid
98 public static NodeIdentifierWithPredicates createNidKey(final QName routeQName,
99 final ImmutableOffsetMapTemplate<QName> routeKeyTemplate, final Object routeKeyValue,
100 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf) {
101 // FIXME: a cache here would mean we instantiate the same identifier for each route making comparison quicker.
102 final Object pathId = maybePathIdLeaf.isPresent() ? maybePathIdLeaf.get().getValue() : NON_PATH_ID_VALUE;
103 return NodeIdentifierWithPredicates.of(routeQName,
104 routeKeyTemplate.instantiateWithValues(pathId, routeKeyValue));