Move ObjectCodec to its own file 48/100548/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Apr 2022 14:54:08 +0000 (16:54 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Apr 2022 16:08:46 +0000 (18:08 +0200)
This the beef of RestCodec, make sure it is a top-level construct.

JIRA: NETCONF-871
Change-Id: I314cc3f91ca3c5e7e2d4e6d7310507541e1cd843
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/codecs/ObjectCodec.java [new file with mode: 0644]
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/codecs/RestCodec.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/YangInstanceIdentifierDeserializer.java

diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/codecs/ObjectCodec.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/codecs/ObjectCodec.java
new file mode 100644 (file)
index 0000000..deb48d4
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * 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.restconf.nb.rfc8040.codecs;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.restconf.common.util.IdentityValuesDTO;
+import org.opendaylight.restconf.common.util.RestUtil;
+import org.opendaylight.restconf.nb.rfc8040.codecs.RestCodec.IdentityrefCodecImpl;
+import org.opendaylight.restconf.nb.rfc8040.codecs.RestCodec.InstanceIdentifierCodecImpl;
+import org.opendaylight.restconf.nb.rfc8040.codecs.RestCodec.LeafrefCodecImpl;
+import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
+import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@SuppressWarnings("rawtypes")
+// FIXME: IllegalArgumentCodec is not quite accurate
+public final class ObjectCodec implements IllegalArgumentCodec<Object, Object> {
+    private static final Logger LOG = LoggerFactory.getLogger(ObjectCodec.class);
+    private static final IllegalArgumentCodec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
+
+    private final IllegalArgumentCodec instanceIdentifier;
+    private final IllegalArgumentCodec identityrefCodec;
+    private final EffectiveModelContext schemaContext;
+    private final TypeDefinition<?> type;
+
+    private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
+            final EffectiveModelContext schemaContext) {
+        this.schemaContext = schemaContext;
+        type = RestUtil.resolveBaseTypeFrom(typeDefinition);
+        if (type instanceof IdentityrefTypeDefinition) {
+            identityrefCodec = new IdentityrefCodecImpl(mountPoint, schemaContext);
+        } else {
+            identityrefCodec = null;
+        }
+        if (type instanceof InstanceIdentifierTypeDefinition) {
+            instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint, schemaContext);
+        } else {
+            instanceIdentifier = null;
+        }
+    }
+
+    public static ObjectCodec of(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
+            final EffectiveModelContext schemaContext) {
+        return new ObjectCodec(typeDefinition, mountPoint, schemaContext);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Legacy returns")
+    public Object deserialize(final Object input) {
+        try {
+            if (type instanceof IdentityrefTypeDefinition) {
+                if (input instanceof IdentityValuesDTO) {
+                    return identityrefCodec.deserialize(input);
+                }
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug(
+                        "Value is not instance of IdentityrefTypeDefinition but is {}. "
+                                + "Therefore NULL is used as translation of - {}",
+                        input == null ? "null" : input.getClass(), String.valueOf(input));
+                }
+                // FIXME: this should be a hard error
+                return null;
+            } else if (type instanceof InstanceIdentifierTypeDefinition) {
+                return input instanceof IdentityValuesDTO ? instanceIdentifier.deserialize(input)
+                    // FIXME: what is it that we are trying to decode here and why?
+                    : new StringModuleInstanceIdentifierCodec(schemaContext).deserialize((String) input);
+            } else {
+                final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec =
+                        TypeDefinitionAwareCodec.from(type);
+                if (typeAwarecodec != null) {
+                    if (input instanceof IdentityValuesDTO) {
+                        return typeAwarecodec.deserialize(((IdentityValuesDTO) input).getOriginValue());
+                    }
+                    return typeAwarecodec.deserialize(String.valueOf(input));
+                } else {
+                    // FIXME: this should be a hard error
+                    LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
+                    return null;
+                }
+            }
+        } catch (final ClassCastException e) {
+            // FIXME: remove this catch when everyone use codecs
+            // FIXME: this should be a hard error
+            LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
+            return null;
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Legacy returns")
+    public Object serialize(final Object input) {
+        try {
+            if (type instanceof IdentityrefTypeDefinition) {
+                return identityrefCodec.serialize(input);
+            } else if (type instanceof LeafrefTypeDefinition) {
+                return LEAFREF_DEFAULT_CODEC.serialize(input);
+            } else if (type instanceof InstanceIdentifierTypeDefinition) {
+                return instanceIdentifier.serialize(input);
+            } else {
+                final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec =
+                        TypeDefinitionAwareCodec.from(type);
+                if (typeAwarecodec != null) {
+                    return typeAwarecodec.serialize(input);
+                } else {
+                    // FIXME: this needs to be a hard error
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
+                    }
+                    return null;
+                }
+            }
+        } catch (final ClassCastException e) {
+            // FIXME: remove this catch when everyone use codecs
+            // FIXME: this needs to be a hard error
+            LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
+            return input;
+        }
+    }
+}
\ No newline at end of file
index 9d585bcbf4b34aabab64def32c4290a12dbb2a3e..40900eef50ea4861544f8c9e0677b09e1916f961 100644 (file)
@@ -24,8 +24,6 @@ import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.common.util.IdentityValuesDTO;
 import org.opendaylight.restconf.common.util.IdentityValuesDTO.IdentityValue;
 import org.opendaylight.restconf.common.util.IdentityValuesDTO.Predicate;
