ObjectCodec does not need a mount point 49/100549/2
authorRobert Varga <[email protected]>
Tue, 12 Apr 2022 14:59:03 +0000 (16:59 +0200)
committerRobert Varga <[email protected]>
Tue, 12 Apr 2022 16:08:46 +0000 (18:08 +0200)
There is only one caller and that caller is not passing a mount point,
propagate that invariant.

JIRA: NETCONF-871
Change-Id: I4a5a312cce87be3176b9757e5bfdde6538c229d0
Signed-off-by: Robert Varga <[email protected]>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/codecs/ObjectCodec.java
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

index deb48d494949d9aede9e6a9433ba02296f4a471c..ede67838cfa2a80227eed9445690b6da6d2b99d0 100644 (file)
@@ -7,8 +7,9 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.codecs;
 
+import static java.util.Objects.requireNonNull;
+
 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;
@@ -35,25 +36,23 @@ public final class ObjectCodec implements IllegalArgumentCodec<Object, Object> {
     private final EffectiveModelContext schemaContext;
     private final TypeDefinition<?> type;
 
-    private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
-            final EffectiveModelContext schemaContext) {
-        this.schemaContext = schemaContext;
+    private ObjectCodec(final EffectiveModelContext schemaContext, final TypeDefinition<?> typeDefinition) {
+        this.schemaContext = requireNonNull(schemaContext);
         type = RestUtil.resolveBaseTypeFrom(typeDefinition);
         if (type instanceof IdentityrefTypeDefinition) {
-            identityrefCodec = new IdentityrefCodecImpl(mountPoint, schemaContext);
+            identityrefCodec = new IdentityrefCodecImpl(schemaContext);
         } else {
             identityrefCodec = null;
         }
         if (type instanceof InstanceIdentifierTypeDefinition) {
-            instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint, schemaContext);
+            instanceIdentifier = new InstanceIdentifierCodecImpl(schemaContext);
         } else {
             instanceIdentifier = null;
         }
     }
 
-    public static ObjectCodec of(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
-            final EffectiveModelContext schemaContext) {
-        return new ObjectCodec(typeDefinition, mountPoint, schemaContext);
+    public static ObjectCodec of(final EffectiveModelContext schemaContext, final TypeDefinition<?> typeDefinition) {
+        return new ObjectCodec(schemaContext, typeDefinition);
     }
 
     @SuppressWarnings("unchecked")
index 40900eef50ea4861544f8c9e0677b09e1916f961..15857e7adc3c98d12ed2fb5f90890e52058a5d75 100644 (file)
@@ -17,10 +17,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 import java.util.Set;
-import org.opendaylight.mdsal.dom.api.DOMMountPoint;
-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;
@@ -40,7 +37,6 @@ import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
@@ -61,10 +57,8 @@ public final class RestCodec {
         private static final Logger LOG = LoggerFactory.getLogger(IdentityrefCodecImpl.class);
 
         private final SchemaContext schemaContext;
-        private final DOMMountPoint mountPoint;
 
-        public IdentityrefCodecImpl(final DOMMountPoint mountPoint, final SchemaContext schemaContext) {
-            this.mountPoint = mountPoint;
+        public IdentityrefCodecImpl(final SchemaContext schemaContext) {
             this.schemaContext = schemaContext;
         }
 
@@ -77,7 +71,7 @@ public final class RestCodec {
         @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(), mountPoint, schemaContext);
+            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());
@@ -106,11 +100,10 @@ public final class RestCodec {
 
     public static class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec<IdentityValuesDTO> {
         private static final Logger LOG = LoggerFactory.getLogger(InstanceIdentifierCodecImpl.class);
-        private final DOMMountPoint mountPoint;
+
         private final SchemaContext schemaContext;
 
-        public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint, final SchemaContext schemaContext) {
-            this.mountPoint = mountPoint;
+        public InstanceIdentifierCodecImpl(final SchemaContext schemaContext) {
             this.schemaContext = schemaContext;
         }
 
@@ -143,7 +136,7 @@ public final class RestCodec {
         public YangInstanceIdentifier deserialize(final IdentityValuesDTO data) {
             final List<PathArgument> result = new ArrayList<>();
             final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
-            final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), mountPoint, schemaContext);
+            final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), schemaContext);
             // FIXME: this needs to be a hard error
             if (module == null) {
                 LOG.info("Module by namespace '{}' of first node in instance-identifier was not found.",
@@ -157,8 +150,7 @@ public final class RestCodec {
             final List<IdentityValue> identities = data.getValuesWithNamespaces();
             for (int i = 0; i < identities.size(); i++) {
                 final IdentityValue identityValue = identities.get(i);
-                XMLNamespace validNamespace =
-                        resolveValidNamespace(identityValue.getNamespace(), mountPoint, schemaContext);
+                XMLNamespace validNamespace = resolveValidNamespace(identityValue.getNamespace(), schemaContext);
                 final DataSchemaNode node = findInstanceDataChildByNameAndNamespace(
                         parentContainer, identityValue.getValue(), validNamespace);
                 // FIXME: this needs to be a hard error
@@ -186,8 +178,7 @@ public final class RestCodec {
                     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);
+                        validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), schemaContext);
                         final DataSchemaNode listKey = findInstanceDataChildByNameAndNamespace(listNode,
                                 predicate.getName().getValue(), validNamespace);
                         predicatesMap.put(listKey.getQName(), predicate.getValue());
@@ -236,35 +227,21 @@ public final class RestCodec {
         }
     }
 
-    private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint,
-            final SchemaContext schemaContext) {
-        final XMLNamespace validNamespace = resolveValidNamespace(namespace, mountPoint, schemaContext);
-        Module module = null;
-        if (mountPoint != null) {
-            module = modelContext(mountPoint).findModules(validNamespace).iterator().next();
-        } else {
-            module = schemaContext.findModules(validNamespace).iterator().next();
-        }
-        if (module == null) {
+    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);
             return null;
         }
-        return module;
+        return it.next();
     }
 
-    private static XMLNamespace resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint,
-            final SchemaContext schemaContext) {
-        XMLNamespace validNamespace;
-        if (mountPoint != null) {
-            validNamespace = findFirstModuleByName(modelContext(mountPoint), namespace);
-        } else {
-            validNamespace = findFirstModuleByName(schemaContext, namespace);
-        }
-        if (validNamespace == null) {
-            validNamespace = XMLNamespace.of(namespace);
-        }
-
-        return validNamespace;
+    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) {
@@ -321,10 +298,4 @@ public final class RestCodec {
                 || node instanceof ContainerSchemaNode || node instanceof ListSchemaNode
                 || node instanceof AnyxmlSchemaNode;
     }
-
-    private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
-        return mountPoint.getService(DOMSchemaService.class)
-            .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
-            .orElse(null);
-    }
 }
index 602d204f922b33abca0a5a6e945f462bf766e755..789173bc99a3ff81ac2c75aa90254ff171b39112 100644 (file)
@@ -269,7 +269,7 @@ public final class YangInstanceIdentifierDeserializer {
             return toIdentityrefQName(value, schemaNode);
         }
         try {
-            return ObjectCodec.of(typedef, null, schemaContext).deserialize(value);
+            return ObjectCodec.of(schemaContext, typedef).deserialize(value);
         } catch (IllegalArgumentException e) {
             throw new RestconfDocumentedException("Invalid value '" + value + "' for " + schemaNode.getQName(),
                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);