Bump odlparent to 6.0.0
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / restconf / impl / RestCodec.java
index dbf87cbaf5106eaeb1a50ebff0c8ffb77e655a7c..f89a5489b6d24ecde1e1cd53e0ad25375936a673 100644 (file)
@@ -7,13 +7,15 @@
  */
 package org.opendaylight.netconf.sal.restconf.impl;
 
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
+import java.util.Set;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.netconf.sal.rest.impl.StringModuleInstanceIdentifierCodec;
 import org.opendaylight.restconf.common.util.IdentityValuesDTO;
 import org.opendaylight.restconf.common.util.IdentityValuesDTO.IdentityValue;
@@ -50,8 +52,8 @@ public final class RestCodec {
     }
 
     public static Codec<Object, Object> from(final TypeDefinition<?> typeDefinition,
-            final DOMMountPoint mountPoint) {
-        return new ObjectCodec(typeDefinition, mountPoint);
+            final DOMMountPoint mountPoint, final ControllerContext controllerContext) {
+        return new ObjectCodec(typeDefinition, mountPoint, controllerContext);
     }
 
     @SuppressWarnings("rawtypes")
@@ -60,20 +62,24 @@ public final class RestCodec {
         private static final Logger LOG = LoggerFactory.getLogger(ObjectCodec.class);
 
         public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl();
+
+        private final ControllerContext controllerContext;
         private final Codec instanceIdentifier;
         private final Codec identityrefCodec;
 
         private final TypeDefinition<?> type;
 
-        private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint) {
+        private ObjectCodec(final TypeDefinition<?> typeDefinition, final DOMMountPoint mountPoint,
+                final ControllerContext controllerContext) {
+            this.controllerContext = controllerContext;
             this.type = RestUtil.resolveBaseTypeFrom(typeDefinition);
             if (this.type instanceof IdentityrefTypeDefinition) {
-                this.identityrefCodec = new IdentityrefCodecImpl(mountPoint);
+                this.identityrefCodec = new IdentityrefCodecImpl(mountPoint, controllerContext);
             } else {
                 this.identityrefCodec = null;
             }
             if (this.type instanceof InstanceIdentifierTypeDefinition) {
-                this.instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint);
+                this.instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint, controllerContext);
             } else {
                 this.instanceIdentifier = null;
             }
@@ -99,7 +105,7 @@ public final class RestCodec {
                         return this.instanceIdentifier.deserialize(input);
                     } else {
                         final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
-                                ControllerContext.getInstance().getGlobalSchema());
+                                controllerContext.getGlobalSchema());
                         return codec.deserialize((String) input);
                     }
                 } else {
@@ -111,15 +117,12 @@ public final class RestCodec {
                         }
                         return typeAwarecodec.deserialize(String.valueOf(input));
                     } else {
-                        LOG.debug("Codec for type \"" + this.type.getQName().getLocalName()
-                                + "\" is not implemented yet.");
+                        LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
                         return null;
                     }
                 }
             } catch (final ClassCastException e) { // TODO remove this catch when everyone use codecs
-                LOG.error(
-                        "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
-                        e);
+                LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
                 return null;
             }
         }
@@ -140,17 +143,12 @@ public final class RestCodec {
                     if (typeAwarecodec != null) {
                         return typeAwarecodec.serialize(input);
                     } else {
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Codec for type \"" + this.type.getQName().getLocalName()
-                                + "\" is not implemented yet.");
-                        }
+                        LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
                         return null;
                     }
                 }
             } catch (final ClassCastException e) { // TODO remove this catch when everyone use codecs
-                LOG.error(
-                        "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input),
-                        e);
+                LOG.error("ClassCastException was thrown when codec is invoked with parameter {}", input, e);
                 return input;
             }
         }
@@ -162,9 +160,11 @@ public final class RestCodec {
         private static final Logger LOG = LoggerFactory.getLogger(IdentityrefCodecImpl.class);
 
         private final DOMMountPoint mountPoint;
+        private final ControllerContext controllerContext;
 
-        public IdentityrefCodecImpl(final DOMMountPoint mountPoint) {
+        public IdentityrefCodecImpl(final DOMMountPoint mountPoint, final ControllerContext controllerContext) {
             this.mountPoint = mountPoint;
+            this.controllerContext = controllerContext;
         }
 
         @Override
@@ -175,7 +175,8 @@ public final class RestCodec {
         @Override
         public QName deserialize(final IdentityValuesDTO data) {
             final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
-            final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint);
+            final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint,
+                    controllerContext);
             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));
@@ -203,10 +204,14 @@ 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 ControllerContext controllerContext;
 
-        public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint) {
+        public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint,
+                final ControllerContext controllerContext) {
             this.mountPoint = mountPoint;
+            this.controllerContext = controllerContext;
         }
 
         @Override
