Bug-607: Segment Routing TLVs parsers/serializers 67/7667/2
authorMilos Fabian <milfabia@cisco.com>
Wed, 4 Jun 2014 09:26:33 +0000 (11:26 +0200)
committerMilos Fabian <milfabia@cisco.com>
Wed, 4 Jun 2014 12:30:30 +0000 (14:30 +0200)
-added parser/serializer for Path Setup Type Tlv + augmented RP and SRP object
-added parser/serializer for SR PCE Capability Tlv + augmented Open Object
-added parsers/serializers junit tests
-added activator

Change-Id: Ib800bdc12ed78f89e39b3df185e006707bd702b0
Signed-off-by: Milos Fabian <milfabia@cisco.com>
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/CInitiated00SrpObjectWithPstTlvParser.java [new file with mode: 0644]
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PathSetupTypeTlvParser.java [new file with mode: 0644]
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepRpObjectWithPstTlvParser.java [new file with mode: 0644]
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/PcepOpenObjectWithSpcTlvParser.java [new file with mode: 0644]
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SegmentRoutingActivator.java [new file with mode: 0644]
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SrPceCapabilityTlvParser.java [new file with mode: 0644]
pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepObjectParserTest.java [new file with mode: 0644]
pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepTlvParserTest.java [new file with mode: 0644]
pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/segment/routing02/PcepObjectParserTest.java [new file with mode: 0644]
pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/segment/routing02/PcepTlvParserTest.java [new file with mode: 0644]

