BUG-130 : split Endpoints object parser. 06/3606/1
authorDana Kutenicsova <dkutenic@cisco.com>
Tue, 10 Dec 2013 12:44:09 +0000 (13:44 +0100)
committerDana Kutenicsova <dkutenic@cisco.com>
Tue, 10 Dec 2013 12:44:09 +0000 (13:44 +0100)
Change-Id: I596857ef76f7dd1c6537cceae629419bdc1faf39
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/Activator.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsIpv4ObjectParser.java [moved from pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsObjectParser.java with 58% similarity]
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsIpv6ObjectParser.java [new file with mode: 0644]
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPObjectParserTest.java

index 0e2ce6bb8df44cbee84e898f926a9a294e559710..9ae68c53f0ada24022c4cc3f6b54bca71e6337aa 100644 (file)
@@ -22,7 +22,8 @@ import org.opendaylight.protocol.pcep.impl.message.PCEPUpdateRequestMessageParse
 import org.opendaylight.protocol.pcep.impl.object.PCEPBandwidthObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPClassTypeObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPCloseObjectParser;
-import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsObjectParser;
+import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsIpv4ObjectParser;
+import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsIpv6ObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPErrorObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPExcludeRouteObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPExplicitRouteObjectParser;
@@ -234,9 +235,10 @@ public final class Activator extends AbstractPCEPExtensionProviderActivator {
                context.registerObjectParser(PCEPRequestParameterObjectParser.CLASS, PCEPRequestParameterObjectParser.TYPE,
                                new PCEPRequestParameterObjectParser(tlvReg));
                context.registerObjectParser(PCEPNoPathObjectParser.CLASS, PCEPNoPathObjectParser.TYPE, new PCEPNoPathObjectParser(tlvReg));
-               context.registerObjectParser(PCEPEndPointsObjectParser.CLASS, PCEPEndPointsObjectParser.TYPE, new PCEPEndPointsObjectParser(tlvReg));
-               context.registerObjectParser(PCEPEndPointsObjectParser.CLASS_6, PCEPEndPointsObjectParser.TYPE_6,
-                               new PCEPEndPointsObjectParser(tlvReg));
+               context.registerObjectParser(PCEPEndPointsIpv4ObjectParser.CLASS, PCEPEndPointsIpv4ObjectParser.TYPE,
+                               new PCEPEndPointsIpv4ObjectParser(tlvReg));
+               context.registerObjectParser(PCEPEndPointsIpv6ObjectParser.CLASS, PCEPEndPointsIpv6ObjectParser.TYPE,
+                               new PCEPEndPointsIpv4ObjectParser(tlvReg));
                context.registerObjectParser(PCEPBandwidthObjectParser.CLASS, PCEPBandwidthObjectParser.TYPE, new PCEPBandwidthObjectParser(tlvReg));
                context.registerObjectParser(PCEPBandwidthObjectParser.E_CLASS, PCEPBandwidthObjectParser.E_TYPE,
                                new PCEPBandwidthObjectParser(tlvReg));
@@ -271,7 +273,7 @@ public final class Activator extends AbstractPCEPExtensionProviderActivator {
                context.registerObjectSerializer(Open.class, new PCEPOpenObjectParser(tlvReg));
                context.registerObjectSerializer(Rp.class, new PCEPRequestParameterObjectParser(tlvReg));
                context.registerObjectSerializer(NoPath.class, new PCEPNoPathObjectParser(tlvReg));
-               context.registerObjectSerializer(EndpointsObj.class, new PCEPEndPointsObjectParser(tlvReg));
+               context.registerObjectSerializer(EndpointsObj.class, new PCEPEndPointsIpv4ObjectParser(tlvReg));
                context.registerObjectSerializer(Bandwidth.class, new PCEPBandwidthObjectParser(tlvReg));
                context.registerObjectSerializer(Metric.class, new PCEPMetricObjectParser(tlvReg));
                context.registerObjectSerializer(Ero.class, new PCEPExplicitRouteObjectParser(eroSubReg));
similarity index 58%
rename from pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsObjectParser.java
rename to pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsIpv4ObjectParser.java
index 2eb58cefe134b8f38702d9936d12af49309fc2a9..63c0811f4e6469d1a3cac165fc4e4bf239342799 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.protocol.pcep.impl.object;
 
 import org.opendaylight.protocol.concepts.Ipv4Util;
-import org.opendaylight.protocol.concepts.Ipv6Util;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
@@ -20,10 +19,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
 import org.slf4j.Logger;
@@ -32,16 +28,13 @@ import org.slf4j.LoggerFactory;
 /**
  * Parser for IPv4 {@link EndpointsObj}
  */
-public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
+public class PCEPEndPointsIpv4ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
 
-       private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsObjectParser.class);
+       private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
 
        public static final int CLASS = 4;
        public static final int TYPE = 1;
 
-       public static final int CLASS_6 = 4;
-       public static final int TYPE_6 = 2;
-
        /*
         * fields lengths and offsets for IPv4 in bytes
         */
@@ -51,13 +44,7 @@ public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<Endp
        private static final int SRC4_F_OFFSET = 0;
        private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
 
-       private static final int SRC6_F_LENGTH = 16;
-       private static final int DEST6_F_LENGTH = 16;
-
-       private static final int SRC6_F_OFFSET = 0;
-       private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
-
-       public PCEPEndPointsObjectParser(final TlvHandlerRegistry tlvReg) {
+       public PCEPEndPointsIpv4ObjectParser(final TlvHandlerRegistry tlvReg) {
                super(tlvReg);
        }
 
@@ -69,24 +56,17 @@ public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<Endp
                final EndpointsObjBuilder builder = new EndpointsObjBuilder();
                builder.setIgnore(header.isIgnore());
                builder.setProcessingRule(header.isProcessingRule());
-
-               if (bytes.length == SRC4_F_LENGTH + DEST4_F_LENGTH) {
-                       final Ipv4Builder b = new Ipv4Builder();
-                       b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
-                       b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
-                       builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
-               } else if (bytes.length == SRC6_F_LENGTH + DEST6_F_LENGTH) {
-                       final Ipv6Builder b = new Ipv6Builder();
-                       b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
-                       b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
-                       builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(b.build()).build());
-               } else {
-                       throw new PCEPDeserializerException("Wrong length of array of bytes.");
-               }
                if (!builder.isProcessingRule()) {
                        LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
                        return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
                }
+               if (bytes.length != SRC4_F_LENGTH + DEST4_F_LENGTH) {
+                       throw new PCEPDeserializerException("Wrong length of array of bytes.");
+               }
+               final Ipv4Builder b = new Ipv4Builder();
+               b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
+               b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
+               builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
                return builder.build();
        }
 
