BUG-64 : initial rewrite, XROSubobjectRegistry. 97/5597/6
authorDana Kutenicsova <dkutenic@cisco.com>
Tue, 11 Mar 2014 12:55:50 +0000 (13:55 +0100)
committerDana Kutenicsova <dkutenic@cisco.com>
Fri, 14 Mar 2014 10:07:32 +0000 (10:07 +0000)
Change-Id: I845831d51830433dd88a881b63e215baeef0a7c8
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/AbstractXROWithSubobjectsParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExcludeRouteObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/EROExplicitExclusionRouteSubobjectParser.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/PCEPExtensionConsumerContext.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectHandlerRegistry.java [deleted file]
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectRegistry.java [new file with mode: 0644]
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimplePCEPExtensionProviderContext.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectRegistry.java [moved from pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectHandlerRegistry.java with 60% similarity]

index 5360f6327f8361d8b01f152ba25c604b8c07b820..a38b08f21ff1b11a1afca583d822a0ce81808a74 100644 (file)
@@ -70,7 +70,7 @@ import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectHandlerRegistry;
 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.pojo.AbstractPCEPExtensionProviderActivator;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Close;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
@@ -159,7 +159,7 @@ public final class Activator extends AbstractPCEPExtensionProviderActivator {
                                org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.PathKeyCase.class,
                                new RROPathKey32SubobjectParser());
 
-               final XROSubobjectHandlerRegistry xroSubReg = context.getXROSubobjectHandlerRegistry();
+               final XROSubobjectRegistry xroSubReg = context.getXROSubobjectHandlerRegistry();
                context.registerXROSubobjectParser(XROIpv4PrefixSubobjectParser.TYPE, new XROIpv4PrefixSubobjectParser());
                context.registerXROSubobjectParser(XROIpv6PrefixSubobjectParser.TYPE, new XROIpv6PrefixSubobjectParser());
                context.registerXROSubobjectParser(XROAsNumberSubobjectParser.TYPE, new XROAsNumberSubobjectParser());
index cd2d0628ba26e3e6b00f7f72035978c466cd0a74..a7bee2d2c37e256cc6aed24b9c5358040de37f1f 100644 (file)
@@ -12,8 +12,7 @@ import java.util.List;
 import org.opendaylight.protocol.pcep.spi.ObjectParser;
 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
 import org.slf4j.Logger;
@@ -35,9 +34,9 @@ public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, O
        private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
        private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
 
-       private final XROSubobjectHandlerRegistry subobjReg;
+       private final XROSubobjectRegistry subobjReg;
 
