Added definition of vendor-specific tlv into pcep-types. 44/5544/3
authorMilos Fabian <milfabia@cisco.com>
Tue, 4 Mar 2014 17:05:00 +0000 (18:05 +0100)
committerMilos Fabian <milfabia@cisco.com>
Fri, 7 Mar 2014 07:34:45 +0000 (08:34 +0100)
Added abstract vs-tlv parser.

Change-Id: I62f920f1db35161fe7fa37645f2a7318eaa434af
Signed-off-by: Milos Fabian <milfabia@cisco.com>
pcep/api/src/main/yang/pcep-types.yang
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPLspaObjectParser.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractVendorSpecificTlvParser.java [new file with mode: 0644]

index 365e2b369fdbe0ab8f1b66880e0608d86e71db62..26c5dda9da4a64f3937bee84fb7437af758ad3a8 100644 (file)
@@ -7,6 +7,7 @@ module pcep-types {
        import ietf-inet-types { prefix inet; revision-date 2010-09-24; }
        import network-concepts { prefix netc; revision-date 2013-11-25; }
        import rsvp { prefix rsvp; revision-date 2013-08-20; }
+       import iana { prefix iana; revision-date 2013-08-16; }
 
        organization "Cisco Systems, Inc.";
        contact "Robert Varga <rovarga@cisco.com>";
@@ -157,6 +158,20 @@ module pcep-types {
                }
        }
 
+       grouping vs-tlv {
+        description "Vendor-specific TLV.";
+        container vs-tlv {
+                uses tlv;
+
+                leaf enterprise-number {
+                    type iana:enterprise-number;
+                }
+
+                choice vendor-payload {
+                }
+        }
+    }
+
        // Objects
        grouping object-header {
                description "Common Object Header";
@@ -493,7 +508,6 @@ module pcep-types {
                        uses object;
                        uses rsvp:tunnel-attributes;
                        container "tlvs" {
-                       
                        }
                }
        }
index 2e666272c8f391f25d358bc458bd3ca1d61a8776..6865faadee132179aa3800ece113c25a42ab4f75 100644 (file)
@@ -126,12 +126,12 @@ public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuild
        }
 
        public byte[] serializeTlvs(final Tlvs tlvs) {
-               return new byte[0];
+           return new byte[0];
        }
 
        @Override
        public int getObjectType() {
-               return TYPE;
+               return TYPE;
        }
 
        @Override
diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractVendorSpecificTlvParser.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractVendorSpecificTlvParser.java
new file mode 100644 (file)
index 0000000..bd43e70
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * 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.spi;
+
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
+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.vs.tlv.VsTlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlvBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.vs.tlv.VendorPayload;
+
+abstract public class AbstractVendorSpecificTlvParser implements TlvParser, TlvSerializer {
+
+    public static final int TYPE = 27;
+
+    protected static final int ENTERPRISE_NUM_LENGTH = 4;
+
+    @Override
+    public int getType() {
+        return TYPE;
+    }
+
+    @Override
+    public byte[] serializeTlv(Tlv tlv) {
+        if (tlv == null) {
+            throw new IllegalArgumentException("Vendor Specific Tlv is mandatory.");
+        }
+        final VsTlv vsTlv = (VsTlv) tlv;
+        if (vsTlv.getEnterpriseNumber().getValue() == getEnterpriseNumber()) {
+            final byte[] payloadBytes = serializeVendorPayload(vsTlv.getVendorPayload());
+            final byte[] ianaNumBytes = ByteArray.longToBytes(vsTlv.getEnterpriseNumber().getValue(),
+                    ENTERPRISE_NUM_LENGTH);
+
+            final byte[] bytes = new byte[ianaNumBytes.length + payloadBytes.length];
+            System.arraycopy(ianaNumBytes, 0, bytes, 0, ENTERPRISE_NUM_LENGTH);
+            System.arraycopy(payloadBytes, 0, bytes, ENTERPRISE_NUM_LENGTH, payloadBytes.length);
+            return bytes;
+        }
+        return new byte[0];
+    }
+
+    @Override
+    public VsTlv parseTlv(byte[] valueBytes) throws PCEPDeserializerException {
+        VsTlvBuilder vsTlvBuider = new VsTlvBuilder();
+        if (valueBytes == null || valueBytes.length == 0) {
+            throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
+        }
+
+        byte[] enBytes = ByteArray.subByte(valueBytes, 0, ENTERPRISE_NUM_LENGTH);
+        long en = ByteArray.bytesToLong(enBytes);
+        if (en == getEnterpriseNumber()) {
+            vsTlvBuider.setEnterpriseNumber(new EnterpriseNumber(getEnterpriseNumber()));
+            int byteOffset = ENTERPRISE_NUM_LENGTH;
+            int payloadLength = valueBytes.length - byteOffset;
+            VendorPayload vendorPayload = null;
+            if (payloadLength > 0) {
+                byte[] payloadBytes = ByteArray.subByte(valueBytes, byteOffset, payloadLength);
+                vendorPayload = parseVendorPayload(payloadBytes);
+                if (vendorPayload != null) {
+                    vsTlvBuider.setVendorPayload(vendorPayload);
+                }
+            }
+        }
+        return vsTlvBuider.build();
+    }
+
+    abstract protected byte[] serializeVendorPayload(VendorPayload payload);
+
+    abstract protected long getEnterpriseNumber();
+
+    abstract protected VendorPayload parseVendorPayload(byte[] payloadBytes) throws PCEPDeserializerException;
+
+    protected static int getPadding(final int length, final int padding) {
+        return (padding - (length % padding)) % padding;
+    }
+}