-import org.opendaylight.restconf.common.util.RestUtil;
-import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -36,7 +34,6 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
 import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec;
 import org.opendaylight.yangtools.yang.data.api.codec.InstanceIdentifierCodec;
 import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec;
-import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
@@ -49,10 +46,6 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,124 +57,11 @@ public final class RestCodec {
         // Hidden on purpose
     }
 
-    // FIXME: IllegalArgumentCodec is not quite accurate
-    public static IllegalArgumentCodec<Object, Object> from(final TypeDefinition<?> typeDefinition,
-            final DOMMountPoint mountPoint, final EffectiveModelContext schemaContext) {
-        return new ObjectCodec(typeDefinition, mountPoint, schemaContext);
-    }
-
-    @SuppressWarnings("rawtypes")
-    public static final class ObjectCodec implements IllegalArgumentCodec<Object, Object> {
-
-        private static final Logger LOG = LoggerFactory.getLogger(ObjectCodec.class);
-
-        public static final IllegalArgumentCodec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
-        private final IllegalArgumentCodec instanceIdentifier;
-        private final IllegalArgumentCodec identityrefCodec;
-
-        private final TypeDefinition<?> type;
-
-        private final EffectiveModelContext schemaContext;
-
-        private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
-                final EffectiveModelContext schemaContext) {
-            this.schemaContext = schemaContext;
-            type = RestUtil.resolveBaseTypeFrom(typeDefinition);
-            if (type instanceof IdentityrefTypeDefinition) {
-                identityrefCodec = new IdentityrefCodecImpl(mountPoint, schemaContext);
-            } else {
-                identityrefCodec = null;
-            }
-            if (type instanceof InstanceIdentifierTypeDefinition) {
-                instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint, schemaContext);
-            } else {
-                instanceIdentifier = null;
-            }
-        }
-
-        @SuppressWarnings("unchecked")
-        @Override
-        @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Legacy returns")
-        public Object deserialize(final Object input) {
-            try {
-                if (type instanceof IdentityrefTypeDefinition) {
-                    if (input instanceof IdentityValuesDTO) {
-                        return identityrefCodec.deserialize(input);
-                    }
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug(
-                            "Value is not instance of IdentityrefTypeDefinition but is {}. "
-                                    + "Therefore NULL is used as translation of - {}",
-                            input == null ? "null" : input.getClass(), String.valueOf(input));
-                    }
-                    // FIXME: this should be a hard error
-                    return null;
-                } else if (type instanceof InstanceIdentifierTypeDefinition) {
-                    return input instanceof IdentityValuesDTO ? instanceIdentifier.deserialize(input)
-                        // FIXME: what is it that we are trying to decode here and why?
-                        : new StringModuleInstanceIdentifierCodec(schemaContext).deserialize((String) input);
-                } else {
-                    final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec =
-                            TypeDefinitionAwareCodec.from(type);
-                    if (typeAwarecodec != null) {
-                        if (input instanceof IdentityValuesDTO) {
-                            return typeAwarecodec.deserialize(((IdentityValuesDTO) input).getOriginValue());
-                        }
-                        return typeAwarecodec.deserialize(String.valueOf(input));
-                    } else {
-                        // FIXME: this should be a hard error
-                        LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
-                        return null;
-                    }
-                }
-            } catch (final ClassCastException e) {
-                // FIXME: remove this catch when everyone use codecs
-                // FIXME: this should be a hard error
-                LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
-                return null;
-            }
-        }
-
-        @SuppressWarnings("unchecked")
-        @Override
-        @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Legacy returns")
-        public Object serialize(final Object input) {
-            try {
-                if (type instanceof IdentityrefTypeDefinition) {
-                    return identityrefCodec.serialize(input);
-                } else if (type instanceof LeafrefTypeDefinition) {
-                    return LEAFREF_DEFAULT_CODEC.serialize(input);
-                } else if (type instanceof InstanceIdentifierTypeDefinition) {
-                    return instanceIdentifier.serialize(input);
-                } else {
-                    final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec =
-                            TypeDefinitionAwareCodec.from(type);
-                    if (typeAwarecodec != null) {
-                        return typeAwarecodec.serialize(input);
-                    } else {
-                        // FIXME: this needs to be a hard error
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
-                        }
-                        return null;
-                    }
-                }
-            } catch (final ClassCastException e) {
-                // FIXME: remove this catch when everyone use codecs
-                // FIXME: this needs to be a hard error
-                LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
-                return input;
-            }
-        }
-    }
-
     public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