@@ -100,26 +80,17 @@ public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<Endp
                if (!(object instanceof EndpointsObj)) {
                        throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
                }
-
                final EndpointsObj ePObj = (EndpointsObj) object;
 
                final AddressFamily afi = ePObj.getAddressFamily();
 
-               if (afi instanceof Ipv4Case) {
-                       final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
-                       ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
-                       ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes,
-                                       DEST4_F_OFFSET);
-                       return retBytes;
-               } else if (afi instanceof Ipv6Case) {
-                       final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
-                       ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
-                       ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()), retBytes,
-                                       DEST6_F_OFFSET);
-                       return retBytes;
-               } else {
+               if (!(afi instanceof Ipv4Case)) {
                        throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
                }
+               final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
+               ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
+               ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
+               return retBytes;
        }
 
        @Override
@@ -131,12 +102,4 @@ public class PCEPEndPointsObjectParser extends AbstractObjectWithTlvsParser<Endp
        public int getObjectClass() {
                return CLASS;
        }
-
-       public int get6ObjectType() {
-               return TYPE_6;
-       }
-
-       public int get6ObjectClass() {
-               return CLASS_6;
-       }
 }
diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsIpv6ObjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPEndPointsIpv6ObjectParser.java
new file mode 100644 (file)
index 0000000..3ab96d2
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2013 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.impl.object;
+
+import org.opendaylight.protocol.concepts.Ipv6Util;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.PCEPErrors;
+import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.UnknownObject;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
+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.endpoints.AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Parser for IPv6 {@link EndpointsObj}
+ */
+public class PCEPEndPointsIpv6ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
+
+       private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
+
+       public static final int CLASS = 4;
+       public static final int TYPE = 2;
+
+       /*
+        * fields lengths and offsets for IPv4 in bytes
+        */
+       private static final int SRC6_F_LENGTH = 16;
+       private static final int DEST6_F_LENGTH = 16;
+
+       private static final int SRC6_F_OFFSET = 0;
+       private static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
+
+       public PCEPEndPointsIpv6ObjectParser(final TlvHandlerRegistry tlvReg) {
+               super(tlvReg);
+       }
+
+       @Override
+       public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
+               if (bytes == null) {
+                       throw new IllegalArgumentException("Array of bytes is mandatory");
+               }
+               final EndpointsObjBuilder builder = new EndpointsObjBuilder();
+               builder.setIgnore(header.isIgnore());
+               builder.setProcessingRule(header.isProcessingRule());
+               if (!builder.isProcessingRule()) {
+                       LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
+                       return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
+               }
+               if (bytes.length != SRC6_F_LENGTH + DEST6_F_LENGTH) {
+                       throw new PCEPDeserializerException("Wrong length of array of bytes.");
+               }
+               final Ipv6Builder b = new Ipv6Builder();
+               b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)));
+               b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGTH))));
+               builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(b.build()).build());
+               return builder.build();
+       }
+
+       @Override
+       public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
+               // No tlvs defined
+       }
+
+       @Override
+       public byte[] serializeObject(final Object object) {
+               if (!(object instanceof EndpointsObj)) {
+                       throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
+               }
+               final EndpointsObj ePObj = (EndpointsObj) object;
+
+               final AddressFamily afi = ePObj.getAddressFamily();
+
+               if (!(afi instanceof Ipv6Case)) {
+                       throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
+               }
+               final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGTH];
+               ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getSourceIpv6Address()), retBytes, SRC6_F_OFFSET);
+               ByteArray.copyWhole(Ipv6Util.bytesForAddress((((Ipv6Case) afi).getIpv6()).getDestinationIpv6Address()), retBytes, DEST6_F_OFFSET);
+               return retBytes;
+       }
+
+       @Override
+       public int getObjectType() {
+               return TYPE;
+       }
+
+       @Override
+       public int getObjectClass() {
+               return CLASS;
+       }
+}
index 2df1f5a9019aa4e0e4ac7448f615f17d1ce029d4..d6ee915263b65f5f8ed64a90def19f052e4c992f 100644 (file)
@@ -23,7 +23,8 @@ import org.opendaylight.protocol.pcep.impl.message.ObjectHeaderImpl;
 import org.opendaylight.protocol.pcep.impl.object.PCEPBandwidthObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPClassTypeObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPCloseObjectParser;