-       protected AbstractXROWithSubobjectsParser(final XROSubobjectHandlerRegistry subobjReg) {
+       protected AbstractXROWithSubobjectsParser(final XROSubobjectRegistry subobjReg) {
                this.subobjReg = Preconditions.checkNotNull(subobjReg);
        }
 
@@ -70,31 +69,30 @@ public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, O
                        soContentsBytes = ByteArray.subByte(bytes, offset, length - SO_CONTENTS_OFFSET);
 
                        LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
-                       final Subobject sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes, mandatory);
-                       LOG.debug("Subobject was parsed. {}", sub);
-
-                       subs.add(sub);
-
+                       final Subobject sub = this.subobjReg.parseSubobject(type, soContentsBytes, mandatory);
+                       if (sub == null) {
+                               LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
+                       } else {
+                               LOG.debug("Subobject was parsed. {}", sub);
+                               subs.add(sub);
+                       }
                        offset += soContentsBytes.length;
                }
                return subs;
        }
 
        protected final byte[] serializeSubobject(final List<Subobject> subobjects) {
-
                final List<byte[]> result = Lists.newArrayList();
-
                int finalLength = 0;
-
                for (final Subobject subobject : subobjects) {
-
-                       final XROSubobjectSerializer serializer = this.subobjReg.getSubobjectSerializer(subobject.getSubobjectType());
-
-                       final byte[] bytes = serializer.serializeSubobject(subobject);
-                       finalLength += bytes.length;
-                       result.add(bytes);
+                       final byte[] bytes = this.subobjReg.serializeSubobject(subobject);
+                       if (bytes == null) {
+                               LOG.warn("Could not find serializer for subobject type: {}. Skipping subobject.", subobject.getSubobjectType());
+                       } else  {
+                               finalLength += bytes.length;
+                               result.add(bytes);
+                       }
                }
-
                final byte[] resultBytes = new byte[finalLength];
                int byteOffset = 0;
                for (final byte[] b : result) {
index 1ca2adf11b1bae8e8f3f4dacc4687ade5d8e1674..ce612b8e8ee996cfa36f88156dc7c126445925e0 100644 (file)
@@ -9,7 +9,7 @@ package org.opendaylight.protocol.pcep.impl.object;
 
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
 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;
@@ -30,7 +30,7 @@ public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobject
 
        private static final int FLAGS_OFFSET = 3;
 
-       public PCEPExcludeRouteObjectParser(final XROSubobjectHandlerRegistry registry) {
+       public PCEPExcludeRouteObjectParser(final XROSubobjectRegistry registry) {
                super(registry);
        }
 
index 8ddc26655d3f7d6b8bbf510a9cdcda0116ed6933..69d3ab57372755e97d6bf47b9c006a79539039bf 100644 (file)
@@ -13,9 +13,7 @@ import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
@@ -37,9 +35,9 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
        private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
        private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
 
-       private final XROSubobjectHandlerRegistry registry;
+       private final XROSubobjectRegistry registry;
 
-       public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectHandlerRegistry registry) {
+       public EROExplicitExclusionRouteSubobjectParser(final XROSubobjectRegistry registry) {
                this.registry = registry;
        }
 
@@ -109,9 +107,7 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
                        soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
                        System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
 
-                       final XROSubobjectParser parser = this.registry.getSubobjectParser(type);
-
-                       subs.add(parser.parseSubobject(soContentsBytes, mandatory));
+                       subs.add(this.registry.parseSubobject(type, soContentsBytes, mandatory));
 
                        offset += length;
                }
@@ -127,9 +123,7 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar
 
                for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject subobject : subobjects) {
 
-                       final XROSubobjectSerializer serializer = this.registry.getSubobjectSerializer(subobject.getSubobjectType());
-
-                       final byte[] bytes = serializer.serializeSubobject(subobject);
+                       final byte[] bytes = this.registry.serializeSubobject(subobject);
                        finalLength += bytes.length;
                        result.add(bytes);
                }
index 8a0f74d659343b9318a535b5a8dbbe72ba54991c..fa01fb8ce4d444148a01cc71994213ec56935065 100644 (file)
@@ -19,7 +19,7 @@ public interface PCEPExtensionConsumerContext {
 
        RROSubobjectHandlerRegistry getRROSubobjectHandlerRegistry();
 
-       XROSubobjectHandlerRegistry getXROSubobjectHandlerRegistry();
+       XROSubobjectRegistry getXROSubobjectHandlerRegistry();
 
        TlvHandlerRegistry getTlvHandlerRegistry();
 }
diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectHandlerRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectHandlerRegistry.java
deleted file mode 100644 (file)
index b8f08c0..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-/*
- * 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.spi;
-
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.SubobjectType;
-
-public interface XROSubobjectHandlerRegistry {
-       XROSubobjectParser getSubobjectParser(int subobjectType);
-       XROSubobjectSerializer getSubobjectSerializer(SubobjectType subobject);
-}
diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/XROSubobjectRegistry.java
new file mode 100644 (file)
index 0000000..69b0327
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * 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.spi;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
+
+public interface XROSubobjectRegistry {
+       /**
+        * Finds parser for given subobject type in the registry. Delegates parsing to found parser.
+        * @param type subobject type, key in parser registry
+        * @param buffer subobject raw binary value to be parsed
+        * @param mandatory XRO specific common field
+        * @return null if the parser for this subobject could not be found
+        * @throws PCEPDeserializerException if the parsing did not succeed
+        */
+       Subobject parseSubobject(final int type, final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException;
+
+       /**
+        * Find serializer for given subobject. Delegates parsing to found serializer.
+        * @param subobject to be parsed
+        * @return null if the serializer for this subobject could not be found
+        */
+       byte[] serializeSubobject(final Subobject subobject);
+}
index 2dd5cd0b47438d6cbb184c43146ef039d03e275e..a1d273451c22d52d75833fd083ffaf3583e707a5 100644 (file)
@@ -28,7 +28,7 @@ import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
@@ -47,7 +47,7 @@ public class SimplePCEPExtensionProviderContext implements PCEPExtensionProvider
        private final SimpleObjectHandlerRegistry objReg = new SimpleObjectHandlerRegistry();
        private final SimpleEROSubobjectHandlerRegistry eroSubReg = new SimpleEROSubobjectHandlerRegistry();
        private final SimpleRROSubobjectHandlerRegistry rroSubReg = new SimpleRROSubobjectHandlerRegistry();
-       private final SimpleXROSubobjectHandlerRegistry xroSubReg = new SimpleXROSubobjectHandlerRegistry();
+       private final SimpleXROSubobjectRegistry xroSubReg = new SimpleXROSubobjectRegistry();
        private final SimpleTlvHandlerRegistry tlvReg = new SimpleTlvHandlerRegistry();
 
        @Override
@@ -76,7 +76,7 @@ public class SimplePCEPExtensionProviderContext implements PCEPExtensionProvider
        }
 
        @Override
