BUG-64 : initial rewrite, EROSubobjectRegistry. 42/5642/2
authorDana Kutenicsova <dkutenic@cisco.com>
Fri, 14 Mar 2014 10:37:24 +0000 (11:37 +0100)
committerRobert Varga <rovarga@cisco.com>
Sat, 15 Mar 2014 11:18:00 +0000 (11:18 +0000)
Change-Id: I3d8e8f6d23ae112c5f4cc045030fb71f0a4d5316
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/AbstractEROWithSubobjectsParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPExplicitRouteObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPIncludeRouteObjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPPathKeyObjectParser.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectHandlerRegistry.java [deleted file]
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectRegistry.java [new file with mode: 0644]
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/PCEPExtensionConsumerContext.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleEROSubobjectRegistry.java [moved from pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleEROSubobjectHandlerRegistry.java with 60% similarity]
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimplePCEPExtensionProviderContext.java

index 488bc24c370b55353c7d28bb84020f312ce8eb99..fb1ba57facd2afa70f89b0c5809518f9542b8c67 100644 (file)
@@ -64,7 +64,7 @@ import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
 import org.opendaylight.protocol.pcep.impl.tlv.OrderTlvParser;
 import org.opendaylight.protocol.pcep.impl.tlv.OverloadedDurationTlvParser;
 import org.opendaylight.protocol.pcep.impl.tlv.ReqMissingTlvParser;
-import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.LabelHandlerRegistry;
 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
