Remove IdentityrefCodecImpl and rename ObjectCodec 53/100553/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Apr 2022 15:28:28 +0000 (17:28 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Apr 2022 16:08:46 +0000 (18:08 +0200)
IdentityrefCodecImpl is not referenced anywhere and removing it makes
RestCodec empty. Move ObjectCodec into its place instead and squash it
to a single static method.

JIRA: NETCONF-871
Change-Id: Idbb072c1dcfbeebfd80cdc73751adf81ad9816f5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/codecs/ObjectCodec.java [deleted file]
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
deleted file mode 100644 (file)
index 97fadfa..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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 static java.util.Objects.requireNonNull;
-
-import org.opendaylight.restconf.common.util.RestUtil;
-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.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public final class ObjectCodec {
-    private static final Logger LOG = LoggerFactory.getLogger(ObjectCodec.class);
-
-    private final EffectiveModelContext schemaContext;
-    private final TypeDefinition<?> type;
-
-    private ObjectCodec(final EffectiveModelContext schemaContext, final TypeDefinition<?> typeDefinition) {
-        this.schemaContext = requireNonNull(schemaContext);
-        type = RestUtil.resolveBaseTypeFrom(typeDefinition);
-    }
-
-    public static ObjectCodec of(final EffectiveModelContext schemaContext, final TypeDefinition<?> typeDefinition) {
-        return new ObjectCodec(schemaContext, typeDefinition);
-    }
-
-    public Object deserialize(final String input) {
-        try {
-            if (type instanceof IdentityrefTypeDefinition) {
-                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) {
-                // FIXME: what is it that we are trying to decode here and why?
-                return new StringModuleInstanceIdentifierCodec(schemaContext).deserialize(input);
-            } else {
-                final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec =
-                        TypeDefinitionAwareCodec.from(type);
-                if (typeAwarecodec != null) {
-                    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;
-        }
-    }
-}
\ No newline at end of file
index e27f421fea6a504aba218c5ad638b60648de97db..dbfeb72e089d9f9e335ce5314fb9266462c1cefd 100644 (file)
@@ -7,14 +7,12 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.codecs;
 
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import org.opendaylight.restconf.common.util.IdentityValuesDTO;
-import org.opendaylight.restconf.common.util.IdentityValuesDTO.IdentityValue;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.XMLNamespace;
-import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.restconf.common.util.RestUtil;
+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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -26,60 +24,39 @@ public final class RestCodec {
         // Hidden on purpose
     }
 
-    public static class IdentityrefCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
-        private static final Logger LOG = LoggerFactory.getLogger(IdentityrefCodecImpl.class);
-
-        private final SchemaContext schemaContext;
-
-        public IdentityrefCodecImpl(final SchemaContext schemaContext) {
-            this.schemaContext = schemaContext;
-        }
-
-        @Override
-        public IdentityValuesDTO serialize(final QName data) {
-            return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), null, null);
-        }
-
-        @Override
-        @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "Legacy return")
-        public QName deserialize(final IdentityValuesDTO data) {
-            final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
-            final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), schemaContext);
-            // FIXME: this needs to be a hard error
-            if (module == null) {
-                LOG.info("Module was not found for namespace {}", valueWithNamespace.getNamespace());
-                LOG.info("Idenetityref will be translated as NULL for data - {}", String.valueOf(valueWithNamespace));
+    public static Object deserialize(final EffectiveModelContext schemaContext, final TypeDefinition<?> typeDefinition,
+            final String input) {
+        final TypeDefinition<?> type = RestUtil.resolveBaseTypeFrom(typeDefinition);
+
+        try {
+            if (type instanceof IdentityrefTypeDefinition) {
+                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) {
+                // FIXME: what is it that we are trying to decode here and why?
+                return new StringModuleInstanceIdentifierCodec(schemaContext).deserialize(input);
+            } else {
+                final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> typeAwarecodec =
+                        TypeDefinitionAwareCodec.from(type);
+                if (typeAwarecodec != null) {
+                    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;
+                }
             }
-
-            return QName.create(module.getNamespace(), module.getRevision(), valueWithNamespace.getValue());
-        }
-
-    }
-
-    private static Module getModuleByNamespace(final String namespace, final SchemaContext schemaContext) {
-        final var validNamespace = resolveValidNamespace(namespace, schemaContext);
-        final var it = schemaContext.findModules(validNamespace).iterator();
-        if (!it.hasNext()) {
-            LOG.info("Module for namespace {} was not found.", validNamespace);
+        } 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;
         }
-        return it.next();
-    }
-
-    private static XMLNamespace resolveValidNamespace(final String namespace, final SchemaContext schemaContext) {
-        XMLNamespace validNamespace = findFirstModuleByName(schemaContext, namespace);
-        return validNamespace != null ? validNamespace
-            // FIXME: what the heck?!
-            : XMLNamespace.of(namespace);
-    }
-
-    private static XMLNamespace findFirstModuleByName(final SchemaContext schemaContext, final String name) {
-        for (final Module module : schemaContext.getModules()) {
-            if (module.getName().equals(name)) {
-                return module.getNamespace();
-            }
-        }
-        return null;
     }
-}
+}
\ No newline at end of file
index 789173bc99a3ff81ac2c75aa90254ff171b39112..2d3cb34cdb9c8aa441cbff5730e7ed97d0da17f0 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.ObjectCodec;
+import org.opendaylight.restconf.nb.rfc8040.codecs.RestCodec;
 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 ObjectCodec.of(schemaContext, typedef).deserialize(value);
+            return RestCodec.deserialize(schemaContext, typedef, value);
         } catch (IllegalArgumentException e) {
             throw new RestconfDocumentedException("Invalid value '" + value + "' for " + schemaNode.getQName(),
                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);