-       public final XROSubobjectHandlerRegistry getXROSubobjectHandlerRegistry() {
+       public final XROSubobjectRegistry getXROSubobjectHandlerRegistry() {
                return this.xroSubReg;
        }
 
similarity index 60%
rename from pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectHandlerRegistry.java
rename to pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleXROSubobjectRegistry.java
index a206bddc116721dd36050df23da3f324c1e0a6e0..85e4b3dc3031fb9f124df1dc9f0b730938ef4d44 100644 (file)
@@ -8,16 +8,18 @@
 package org.opendaylight.protocol.pcep.spi.pojo;
 
 import org.opendaylight.protocol.concepts.HandlerRegistry;
-import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
+import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
 import org.opendaylight.protocol.util.Values;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.SubobjectType;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 
 import com.google.common.base.Preconditions;
 
-public final class SimpleXROSubobjectHandlerRegistry implements XROSubobjectHandlerRegistry {
+public final class SimpleXROSubobjectRegistry implements XROSubobjectRegistry {
        private final HandlerRegistry<DataContainer, XROSubobjectParser, XROSubobjectSerializer> handlers = new HandlerRegistry<>();
 
        public AutoCloseable registerSubobjectParser(final int subobjectType, final XROSubobjectParser parser) {
@@ -31,13 +33,21 @@ public final class SimpleXROSubobjectHandlerRegistry implements XROSubobjectHand
        }
 
        @Override
-       public XROSubobjectParser getSubobjectParser(final int subobjectType) {
-               Preconditions.checkArgument(subobjectType >= 0 && subobjectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
-               return this.handlers.getParser(subobjectType);
+       public Subobject parseSubobject(int type, byte[] buffer, boolean mandatory) throws PCEPDeserializerException {
+               Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
+               final XROSubobjectParser parser = this.handlers.getParser(type);
+               if (parser == null) {
+                       return null;
+               }
+               return parser.parseSubobject(buffer, mandatory);
        }
 
        @Override
-       public XROSubobjectSerializer getSubobjectSerializer(final SubobjectType subobject) {
-               return this.handlers.getSerializer(subobject.getImplementedInterface());
+       public byte[] serializeSubobject(Subobject subobject) {
+               final XROSubobjectSerializer serializer = this.handlers.getSerializer(subobject.getSubobjectType().getImplementedInterface());
+               if (serializer == null) {
+                       return null;
+               }
+               return serializer.serializeSubobject(subobject);
        }
 }