-
         private static final Logger LOG = LoggerFactory.getLogger(IdentityrefCodecImpl.class);
 
-        private final DOMMountPoint mountPoint;
-
         private final SchemaContext schemaContext;
+        private final DOMMountPoint mountPoint;
 
         public IdentityrefCodecImpl(final DOMMountPoint mountPoint, final SchemaContext schemaContext) {
             this.mountPoint = mountPoint;
@@ -292,35 +172,33 @@ public final class RestCodec {
                 PathArgument pathArgument = null;
                 if (identityValue.getPredicates().isEmpty()) {
                     pathArgument = new NodeIdentifier(qName);
-                } else {
-                    if (node instanceof LeafListSchemaNode) { // predicate is value of leaf-list entry
-                        final Predicate leafListPredicate = identityValue.getPredicates().get(0);
-                        // FIXME: this needs to be a hard error
-                        if (!leafListPredicate.isLeafList()) {
-                            LOG.info("Predicate's data is not type of leaf-list. It should be in format \".='value'\"");
-                            LOG.info("Instance-identifier will be translated as NULL for data - {}",
-                                    String.valueOf(identityValue.getValue()));
-                            return null;
-                        }
-                        pathArgument = new NodeWithValue<>(qName, leafListPredicate.getValue());
-                    } else if (node instanceof ListSchemaNode) { // predicates are keys of list
-                        final DataNodeContainer listNode = (DataNodeContainer) node;
-                        final Map<QName, Object> predicatesMap = new HashMap<>();
-                        for (final Predicate predicate : identityValue.getPredicates()) {
-                            validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), mountPoint,
-                                    schemaContext);
-                            final DataSchemaNode listKey = findInstanceDataChildByNameAndNamespace(listNode,
-                                    predicate.getName().getValue(), validNamespace);
-                            predicatesMap.put(listKey.getQName(), predicate.getValue());
-                        }
-                        pathArgument = NodeIdentifierWithPredicates.of(qName, predicatesMap);
-                    } else {
-                        // FIXME: this needs to be a hard error
-                        LOG.info("Node {} is not List or Leaf-list.", node);
+                } else if (node instanceof LeafListSchemaNode) { // predicate is value of leaf-list entry
+                    final Predicate leafListPredicate = identityValue.getPredicates().get(0);
+                    // FIXME: this needs to be a hard error
+                    if (!leafListPredicate.isLeafList()) {
+                        LOG.info("Predicate's data is not type of leaf-list. It should be in format \".='value'\"");
                         LOG.info("Instance-identifier will be translated as NULL for data - {}",
                                 String.valueOf(identityValue.getValue()));
                         return null;
                     }
+                    pathArgument = new NodeWithValue<>(qName, leafListPredicate.getValue());
+                } else if (node instanceof ListSchemaNode) { // predicates are keys of list
+                    final DataNodeContainer listNode = (DataNodeContainer) node;
+                    final Map<QName, Object> predicatesMap = new HashMap<>();
+                    for (final Predicate predicate : identityValue.getPredicates()) {
+                        validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), mountPoint,
+                                schemaContext);
+                        final DataSchemaNode listKey = findInstanceDataChildByNameAndNamespace(listNode,
+                                predicate.getName().getValue(), validNamespace);
+                        predicatesMap.put(listKey.getQName(), predicate.getValue());
+                    }
+                    pathArgument = NodeIdentifierWithPredicates.of(qName, predicatesMap);
+                } else {
+                    // FIXME: this needs to be a hard error
+                    LOG.info("Node {} is not List or Leaf-list.", node);
+                    LOG.info("Instance-identifier will be translated as NULL for data - {}",
+                            String.valueOf(identityValue.getValue()));
+                    return null;
                 }
                 result.add(pathArgument);
                 if (i < identities.size() - 1) { // last element in instance-identifier can be other than
index 19d4e7ebdfdc234ea6a2e882aa22476979671273..602d204f922b33abca0a5a6e945f462bf766e755 100644 (file)
@@ -20,7 +20,7 @@ import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.util.RestUtil;
 import org.opendaylight.restconf.nb.rfc8040.ApiPath;
 import org.opendaylight.restconf.nb.rfc8040.ApiPath.ListInstance;
-import org.opendaylight.restconf.nb.rfc8040.codecs.RestCodec;
+import org.opendaylight.restconf.nb.rfc8040.codecs.ObjectCodec;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -269,7 +269,7 @@ public final class YangInstanceIdentifierDeserializer {
             return toIdentityrefQName(value, schemaNode);
         }
         try {
-            return RestCodec.from(typedef, null, schemaContext).deserialize(value);
+            return ObjectCodec.of(typedef, null, schemaContext).deserialize(value);
         } catch (IllegalArgumentException e) {
             throw new RestconfDocumentedException("Invalid value '" + value + "' for " + schemaNode.getQName(),
                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);