Bug 4827: Add PathId utility class 12/35612/14
authorMilos Fabian <milfabia@cisco.com>
Wed, 2 Mar 2016 13:59:56 +0000 (14:59 +0100)
committerClaudio D. Gasparini <cgaspari@cisco.com>
Thu, 21 Apr 2016 12:15:49 +0000 (14:15 +0200)
The Utility class provides functions
for optional writing of path-id value
into a ByteBuf.

Change-Id: I65235c7aab9d60df9054ecd475481f21e1c430a8
Signed-off-by: Milos Fabian <milfabia@cisco.com>
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtil.java [new file with mode: 0644]
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtilTest.java [new file with mode: 0644]
features/bgp/src/main/features/features.xml

diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtil.java
new file mode 100644 (file)
index 0000000..ae1c80f
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.parser.spi;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import javax.annotation.Nullable;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
+
+public final class PathIdUtil {
+
+    private PathIdUtil() {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Writes path-id value into the buffer when
+     * the path-id is not null or does not equal to zero.
+     *
+     * @param pathId The NLRI Path Identifier.
+     * @param buffer The ByteBuf where path-id value can be written.
+     */
+    public static void writePathId(final PathId pathId, final ByteBuf buffer) {
+        if (pathId != null && pathId.getValue() != 0) {
+            ByteBufWriteUtil.writeUnsignedInt(pathId.getValue(), buffer);
+        }
+    }
+
+    /**
+     * Reads Path Identifier (4 bytes) from buffer.
+     *
+     * @param buffer Input buffer.
+     * @return Decoded PathId.
+     */
+    public static PathId readPathId(final ByteBuf buffer) {
+        Preconditions.checkArgument(buffer != null && buffer.isReadable(ByteBufWriteUtil.INT_BYTES_LENGTH));
+        return new PathId(buffer.readUnsignedInt());
+    }
+
+    /**
+     * Extract PathId from route change received
+     *
+     * @param data
+     * @param pathNii Path Id NodeIdentifier specific per each Rib support
+     * @return The path identifier from data change, in case its not provided or supported return null
+     */
+    @Nullable
+    public static Long extractPathId(final NormalizedNode<?, ?> data, final NodeIdentifier pathNii) {
+        final NormalizedNode<?, ?> pathId = NormalizedNodes.findNode(data, pathNii).orNull();
+        if (pathId == null) {
+            return null;
+        }
+        return (Long) pathId.getValue();
+    }
+}
diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtilTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/PathIdUtilTest.java
new file mode 100644 (file)
index 0000000..d9b81d5
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.parser.spi;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
+
+public class PathIdUtilTest {
+
+    private ByteBuf buffer;
+
+    @Before
+    public void setUp() {
+        this.buffer = Unpooled.buffer();
+    }
+
+    @Test
+    public void testWritePathIdNull() {
+        PathIdUtil.writePathId(null, this.buffer);
+        Assert.assertEquals(0, this.buffer.readableBytes());
+    }
+
+    @Test
+    public void testWritePathIdZero() {
+        PathIdUtil.writePathId(new PathId(0l), this.buffer);
+        Assert.assertEquals(0, this.buffer.readableBytes());
+    }
+
+    @Test
+    public void testWritePathId() {
+        PathIdUtil.writePathId(new PathId(10l), this.buffer);
+        Assert.assertEquals(Integer.BYTES, this.buffer.readableBytes());
+    }
+
+    @Test
+    public void testReadPathId() {
+        final long expected = 10L;
+        ByteBufWriteUtil.writeUnsignedInt(expected, this.buffer);
+        final PathId pathId = PathIdUtil.readPathId(this.buffer);
+        Assert.assertEquals(expected, pathId.getValue().longValue());
+    }
+
+    @Test
+    public void testExtractPathId() {
+        final NodeIdentifier NII = new NodeIdentifier(QName.create("urn:opendaylight:params:xml:ns:yang:bgp-inet", "2015-03-05", "path-id").intern());
+        final long pathIdValue = 0;
+        final ContainerNode cont = ImmutableContainerNodeSchemaAwareBuilder.create().withNodeIdentifier(NII).
+            addChild(new ImmutableLeafNodeBuilder<>().withNodeIdentifier(NII).withValue(pathIdValue).build()).build();
+        Assert.assertEquals(pathIdValue, PathIdUtil.extractPathId(cont, NII).longValue());
+    }
+
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testReadPathIdBufferNull() {
+        PathIdUtil.readPathId(null);
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testReadPathIdBufferEmpty() {
+        PathIdUtil.readPathId(this.buffer);
+    }
+
+}
index fd17bbbd3837049f878089ff51f5d91c662c0402..585968fc13cb4964e019b4b411d86524ea4051e4 100644 (file)
@@ -79,6 +79,7 @@
         <feature version='${project.version}'>odl-bgpcep-bgp-dependencies</feature>
         <feature version='${project.version}'>odl-bgpcep-bgp-inet</feature>
         <feature version='${config.version}'>odl-config-api</feature>
+        <feature version='${yangtools.version}'>odl-yangtools-yang-data</feature>
         <bundle>mvn:org.opendaylight.bgpcep/bgp-parser-api/{{VERSION}}</bundle>
         <bundle>mvn:org.opendaylight.bgpcep/bgp-parser-spi/{{VERSION}}</bundle>
         <bundle>mvn:org.opendaylight.bgpcep/bgp-parser-impl/{{VERSION}}</bundle>