@@ -216,11 +221,11 @@ public final class RestCodec {
                 final IdentityValue identityValue = qNameToIdentityValue(pathArgument.getNodeType());
                 if (pathArgument instanceof NodeIdentifierWithPredicates && identityValue != null) {
                     final List<Predicate> predicates =
-                            keyValuesToPredicateList(((NodeIdentifierWithPredicates) pathArgument).getKeyValues());
+                            keyValuesToPredicateList(((NodeIdentifierWithPredicates) pathArgument).entrySet());
                     identityValue.setPredicates(predicates);
                 } else if (pathArgument instanceof NodeWithValue && identityValue != null) {
                     final List<Predicate> predicates = new ArrayList<>();
-                    final String value = String.valueOf(((NodeWithValue) pathArgument).getValue());
+                    final String value = String.valueOf(((NodeWithValue<?>) pathArgument).getValue());
                     predicates.add(new Predicate(null, value));
                     identityValue.setPredicates(predicates);
                 }
@@ -229,11 +234,14 @@ public final class RestCodec {
             return identityValuesDTO;
         }
 
+        @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
+                justification = "Unrecognised NullableDecl")
         @Override
         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(), this.mountPoint);
+            final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint,
+                    controllerContext);
             if (module == null) {
                 LOG.info("Module by namespace '{}' of first node in instance-identifier was not found.",
                         valueWithNamespace.getNamespace());
@@ -246,7 +254,8 @@ public final class RestCodec {
             final List<IdentityValue> identities = data.getValuesWithNamespaces();
             for (int i = 0; i < identities.size(); i++) {
                 final IdentityValue identityValue = identities.get(i);
-                URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), this.mountPoint);
+                URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), this.mountPoint,
+                        controllerContext);
                 final DataSchemaNode node = ControllerContext.findInstanceDataChildByNameAndNamespace(
                         parentContainer, identityValue.getValue(), validNamespace);
                 if (node == null) {
@@ -273,13 +282,14 @@ 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(), this.mountPoint);
+                            validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), this.mountPoint,
+                                    controllerContext);
                             final DataSchemaNode listKey = ControllerContext
                                     .findInstanceDataChildByNameAndNamespace(listNode, predicate.getName().getValue(),
                                             validNamespace);
                             predicatesMap.put(listKey.getQName(), predicate.getValue());
                         }
-                        pathArgument = new NodeIdentifierWithPredicates(qName, predicatesMap);
+                        pathArgument = NodeIdentifierWithPredicates.of(qName, predicatesMap);
                     } else {
                         LOG.info("Node {} is not List or Leaf-list.", node);
                         LOG.info("Instance-identifier will be translated as NULL for data - {}",
@@ -304,9 +314,9 @@ public final class RestCodec {
             return result.isEmpty() ? null : YangInstanceIdentifier.create(result);
         }
 
-        private static List<Predicate> keyValuesToPredicateList(final Map<QName, Object> keyValues) {
+        private static List<Predicate> keyValuesToPredicateList(final Set<Entry<QName, Object>> keyValues) {
             final List<Predicate> result = new ArrayList<>();
-            for (final Entry<QName, Object> entry : keyValues.entrySet()) {
+            for (final Entry<QName, Object> entry : keyValues) {
                 final QName qualifiedName = entry.getKey();
                 final Object value = entry.getValue();
                 result.add(new Predicate(qNameToIdentityValue(qualifiedName), String.valueOf(value)));
@@ -322,28 +332,32 @@ public final class RestCodec {
         }
     }
 
-    private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint) {
-        final URI validNamespace = resolveValidNamespace(namespace, mountPoint);
+    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+            justification = "https://github.com/spotbugs/spotbugs/issues/811")
+    private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint,
+            final ControllerContext controllerContext) {
+        final URI validNamespace = resolveValidNamespace(namespace, mountPoint, controllerContext);
 
         Module module = null;
         if (mountPoint != null) {
-            module = ControllerContext.getInstance().findModuleByNamespace(mountPoint, validNamespace);
+            module = controllerContext.findModuleByNamespace(mountPoint, validNamespace);
         } else {
-            module = ControllerContext.getInstance().findModuleByNamespace(validNamespace);
+            module = controllerContext.findModuleByNamespace(validNamespace);
         }
         if (module == null) {
-            LOG.info("Module for namespace " + validNamespace + " wasn't found.");
+            LOG.info("Module for namespace {} was not found.", validNamespace);
             return null;
         }
         return module;
     }
 
-    private static URI resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint) {
+    private static URI resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint,
+            final ControllerContext controllerContext) {
         URI validNamespace;
         if (mountPoint != null) {
-            validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(mountPoint, namespace);
+            validNamespace = controllerContext.findNamespaceByModuleName(mountPoint, namespace);
         } else {
-            validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(namespace);
+            validNamespace = controllerContext.findNamespaceByModuleName(namespace);
         }
         if (validNamespace == null) {
             validNamespace = URI.create(namespace);