diff --git a/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/CInitiated00SrpObjectWithPstTlvParser.java b/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/CInitiated00SrpObjectWithPstTlvParser.java
new file mode 100644 (file)
index 0000000..4beef11
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2014 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.pcep.lsp.setup.type01;
+
+import org.opendaylight.protocol.pcep.ietf.initiated00.CInitiated00SrpObjectParser;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.Tlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs8;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs8Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
+
+public class CInitiated00SrpObjectWithPstTlvParser extends CInitiated00SrpObjectParser {
+
+    public CInitiated00SrpObjectWithPstTlvParser(TlvRegistry tlvReg) {
+        super(tlvReg);
+    }
+
+    @Override
+    public void addTlv(SrpBuilder builder, Tlv tlv) {
+        super.addTlv(builder, tlv);
+        final Tlvs8Builder tlvBuilder = new Tlvs8Builder();
+        if (builder.getTlvs() != null) {
+            if (builder.getTlvs().getAugmentation(Tlvs8.class) != null) {
+                final Tlvs8 t = builder.getTlvs().getAugmentation(Tlvs8.class);
+                if (t.getPathSetupType() != null) {
+                    tlvBuilder.setPathSetupType(t.getPathSetupType());
+                }
+            }
+        }
+        if (tlv instanceof PathSetupType) {
+            tlvBuilder.setPathSetupType((PathSetupType) tlv);
+        }
+        builder.setTlvs(new TlvsBuilder().addAugmentation(Tlvs8.class, tlvBuilder.build()).build());
+    }
+
+    @Override
+    public byte[] serializeTlvs(Tlvs tlvs) {
+        if (tlvs == null) {
+            return new byte[0];
+        }
+        final byte[] prev = super.serializeTlvs(tlvs);
+        int finalLength = prev.length;
+        byte[] nameBytes = null;
+        if (tlvs.getAugmentation(Tlvs8.class) != null) {
+            final Tlvs8 nameTlvs = tlvs.getAugmentation(Tlvs8.class);
+            if (nameTlvs.getPathSetupType() != null) {
+                nameBytes = serializeTlv(nameTlvs.getPathSetupType());
+                finalLength += nameBytes.length;
+            }
+        }
+        final byte[] result = new byte[finalLength];
+        ByteArray.copyWhole(prev, result, 0);
+        int offset = prev.length;
+        if (nameBytes != null) {
+            ByteArray.copyWhole(nameBytes, result, offset);
+            offset += nameBytes.length;
+        }
+        return result;
+    }
+
+}
diff --git a/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PathSetupTypeTlvParser.java b/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PathSetupTypeTlvParser.java
new file mode 100644 (file)
index 0000000..38f3ed0
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2014 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.pcep.lsp.setup.type01;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.TlvParser;
+import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupTypeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
+
+public class PathSetupTypeTlvParser implements TlvParser, TlvSerializer {
+
+    // http://tools.ietf.org/html/draft-sivabalan-pce-segment-routing-01#section-9.3
+    public static final int TYPE = 27;
+    private static final int PST_LENGTH = 1;
+    private static final int OFFSET = 4 - PST_LENGTH;
+
+    @Override
+    public byte[] serializeTlv(Tlv tlv) {
+        Preconditions.checkNotNull(tlv, "PathSetupType is mandatory.");
+        final PathSetupType pstTlv = (PathSetupType) tlv;
+        return TlvUtil.formatTlv(TYPE, new byte[] { 0, 0, 0, (byte) (pstTlv.isPst() ? 1 : 0) });
+    }
+
+    @Override
+    public Tlv parseTlv(ByteBuf buffer) throws PCEPDeserializerException {
+        if (buffer == null) {
+            return null;
+        }
+        final boolean pst = buffer.readerIndex(OFFSET).readBoolean();
+        return new PathSetupTypeBuilder().setPst(pst).build();
+    }
+
+}
diff --git a/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepRpObjectWithPstTlvParser.java b/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepRpObjectWithPstTlvParser.java
new file mode 100644 (file)
index 0000000..550069b
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2014 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.pcep.lsp.setup.type01;
+
+import org.opendaylight.protocol.pcep.impl.object.PCEPRequestParameterObjectParser;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs3;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs3Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.RpBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.Tlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.TlvsBuilder;
+
+public class PcepRpObjectWithPstTlvParser extends PCEPRequestParameterObjectParser {
+
+    public PcepRpObjectWithPstTlvParser(TlvRegistry tlvReg) {
+        super(tlvReg);
+    }
+
+    @Override
+    public void addTlv(RpBuilder builder, Tlv tlv) {
+        super.addTlv(builder, tlv);
+        final Tlvs3Builder tlvBuilder = new Tlvs3Builder();
+        if (builder.getTlvs() != null) {
+            if (builder.getTlvs().getAugmentation(Tlvs3.class) != null) {
+                final Tlvs3 t = builder.getTlvs().getAugmentation(Tlvs3.class);
+                if (t.getPathSetupType() != null) {
+                    tlvBuilder.setPathSetupType(t.getPathSetupType());
+                }
+            }
+        }
+        if (tlv instanceof PathSetupType) {
+            tlvBuilder.setPathSetupType((PathSetupType) tlv);
+        }
+        builder.setTlvs(new TlvsBuilder().addAugmentation(Tlvs3.class, tlvBuilder.build()).build());
+    }
+
+    @Override
+    public byte[] serializeTlvs(Tlvs tlvs) {
+        if (tlvs == null) {
+            return new byte[0];
+        }
+        final byte[] prev = super.serializeTlvs(tlvs);
+        int finalLength = prev.length;
+        byte[] nameBytes = null;
+        if (tlvs.getAugmentation(Tlvs3.class) != null) {
+            final Tlvs3 nameTlvs = tlvs.getAugmentation(Tlvs3.class);
+            if (nameTlvs.getPathSetupType() != null) {
+                nameBytes = serializeTlv(nameTlvs.getPathSetupType());
+                finalLength += nameBytes.length;
+            }
+        }
+        final byte[] result = new byte[finalLength];
+        ByteArray.copyWhole(prev, result, 0);
+        int offset = prev.length;
+        if (nameBytes != null) {
+            ByteArray.copyWhole(nameBytes, result, offset);
+            offset += nameBytes.length;
+        }
+        return result;
+    }
+
+}
diff --git a/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/PcepOpenObjectWithSpcTlvParser.java b/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/PcepOpenObjectWithSpcTlvParser.java
new file mode 100644 (file)
index 0000000..8361a3a
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2014 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.pcep.segment.routing02;
+
+import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
+
+public class PcepOpenObjectWithSpcTlvParser extends PCEPOpenObjectParser {
+
+    public PcepOpenObjectWithSpcTlvParser(TlvRegistry tlvReg) {
+        super(tlvReg);
+    }
+
+    @Override
+    public void addTlv(TlvsBuilder tbuilder, Tlv tlv) {
+        super.addTlv(tbuilder, tlv);
+        final Tlvs1Builder tlvBuilder = new Tlvs1Builder();
+        if (tbuilder.getAugmentation(Tlvs1.class) != null) {
+            final Tlvs1 tlvs = tbuilder.getAugmentation(Tlvs1.class);
+            if (tlvs.getSrPceCapability() != null) {
+                tlvBuilder.setSrPceCapability(tlvs.getSrPceCapability());
+            }
+        }
+        if (tlv instanceof SrPceCapability) {
+            tlvBuilder.setSrPceCapability((SrPceCapability) tlv);
+        }
+        tbuilder.addAugmentation(Tlvs1.class, tlvBuilder.build());
+    }
+
+    @Override
+    public byte[] serializeTlvs(Tlvs tlvs) {
+        if (tlvs == null) {
+            return new byte[0];
+        }
+        final byte[] prev = super.serializeTlvs(tlvs);
+        int finalLength = prev.length;
+        byte[] nameBytes = null;
+        if (tlvs.getAugmentation(Tlvs1.class) != null) {
+            final Tlvs1 spcTlvs = tlvs.getAugmentation(Tlvs1.class);
+            if (spcTlvs.getSrPceCapability() != null) {
+                nameBytes = serializeTlv(spcTlvs.getSrPceCapability());
+                finalLength += nameBytes.length;
+            }
+        }
+        final byte[] result = new byte[finalLength];
+        ByteArray.copyWhole(prev, result, 0);
+        int offset = prev.length;
+        if (nameBytes != null) {
+            ByteArray.copyWhole(nameBytes, result, offset);
+            offset += nameBytes.length;
+        }
+        return result;
+    }
+
+}
diff --git a/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SegmentRoutingActivator.java b/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SegmentRoutingActivator.java
new file mode 100644 (file)
index 0000000..584be15
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2014 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.pcep.segment.routing02;
+
+import com.google.common.collect.Lists;
+import java.util.List;
+import org.opendaylight.protocol.pcep.lsp.setup.type01.PathSetupTypeTlvParser;
+import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
+import org.opendaylight.protocol.pcep.spi.pojo.AbstractPCEPExtensionProviderActivator;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapability;
+
+public class SegmentRoutingActivator extends AbstractPCEPExtensionProviderActivator {
+
+    @Override
+    protected List<AutoCloseable> startImpl(PCEPExtensionProviderContext context) {
+        final List<AutoCloseable> regs = Lists.newArrayList();
+
+        regs.add(context.registerTlvParser(SrPceCapabilityTlvParser.TYPE, new SrPceCapabilityTlvParser()));
+        regs.add(context.registerTlvSerializer(SrPceCapability.class, new SrPceCapabilityTlvParser()));
+
+        regs.add(context.registerTlvParser(PathSetupTypeTlvParser.TYPE, new PathSetupTypeTlvParser()));
+        regs.add(context.registerTlvSerializer(PathSetupType.class, new PathSetupTypeTlvParser()));
+        return regs;
+    }
+}
diff --git a/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SrPceCapabilityTlvParser.java b/pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SrPceCapabilityTlvParser.java
new file mode 100644 (file)
index 0000000..e1cec14
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2014 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.pcep.segment.routing02;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.TlvParser;
+import org.opendaylight.protocol.pcep.spi.TlvSerializer;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapabilityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
+
+public class SrPceCapabilityTlvParser implements TlvParser, TlvSerializer {
+
+    public static final int TYPE = 26;
+
+    private static final int MSD_LENGTH = 1;
+    private static final int OFFSET = 4 - MSD_LENGTH;
+
+    @Override
+    public byte[] serializeTlv(Tlv tlv) {
+        Preconditions.checkNotNull(tlv, "SrPceCapability is mandatory.");
+        final SrPceCapability spcTlv = (SrPceCapability) tlv;
+        return TlvUtil.formatTlv(TYPE, ByteArray.intToBytes(spcTlv.getMsd()));
+    }
+
+    @Override
+    public Tlv parseTlv(ByteBuf buffer) throws PCEPDeserializerException {
+        if (buffer == null) {
+            return null;
+        }
+        final short msd = buffer.readerIndex(OFFSET).readUnsignedByte();
+        return new SrPceCapabilityBuilder().setMsd(msd).build();
+    }
+
+}
diff --git a/pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepObjectParserTest.java b/pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepObjectParserTest.java
new file mode 100644 (file)
index 0000000..b57a99c
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2014 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.pcep.lsp.setup.type01;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.protocol.pcep.segment.routing02.SegmentRoutingActivator;
+import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
+import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Srp1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs3;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs3Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs8;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs8Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupTypeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.RpBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.TlvsBuilder;
+
+public class PcepObjectParserTest {
+
+    private static final byte[] rpObjectWithPstTlvBytes = { 0x2, 0x10, 0x0, 0x14, 0x0, 0x0, 0x4, 0x2d, (byte) 0xde,
+        (byte) 0xad, (byte) 0xbe, (byte) 0xef,
+        /* pst-tlv */
+        0x0, 0x1b, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
+
+    private static final byte[] srpObjectWithPstTlvBytes = { 0x21, 0x10, 0x00, 0x14, 0x0, 0x0, 0x0, 0x01, 0x0, 0x0,
+        0x0, 0x01,
+        /* pst-tlv */
+        0x0, 0x1b, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
+
+    private TlvRegistry tlvRegistry;
+
+    private SimplePCEPExtensionProviderContext ctx;
+    private SegmentRoutingActivator act;
+
+    @Before
+    public void setUp() {
+        this.ctx = new SimplePCEPExtensionProviderContext();
+        this.act = new SegmentRoutingActivator();
+        this.act.start(this.ctx);
+        this.tlvRegistry = this.ctx.getTlvHandlerRegistry();
+    }
+
+    @Test
+    public void testRpObjectWithPstTlvParser() throws PCEPDeserializerException {
+        final PcepRpObjectWithPstTlvParser parser = new PcepRpObjectWithPstTlvParser(this.tlvRegistry);
+        final RpBuilder builder = new RpBuilder();
+        builder.setProcessingRule(false);
+        builder.setIgnore(false);
+        builder.setReoptimization(true);
+        builder.setBiDirectional(false);
+        builder.setLoose(true);
+        builder.setMakeBeforeBreak(true);
+        builder.setOrder(false);
+        builder.setPathKey(false);
+        builder.setSupplyOf(false);
+        builder.setFragmentation(false);
+        builder.setP2mp(false);
+        builder.setEroCompression(false);
+        builder.setPriority((short) 5);
+        builder.setRequestId(new RequestId(0xdeadbeefL));
+        builder.setTlvs(new TlvsBuilder().addAugmentation(Tlvs3.class,
+                new Tlvs3Builder().setPathSetupType(new PathSetupTypeBuilder().setPst(true).build()).build()).build());
+
+        final ByteBuf result = Unpooled.wrappedBuffer(rpObjectWithPstTlvBytes);
+        assertEquals(builder.build(),
+                parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
+        assertArrayEquals(rpObjectWithPstTlvBytes, parser.serializeObject(builder.build()));
+    }
+
+    @Test
+    public void testSrpObjectWithPstTlvParser() throws PCEPDeserializerException {
+        final CInitiated00SrpObjectWithPstTlvParser parser = new CInitiated00SrpObjectWithPstTlvParser(this.tlvRegistry);
+        final SrpBuilder builder = new SrpBuilder();
+        builder.setProcessingRule(false);
+        builder.setIgnore(false);
+        builder.setOperationId(new SrpIdNumber(1L));
+        builder.addAugmentation(Srp1.class, new Srp1Builder().setRemove(true).build());
+        builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder()
+                .addAugmentation(Tlvs8.class,
+                        new Tlvs8Builder().setPathSetupType(new PathSetupTypeBuilder().setPst(true).build()).build())
+                .build());
+
+        final ByteBuf result = Unpooled.wrappedBuffer(srpObjectWithPstTlvBytes);
+        assertEquals(builder.build(),
+                parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
+        assertArrayEquals(srpObjectWithPstTlvBytes, parser.serializeObject(builder.build()));
+    }
+}
diff --git a/pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepTlvParserTest.java b/pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PcepTlvParserTest.java
new file mode 100644 (file)
index 0000000..76311ef
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2014 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.pcep.lsp.setup.type01;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import io.netty.buffer.Unpooled;
+import org.junit.Test;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupTypeBuilder;
+
+public class PcepTlvParserTest {
+
+    private static final byte[] pstTlvBytes = { 0x0, 0x1b, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
+
+    @Test
+    public void testPathSetupTypeTlvParser() throws PCEPDeserializerException {
+        final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
+        final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst(true).build();
+
+        assertEquals(pstTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(pstTlvBytes, 4))));
+        assertArrayEquals(pstTlvBytes, parser.serializeTlv(pstTlv));
+    }
+
+}
diff --git a/pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/segment/routing02/PcepObjectParserTest.java b/pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/segment/routing02/PcepObjectParserTest.java
new file mode 100644 (file)
index 0000000..b9e8907
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2014 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.pcep.segment.routing02;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.TlvRegistry;
+import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.Tlvs1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapabilityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
+
+public class PcepObjectParserTest {
+
+    private static final byte[] openObjectBytes = { 0x01, 0x10, 0x00, 0x10, 0x20, 0x1e, 0x78, 0x01,
+        /* sr-capability-tlv */
+        0x0, 0x1a, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
+
+    private TlvRegistry tlvRegistry;
+
+    private SimplePCEPExtensionProviderContext ctx;
+    private SegmentRoutingActivator act;
+
+    @Before
+    public void setUp() {
+        this.ctx = new SimplePCEPExtensionProviderContext();
+        this.act = new SegmentRoutingActivator();
+        this.act.start(this.ctx);
+        this.tlvRegistry = this.ctx.getTlvHandlerRegistry();
+    }
+
+    @Test
+    public void testOpenObjectWithSpcTlv() throws PCEPDeserializerException {
+        final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(tlvRegistry);
+
+        final OpenBuilder builder = new OpenBuilder();
+        builder.setProcessingRule(false);
+        builder.setIgnore(false);
+        builder.setVersion(new ProtocolVersion((short) 1));
+        builder.setKeepalive((short) 30);
+        builder.setDeadTimer((short) 120);
+        builder.setSessionId((short) 1);
+
+        Tlvs1 tlv = new Tlvs1Builder().setSrPceCapability(new SrPceCapabilityBuilder().setMsd((short) 1).build())
+                .build();
+        builder.setTlvs(new TlvsBuilder().addAugmentation(Tlvs1.class, tlv).build());
+
+        final ByteBuf result = Unpooled.wrappedBuffer(openObjectBytes);
+        assertEquals(builder.build(),
+                parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
+        assertArrayEquals(openObjectBytes, parser.serializeObject(builder.build()));
+    }
+
+}
diff --git a/pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/segment/routing02/PcepTlvParserTest.java b/pcep/segment-routing/src/test/java/org/opendaylight/protocol/pcep/segment/routing02/PcepTlvParserTest.java
new file mode 100644 (file)
index 0000000..790eabf
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014 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.pcep.segment.routing02;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import io.netty.buffer.Unpooled;
+import org.junit.Test;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.sr.pce.capability.tlv.SrPceCapabilityBuilder;
+
+public class PcepTlvParserTest {
+
+    private static final byte[] spcTlvBytes = { 0x0, 0x1a, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
+
+    @Test
+    public void testSrPceCapabilityParser() throws PCEPDeserializerException {
+        final SrPceCapabilityTlvParser parser = new SrPceCapabilityTlvParser();
+        final SrPceCapability spcTlv = new SrPceCapabilityBuilder().setMsd((short) 1).build();
+        assertEquals(spcTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(spcTlvBytes, 4))));
+        assertArrayEquals(spcTlvBytes, parser.serializeTlv(spcTlv));
+    }
+
+}