@@ -123,7 +123,7 @@ public final class Activator extends AbstractPCEPExtensionProviderActivator {
                context.registerLabelSerializer(GeneralizedLabelCase.class, new GeneralizedLabelParser());
                context.registerLabelSerializer(WavebandSwitchingLabelCase.class, new WavebandSwitchingLabelParser());
 
-               final EROSubobjectHandlerRegistry eroSubReg = context.getEROSubobjectHandlerRegistry();
+               final EROSubobjectRegistry eroSubReg = context.getEROSubobjectHandlerRegistry();
                context.registerEROSubobjectParser(EROIpv4PrefixSubobjectParser.TYPE, new EROIpv4PrefixSubobjectParser());
                context.registerEROSubobjectParser(EROIpv6PrefixSubobjectParser.TYPE, new EROIpv6PrefixSubobjectParser());
                context.registerEROSubobjectParser(EROAsNumberSubobjectParser.TYPE, new EROAsNumberSubobjectParser());
index 324e9f40e3934c0695ee7fe9af6cbe9a20df40d0..5fd58c8f826b41a431bb2e235af2e34a83fc2023 100644 (file)
@@ -9,8 +9,7 @@ package org.opendaylight.protocol.pcep.impl.object;
 
 import java.util.List;
 
-import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
-import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.ObjectParser;
 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
@@ -33,9 +32,9 @@ public abstract class AbstractEROWithSubobjectsParser 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 EROSubobjectHandlerRegistry subobjReg;
+       private final EROSubobjectRegistry subobjReg;
 
-       protected AbstractEROWithSubobjectsParser(final EROSubobjectHandlerRegistry subobjReg) {
+       protected AbstractEROWithSubobjectsParser(final EROSubobjectRegistry subobjReg) {
                this.subobjReg = Preconditions.checkNotNull(subobjReg);
        }
 
@@ -68,32 +67,30 @@ public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, O
                        System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
 
                        LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
-                       final Subobject sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes, loose);
-                       LOG.debug("Subobject was parsed. {}", sub);
-
-                       subs.add(sub);
-
+                       final Subobject sub = this.subobjReg.parseSubobject(type, soContentsBytes, loose);
+                       if (sub == null) {
+                               LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
+                       } else {
+                               LOG.debug("Subobject was parsed. {}", sub);
+                               subs.add(sub);
+                       }
                        offset += 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 EROSubobjectSerializer 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 bec1f4c5f59d642c6a21765ebba5772d8c41f2dd..550b2730367e0761bb2b73bcffff44821f2a2373 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
-import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
@@ -24,7 +24,7 @@ public class PCEPExplicitRouteObjectParser extends AbstractEROWithSubobjectsPars
 
        public static final int TYPE = 1;
 
-       public PCEPExplicitRouteObjectParser(final EROSubobjectHandlerRegistry subobjReg) {
+       public PCEPExplicitRouteObjectParser(final EROSubobjectRegistry subobjReg) {
                super(subobjReg);
        }
 
index 47950d0e6307e0169e2d83a35fcc0f3150dea601..da537b6717c473f3d24ca6f41d4b8f6d838d85fc 100644 (file)
@@ -9,7 +9,7 @@ package org.opendaylight.protocol.pcep.impl.object;
 
 import java.util.List;
 
-import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
@@ -30,7 +30,7 @@ public class PCEPIncludeRouteObjectParser extends AbstractEROWithSubobjectsParse
 
        public static final int TYPE = 1;
 
-       public PCEPIncludeRouteObjectParser(final EROSubobjectHandlerRegistry subobjReg) {
+       public PCEPIncludeRouteObjectParser(final EROSubobjectRegistry subobjReg) {
                super(subobjReg);
        }
 
index 59a5327604d31a7290b555c21e06255db8f8d5f1..10a9c54235f097d9382a4a72ab2e30c2581986a3 100644 (file)
@@ -9,7 +9,7 @@ package org.opendaylight.protocol.pcep.impl.object;
 
 import java.util.List;
 
-import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
@@ -32,7 +32,7 @@ public class PCEPPathKeyObjectParser extends AbstractEROWithSubobjectsParser {
 
        public static final int TYPE = 1;
 
-       public PCEPPathKeyObjectParser(final EROSubobjectHandlerRegistry subReg) {
+       public PCEPPathKeyObjectParser(final EROSubobjectRegistry subReg) {
                super(subReg);
        }
 
diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectHandlerRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectHandlerRegistry.java
deleted file mode 100644 (file)
index f70c72a..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 EROSubobjectHandlerRegistry {
-       EROSubobjectParser getSubobjectParser(int subobjectType);
-       EROSubobjectSerializer getSubobjectSerializer(SubobjectType subobject);
-}
diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectRegistry.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/EROSubobjectRegistry.java
new file mode 100644 (file)
index 0000000..bc97b26
--- /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.explicit.route.object.ero.Subobject;
+
+public interface EROSubobjectRegistry {
+       /**
+        * 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 loose ERO 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 subobjectType, final byte[] buffer, final boolean loose) 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 35debc856d7dcd9f2d178c38f835c7d4ea2ff6d9..b59452d47238d7ec84bfc80ac731ed4159d0ba7b 100644 (file)
@@ -15,7 +15,7 @@ public interface PCEPExtensionConsumerContext {
 
        ObjectHandlerRegistry getObjectHandlerRegistry();
 
-       EROSubobjectHandlerRegistry getEROSubobjectHandlerRegistry();
+       EROSubobjectRegistry getEROSubobjectHandlerRegistry();
 
        RROSubobjectRegistry getRROSubobjectHandlerRegistry();
 
similarity index 60%
rename from pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleEROSubobjectHandlerRegistry.java
rename to pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/pojo/SimpleEROSubobjectRegistry.java
index 29950954671791874c6e14a9740f61194113016c..cd042c5472800b871c5752eaf822d9ca7c59e13f 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.EROSubobjectHandlerRegistry;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.util.Values;
+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.rsvp.rev130820.basic.explicit.route.subobjects.SubobjectType;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 
 import com.google.common.base.Preconditions;
 
-public final class SimpleEROSubobjectHandlerRegistry implements EROSubobjectHandlerRegistry {
+public final class SimpleEROSubobjectRegistry implements EROSubobjectRegistry {
        private final HandlerRegistry<DataContainer, EROSubobjectParser, EROSubobjectSerializer> handlers = new HandlerRegistry<>();
 
        public AutoCloseable registerSubobjectParser(final int subobjectType, final EROSubobjectParser parser) {
@@ -31,13 +33,21 @@ public final class SimpleEROSubobjectHandlerRegistry implements EROSubobjectHand
        }
 
        @Override
-       public EROSubobjectParser 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 loose) throws PCEPDeserializerException {
+               Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
+               final EROSubobjectParser parser = this.handlers.getParser(type);
+               if (parser == null) {
+                       return null;
+               }
+               return parser.parseSubobject(buffer, loose);
        }
 
        @Override
-       public EROSubobjectSerializer getSubobjectSerializer(final SubobjectType subobject) {
-               return this.handlers.getSerializer(subobject.getImplementedInterface());
+       public byte[] serializeSubobject(Subobject subobject) {
+               final EROSubobjectSerializer serializer = this.handlers.getSerializer(subobject.getSubobjectType().getImplementedInterface());
+               if (serializer == null) {
+                       return null;
+               }
+               return serializer.serializeSubobject(subobject);
        }
 }
index e1be2c5889587553faee538303d1b32345c6b9d9..27fa7be5c912e542e3f862365130a8843c53d3af 100644 (file)
@@ -9,7 +9,7 @@ package org.opendaylight.protocol.pcep.spi.pojo;
 
 import javax.annotation.concurrent.ThreadSafe;
 
-import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
 import org.opendaylight.protocol.pcep.spi.LabelHandlerRegistry;
@@ -45,7 +45,7 @@ public class SimplePCEPExtensionProviderContext implements PCEPExtensionProvider
        private final SimpleLabelHandlerRegistry labelReg = new SimpleLabelHandlerRegistry();
        private final SimpleMessageHandlerRegistry msgReg = new SimpleMessageHandlerRegistry();
        private final SimpleObjectHandlerRegistry objReg = new SimpleObjectHandlerRegistry();
-       private final SimpleEROSubobjectHandlerRegistry eroSubReg = new SimpleEROSubobjectHandlerRegistry();
+       private final SimpleEROSubobjectRegistry eroSubReg = new SimpleEROSubobjectRegistry();
        private final SimpleRROSubobjectRegistry rroSubReg = new SimpleRROSubobjectRegistry();
        private final SimpleXROSubobjectRegistry xroSubReg = new SimpleXROSubobjectRegistry();
        private final SimpleTlvRegistry tlvReg = new SimpleTlvRegistry();
@@ -66,7 +66,7 @@ public class SimplePCEPExtensionProviderContext implements PCEPExtensionProvider
        }
 
        @Override
-       public final EROSubobjectHandlerRegistry getEROSubobjectHandlerRegistry() {
+       public final EROSubobjectRegistry getEROSubobjectHandlerRegistry() {
                return this.eroSubReg;
        }