-import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsObjectParser;
+import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsIpv4ObjectParser;
+import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsIpv6ObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPErrorObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPExcludeRouteObjectParser;
 import org.opendaylight.protocol.pcep.impl.object.PCEPExplicitRouteObjectParser;
@@ -319,7 +320,7 @@ public class PCEPObjectParserTest {
                final byte[] srcIPBytes = { (byte) 0xA2, (byte) 0xF5, (byte) 0x11, (byte) 0x0E };
                final byte[] destIPBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
 
-               final PCEPEndPointsObjectParser parser = new PCEPEndPointsObjectParser(this.tlvRegistry);
+               final PCEPEndPointsIpv4ObjectParser parser = new PCEPEndPointsIpv4ObjectParser(this.tlvRegistry);
                final byte[] result = ByteArray.fileToBytes("src/test/resources/PCEPEndPointsObject1IPv4.bin");
 
                final EndpointsObjBuilder builder = new EndpointsObjBuilder();
@@ -341,7 +342,7 @@ public class PCEPObjectParserTest {
                final byte[] srcIPBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
 
-               final PCEPEndPointsObjectParser parser = new PCEPEndPointsObjectParser(this.tlvRegistry);
+               final PCEPEndPointsIpv6ObjectParser parser = new PCEPEndPointsIpv6ObjectParser(this.tlvRegistry);
                final byte[] result = ByteArray.fileToBytes("src/test/resources/PCEPEndPointsObject2IPv6.bin");
 
                final EndpointsObjBuilder builder = new EndpointsObjBuilder();