Merge "Bug 9092: revert to org.json temporarily"
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / restconf / impl / ControllerContext.java
index e4f71a7ff03e00a07df93422f9bd8b0eb7826cc2..7ebb0dcd4b57c0009e0c34e010dc974328635aab 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,11 +7,8 @@
  */
 package org.opendaylight.netconf.sal.restconf.impl;
 
-import com.google.common.base.Function;
-import com.google.common.base.Objects;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-import com.google.common.base.Predicate;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableMap;
@@ -20,6 +17,8 @@ import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -36,7 +35,7 @@ import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizat
 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
-import org.opendaylight.netconf.sal.rest.api.Draft02;
+import org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule;
 import org.opendaylight.netconf.sal.rest.impl.RestUtil;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
@@ -70,35 +69,35 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class ControllerContext implements SchemaContextListener {
-    private final static Logger LOG = LoggerFactory.getLogger(ControllerContext.class);
+    // FIXME: this should be in md-sal somewhere
+    public static final String MOUNT = "yang-ext:mount";
 
-    private final static ControllerContext INSTANCE = new ControllerContext();
+    private static final Logger LOG = LoggerFactory.getLogger(ControllerContext.class);
 
-    private final static String NULL_VALUE = "null";
+    // FIXME: this should be the current instance which is mutated
+    private static final ControllerContext INSTANCE = new ControllerContext();
 
-    private final static String MOUNT_MODULE = "yang-ext";
+    private static final String NULL_VALUE = "null";
 
-    private final static String MOUNT_NODE = "mount";
+    private static final String MOUNT_MODULE = "yang-ext";
 
-    public final static String MOUNT = "yang-ext:mount";
+    private static final String MOUNT_NODE = "mount";
 
-    private final static String URI_ENCODING_CHAR_SET = "ISO-8859-1";
+    private static final Charset URI_ENCODING_CHARSET = StandardCharsets.UTF_8;
 
     private static final Splitter SLASH_SPLITTER = Splitter.on('/');
 
-    private static final YangInstanceIdentifier ROOT = YangInstanceIdentifier.builder().build();
-
-    private final AtomicReference<Map<QName, RpcDefinition>> qnameToRpc =
-            new AtomicReference<>(Collections.<QName, RpcDefinition>emptyMap());
+    private final AtomicReference<Map<QName, RpcDefinition>> qnameToRpc = new AtomicReference<>(Collections.emptyMap());
 
+    // FIXME; these three should be final
     private volatile SchemaContext globalSchema;
     private volatile DOMMountPointService mountService;
-
     private DataNormalizer dataNormalizer;
 
+
     public void setGlobalSchema(final SchemaContext globalSchema) {
         this.globalSchema = globalSchema;
-        dataNormalizer = new DataNormalizer(globalSchema);
+        this.dataNormalizer = new DataNormalizer(globalSchema);
     }
 
     public void setMountService(final DOMMountPointService mountService) {
@@ -109,11 +108,11 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public static ControllerContext getInstance() {
-        return ControllerContext.INSTANCE;
+        return INSTANCE;
     }
 
     private void checkPreconditions() {
-        if (globalSchema == null) {
+        if (this.globalSchema == null) {
             throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
         }
     }
@@ -127,18 +126,20 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public SchemaContext getGlobalSchema() {
-        return globalSchema;
+        return this.globalSchema;
     }
 
     public InstanceIdentifierContext<?> toMountPointIdentifier(final String restconfInstance) {
         return toIdentifier(restconfInstance, true);
     }
 
-    private InstanceIdentifierContext<?> toIdentifier(final String restconfInstance, final boolean toMountPointIdentifier) {
+    private InstanceIdentifierContext<?> toIdentifier(final String restconfInstance,
+                                                      final boolean toMountPointIdentifier) {
         checkPreconditions();
 
-        if(restconfInstance == null) {
-            return new InstanceIdentifierContext<>(ROOT, globalSchema, null, globalSchema);
+        if (restconfInstance == null) {
+            return new InstanceIdentifierContext<>(YangInstanceIdentifier.EMPTY, this.globalSchema, null,
+                    this.globalSchema);
         }
 
         final List<String> pathArgs = urlPathArgsDecode(SLASH_SPLITTER.split(restconfInstance));
@@ -148,21 +149,22 @@ public class ControllerContext implements SchemaContextListener {
         }
 
         final String first = pathArgs.iterator().next();
-        final String startModule = ControllerContext.toModuleName(first);
+        final String startModule = toModuleName(first);
         if (startModule == null) {
             throw new RestconfDocumentedException("First node in URI has to be in format \"moduleName:nodeName\"",
                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
 
         final InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
-        final Module latestModule = globalSchema.findModuleByName(startModule, null);
+        final Module latestModule = this.globalSchema.findModuleByName(startModule, null);
 
         if (latestModule == null) {
-            throw new RestconfDocumentedException("The module named '" + startModule + "' does not exist.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
+            throw new RestconfDocumentedException("The module named '" + startModule + "' does not exist.",
+                    ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
         }
 
-        final InstanceIdentifierContext<?> iiWithSchemaNode = collectPathArguments(builder, pathArgs, latestModule, null,
-                toMountPointIdentifier);
+        final InstanceIdentifierContext<?> iiWithSchemaNode =
+                collectPathArguments(builder, pathArgs, latestModule, null, toMountPointIdentifier);
 
         if (iiWithSchemaNode == null) {
             throw new RestconfDocumentedException("URI has bad format", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
@@ -192,14 +194,15 @@ public class ControllerContext implements SchemaContextListener {
 
         return list;
     }
+
     public Module findModuleByName(final String moduleName) {
         checkPreconditions();
-        Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
-        return globalSchema.findModuleByName(moduleName, null);
+        Preconditions.checkArgument((moduleName != null) && !moduleName.isEmpty());
+        return this.globalSchema.findModuleByName(moduleName, null);
     }
 
     public Module findModuleByName(final DOMMountPoint mountPoint, final String moduleName) {
-        Preconditions.checkArgument(moduleName != null && mountPoint != null);
+        Preconditions.checkArgument((moduleName != null) && (mountPoint != null));
 
         final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
         if (mountPointSchema == null) {
@@ -212,11 +215,11 @@ public class ControllerContext implements SchemaContextListener {
     public Module findModuleByNamespace(final URI namespace) {
         checkPreconditions();
         Preconditions.checkArgument(namespace != null);
-        return globalSchema.findModuleByNamespaceAndRevision(namespace, null);
+        return this.globalSchema.findModuleByNamespaceAndRevision(namespace, null);
     }
 
     public Module findModuleByNamespace(final DOMMountPoint mountPoint, final URI namespace) {
-        Preconditions.checkArgument(namespace != null && mountPoint != null);
+        Preconditions.checkArgument((namespace != null) && (mountPoint != null));
 
         final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
         if (mountPointSchema == null) {
@@ -228,15 +231,17 @@ public class ControllerContext implements SchemaContextListener {
 
     public Module findModuleByNameAndRevision(final QName module) {
         checkPreconditions();
-        Preconditions.checkArgument(module != null && module.getLocalName() != null && module.getRevision() != null);
+        Preconditions
+                .checkArgument((module != null) && (module.getLocalName() != null) && (module.getRevision() != null));
 
-        return globalSchema.findModuleByName(module.getLocalName(), module.getRevision());
+        return this.globalSchema.findModuleByName(module.getLocalName(), module.getRevision());
     }
 
     public Module findModuleByNameAndRevision(final DOMMountPoint mountPoint, final QName module) {
         checkPreconditions();
-        Preconditions.checkArgument(module != null && module.getLocalName() != null && module.getRevision() != null
-                && mountPoint != null);
+        Preconditions
+                .checkArgument((module != null) && (module.getLocalName() != null) && (module.getRevision() != null)
+                && (mountPoint != null));
 
         final SchemaContext schemaContext = mountPoint.getSchemaContext();
         return schemaContext == null ? null : schemaContext.findModuleByName(module.getLocalName(),
@@ -249,13 +254,13 @@ public class ControllerContext implements SchemaContextListener {
         final Iterable<PathArgument> elements = path.getPathArguments();
         final PathArgument head = elements.iterator().next();
         final QName startQName = head.getNodeType();
-        final Module initialModule = globalSchema.findModuleByNamespaceAndRevision(startQName.getNamespace(),
+        final Module initialModule = this.globalSchema.findModuleByNamespaceAndRevision(startQName.getNamespace(),
                 startQName.getRevision());
         DataNodeContainer node = initialModule;
         for (final PathArgument element : elements) {
             final QName _nodeType = element.getNodeType();
-            final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
-            if (potentialNode == null || !ControllerContext.isListOrContainer(potentialNode)) {
+            final DataSchemaNode potentialNode = childByQName(node, _nodeType);
+            if ((potentialNode == null) || !isListOrContainer(potentialNode)) {
                 return null;
             }
             node = (DataNodeContainer) potentialNode;
@@ -275,7 +280,7 @@ public class ControllerContext implements SchemaContextListener {
         if (mount != null) {
             schemaContext = mount.getSchemaContext();
         } else {
-            schemaContext = globalSchema;
+            schemaContext = this.globalSchema;
         }
         final Module initialModule = schemaContext.findModuleByNamespaceAndRevision(startQName.getNamespace(),
                 startQName.getRevision());
@@ -283,9 +288,9 @@ public class ControllerContext implements SchemaContextListener {
         for (final PathArgument element : elements) {
             if (!(element instanceof AugmentationIdentifier)) {
                 final QName _nodeType = element.getNodeType();
-                final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
-                if (!(element instanceof NodeIdentifier && potentialNode instanceof ListSchemaNode) &&
-                        !(potentialNode instanceof ChoiceSchemaNode)) {
+                final DataSchemaNode potentialNode = childByQName(node, _nodeType);
+                if (!((element instanceof NodeIdentifier) && (potentialNode instanceof ListSchemaNode))
+                        && !(potentialNode instanceof ChoiceSchemaNode)) {
                     builder.append(convertToRestconfIdentifier(element, potentialNode, mount));
                     if (potentialNode instanceof DataNodeContainer) {
                         node = (DataNodeContainer) potentialNode;
@@ -328,10 +333,10 @@ public class ControllerContext implements SchemaContextListener {
 
     public Set<Module> getAllModules() {
         checkPreconditions();
-        return globalSchema.getModules();
+        return this.globalSchema.getModules();
     }
 
-    private static final CharSequence toRestconfIdentifier(final SchemaContext context, final QName qname) {
+    private static CharSequence toRestconfIdentifier(final SchemaContext context, final QName qname) {
         final Module schema = context.findModuleByNamespaceAndRevision(qname.getNamespace(), qname.getRevision());
         return schema == null ? null : schema.getName() + ':' + qname.getLocalName();
     }
@@ -342,7 +347,7 @@ public class ControllerContext implements SchemaContextListener {
             schema = mount.getSchemaContext();
         } else {
             checkPreconditions();
-            schema = globalSchema;
+            schema = this.globalSchema;
         }
 
         return toRestconfIdentifier(schema, qname);
@@ -351,7 +356,7 @@ public class ControllerContext implements SchemaContextListener {
     public CharSequence toRestconfIdentifier(final QName qname) {
         checkPreconditions();
 
-        return toRestconfIdentifier(globalSchema, qname);
+        return toRestconfIdentifier(this.globalSchema, qname);
     }
 
     public CharSequence toRestconfIdentifier(final DOMMountPoint mountPoint, final QName qname) {
@@ -363,16 +368,9 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public Module getRestconfModule() {
-        return findModuleByNameAndRevision(Draft02.RestConfModule.IETF_RESTCONF_QNAME);
+        return findModuleByNameAndRevision(RestConfModule.IETF_RESTCONF_QNAME);
     }
 
-    private static final Predicate<GroupingDefinition> ERRORS_GROUPING_FILTER = new Predicate<GroupingDefinition>() {
-        @Override
-        public boolean apply(final GroupingDefinition g) {
-            return Draft02.RestConfModule.ERRORS_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName());
-        }
-    };
-
     public DataSchemaNode getRestconfModuleErrorsSchemaNode() {
         final Module restconfModule = getRestconfModule();
         if (restconfModule == null) {
@@ -381,23 +379,18 @@ public class ControllerContext implements SchemaContextListener {
 
         final Set<GroupingDefinition> groupings = restconfModule.getGroupings();
 
-        final Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, ERRORS_GROUPING_FILTER);
+        final Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings,
+            g -> RestConfModule.ERRORS_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName()));
 
         final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
 
         final List<DataSchemaNode> instanceDataChildrenByName = findInstanceDataChildrenByName(restconfGrouping,
-                Draft02.RestConfModule.ERRORS_CONTAINER_SCHEMA_NODE);
+                RestConfModule.ERRORS_CONTAINER_SCHEMA_NODE);
         return Iterables.getFirst(instanceDataChildrenByName, null);
     }
 
-    private static final Predicate<GroupingDefinition> GROUPING_FILTER = new Predicate<GroupingDefinition>() {
-        @Override
-        public boolean apply(final GroupingDefinition g) {
-            return Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName());
-        }
-    };
-
-    public DataSchemaNode getRestconfModuleRestConfSchemaNode(final Module inRestconfModule, final String schemaNodeName) {
+    public DataSchemaNode getRestconfModuleRestConfSchemaNode(final Module inRestconfModule,
+            final String schemaNodeName) {
         Module restconfModule = inRestconfModule;
         if (restconfModule == null) {
             restconfModule = getRestconfModule();
@@ -408,42 +401,43 @@ public class ControllerContext implements SchemaContextListener {
         }
 
         final Set<GroupingDefinition> groupings = restconfModule.getGroupings();
-        final Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, GROUPING_FILTER);
+        final Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings,
+            g -> RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName()));
         final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
 
         final List<DataSchemaNode> instanceDataChildrenByName = findInstanceDataChildrenByName(restconfGrouping,
-                Draft02.RestConfModule.RESTCONF_CONTAINER_SCHEMA_NODE);
+                RestConfModule.RESTCONF_CONTAINER_SCHEMA_NODE);
         final DataSchemaNode restconfContainer = Iterables.getFirst(instanceDataChildrenByName, null);
 
-        if (Objects.equal(schemaNodeName, Draft02.RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE)) {
+        if (RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE.equals(schemaNodeName)) {
             final List<DataSchemaNode> instances = findInstanceDataChildrenByName(
-                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE);
+                    ((DataNodeContainer) restconfContainer), RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE)) {
+        } else if (RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE.equals(schemaNodeName)) {
             final List<DataSchemaNode> instances = findInstanceDataChildrenByName(
-                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
+                    ((DataNodeContainer) restconfContainer), RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE)) {
+        } else if (RestConfModule.STREAM_LIST_SCHEMA_NODE.equals(schemaNodeName)) {
             List<DataSchemaNode> instances = findInstanceDataChildrenByName(
-                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
+                    ((DataNodeContainer) restconfContainer), RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
             final DataSchemaNode modules = Iterables.getFirst(instances, null);
             instances = findInstanceDataChildrenByName(((DataNodeContainer) modules),
-                    Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
+                    RestConfModule.STREAM_LIST_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE)) {
+        } else if (RestConfModule.MODULES_CONTAINER_SCHEMA_NODE.equals(schemaNodeName)) {
             final List<DataSchemaNode> instances = findInstanceDataChildrenByName(
-                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
+                    ((DataNodeContainer) restconfContainer), RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.MODULE_LIST_SCHEMA_NODE)) {
+        } else if (RestConfModule.MODULE_LIST_SCHEMA_NODE.equals(schemaNodeName)) {
             List<DataSchemaNode> instances = findInstanceDataChildrenByName(
-                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
+                    ((DataNodeContainer) restconfContainer), RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
             final DataSchemaNode modules = Iterables.getFirst(instances, null);
             instances = findInstanceDataChildrenByName(((DataNodeContainer) modules),
-                    Draft02.RestConfModule.MODULE_LIST_SCHEMA_NODE);
+                    RestConfModule.MODULE_LIST_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE)) {
+        } else if (RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE.equals(schemaNodeName)) {
             final List<DataSchemaNode> instances = findInstanceDataChildrenByName(
-                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
+                    ((DataNodeContainer) restconfContainer), RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
         }
 
@@ -452,7 +446,7 @@ public class ControllerContext implements SchemaContextListener {
 
     private static DataSchemaNode childByQName(final ChoiceSchemaNode container, final QName name) {
         for (final ChoiceCaseNode caze : container.getCases()) {
-            final DataSchemaNode ret = ControllerContext.childByQName(caze, name);
+            final DataSchemaNode ret = childByQName(caze, name);
             if (ret != null) {
                 return ret;
             }
@@ -466,28 +460,48 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     private static DataSchemaNode childByQName(final ContainerSchemaNode container, final QName name) {
-        return ControllerContext.dataNodeChildByQName(container, name);
+        return dataNodeChildByQName(container, name);
     }
 
     private static DataSchemaNode childByQName(final ListSchemaNode container, final QName name) {
-        return ControllerContext.dataNodeChildByQName(container, name);
+        return dataNodeChildByQName(container, name);
     }
 
     private static DataSchemaNode childByQName(final Module container, final QName name) {
-        return ControllerContext.dataNodeChildByQName(container, name);
+        return dataNodeChildByQName(container, name);
     }
 
     private static DataSchemaNode childByQName(final DataSchemaNode container, final QName name) {
         return null;
     }
 
+
+    private static DataSchemaNode childByQName(final Object container, final QName name) {
+        if (container instanceof ChoiceCaseNode) {
+            return childByQName((ChoiceCaseNode) container, name);
+        } else if (container instanceof ChoiceSchemaNode) {
+            return childByQName((ChoiceSchemaNode) container, name);
+        } else if (container instanceof ContainerSchemaNode) {
+            return childByQName((ContainerSchemaNode) container, name);
+        } else if (container instanceof ListSchemaNode) {
+            return childByQName((ListSchemaNode) container, name);
+        } else if (container instanceof DataSchemaNode) {
+            return childByQName((DataSchemaNode) container, name);
+        } else if (container instanceof Module) {
+            return childByQName((Module) container, name);
+        } else {
+            throw new IllegalArgumentException("Unhandled parameter types: "
+                    + Arrays.<Object>asList(container, name).toString());
+        }
+    }
+
     private static DataSchemaNode dataNodeChildByQName(final DataNodeContainer container, final QName name) {
         final DataSchemaNode ret = container.getDataChildByName(name);
         if (ret == null) {
             for (final DataSchemaNode node : container.getChildNodes()) {
                 if ((node instanceof ChoiceSchemaNode)) {
                     final ChoiceSchemaNode choiceNode = ((ChoiceSchemaNode) node);
-                    final DataSchemaNode childByQName = ControllerContext.childByQName(choiceNode, name);
+                    final DataSchemaNode childByQName = childByQName(choiceNode, name);
                     if (childByQName != null) {
                         return childByQName;
                     }
@@ -497,45 +511,47 @@ public class ControllerContext implements SchemaContextListener {
         return ret;
     }
 
-    private String toUriString(final Object object, final LeafSchemaNode leafNode, final DOMMountPoint mount) throws UnsupportedEncodingException {
+    private static String toUriString(final Object object, final LeafSchemaNode leafNode, final DOMMountPoint mount)
+            throws UnsupportedEncodingException {
         final Codec<Object, Object> codec = RestCodec.from(leafNode.getType(), mount);
-        return object == null ? "" : URLEncoder.encode(codec.serialize(object).toString(), ControllerContext.URI_ENCODING_CHAR_SET);
+        // FIXME: UrlEncoder looks up a well-known charset, we need something that will use it directly
+        return object == null ? "" : URLEncoder.encode(codec.serialize(object).toString(), URI_ENCODING_CHARSET.name());
     }
 
     private InstanceIdentifierContext<?> collectPathArguments(final InstanceIdentifierBuilder builder,
             final List<String> strings, final DataNodeContainer parentNode, final DOMMountPoint mountPoint,
             final boolean returnJustMountPoint) {
-        Preconditions.<List<String>> checkNotNull(strings);
+        Preconditions.<List<String>>checkNotNull(strings);
 
         if (parentNode == null) {
             return null;
         }
 
         if (strings.isEmpty()) {
-            return createContext(builder.build(), ((DataSchemaNode) parentNode), mountPoint,mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
+            return createContext(builder.build(), ((DataSchemaNode) parentNode),
+                mountPoint,mountPoint != null ? mountPoint.getSchemaContext() : this.globalSchema);
         }
 
         final String head = strings.iterator().next();
         final String nodeName = toNodeName(head);
-        final String moduleName = ControllerContext.toModuleName(head);
+        final String moduleName = toModuleName(head);
 
         DataSchemaNode targetNode = null;
         if (!Strings.isNullOrEmpty(moduleName)) {
-            if (Objects.equal(moduleName, ControllerContext.MOUNT_MODULE)
-                    && Objects.equal(nodeName, ControllerContext.MOUNT_NODE)) {
+            if (MOUNT_MODULE.equals(moduleName) && MOUNT_NODE.equals(nodeName)) {
                 if (mountPoint != null) {
                     throw new RestconfDocumentedException("Restconf supports just one mount point in URI.",
                             ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
                 }
 
-                if (mountService == null) {
+                if (this.mountService == null) {
                     throw new RestconfDocumentedException(
                             "MountService was not found. Finding behind mount points does not work.",
                             ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
                 }
 
-                final YangInstanceIdentifier partialPath = dataNormalizer.toNormalized(builder.build());
-                final Optional<DOMMountPoint> mountOpt = mountService.getMountPoint(partialPath);
+                final YangInstanceIdentifier partialPath = this.dataNormalizer.toNormalized(builder.build());
+                final Optional<DOMMountPoint> mountOpt = this.mountService.getMountPoint(partialPath);
                 if (!mountOpt.isPresent()) {
                     LOG.debug("Instance identifier to missing mount point: {}", partialPath);
                     throw new RestconfDocumentedException("Mount point does not exist.", ErrorType.PROTOCOL,
@@ -549,7 +565,7 @@ public class ControllerContext implements SchemaContextListener {
                             ErrorType.APPLICATION, ErrorTag.UNKNOWN_ELEMENT);
                 }
 
-                if (returnJustMountPoint || strings.size() == 1) {
+                if (returnJustMountPoint || (strings.size() == 1)) {
                     final YangInstanceIdentifier instance = YangInstanceIdentifier.builder().build();
                     return new InstanceIdentifierContext<>(instance, mountPointSchema, mount,mountPointSchema);
                 }
@@ -561,9 +577,10 @@ public class ControllerContext implements SchemaContextListener {
                             ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
                 }
 
-                final Module moduleBehindMountPoint = mountPointSchema.findModuleByName(moduleNameBehindMountPoint, null);
+                final Module moduleBehindMountPoint =
+                        mountPointSchema.findModuleByName(moduleNameBehindMountPoint, null);
                 if (moduleBehindMountPoint == null) {
-                    throw new RestconfDocumentedException("\"" + moduleName
+                    throw new RestconfDocumentedException("\"" + moduleNameBehindMountPoint
                             + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                 }
 
@@ -575,7 +592,7 @@ public class ControllerContext implements SchemaContextListener {
             Module module = null;
             if (mountPoint == null) {
                 checkPreconditions();
-                module = globalSchema.findModuleByName(moduleName, null);
+                module = this.globalSchema.findModuleByName(moduleName, null);
                 if (module == null) {
                     throw new RestconfDocumentedException("\"" + moduleName + "\" module does not exist.",
                             ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
@@ -595,17 +612,18 @@ public class ControllerContext implements SchemaContextListener {
 
             targetNode = findInstanceDataChildByNameAndNamespace(parentNode, nodeName, module.getNamespace());
 
-            if (targetNode == null && parentNode instanceof Module) {
+            if ((targetNode == null) && (parentNode instanceof Module)) {
                 final RpcDefinition rpc;
                 if (mountPoint == null) {
                     rpc = ControllerContext.getInstance().getRpcDefinition(head, module.getRevision());
                 } else {
                     final String rpcName = toNodeName(head);
-                    rpc = ControllerContext.getInstance().getRpcDefinition(module, rpcName);
+                    ControllerContext.getInstance();
+                    rpc = ControllerContext.getRpcDefinition(module, rpcName);
                 }
                 if (rpc != null) {
-                    return new InstanceIdentifierContext<RpcDefinition>(builder.build(), rpc, mountPoint,
-                            mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
+                    return new InstanceIdentifierContext<>(builder.build(), rpc, mountPoint,
+                            mountPoint != null ? mountPoint.getSchemaContext() : this.globalSchema);
                 }
             }
 
@@ -625,9 +643,9 @@ public class ControllerContext implements SchemaContextListener {
 
                 throw new RestconfDocumentedException(
                         "URI has bad format. Node \""
-                                + nodeName
-                                + "\" is added as augment from more than one module. "
-                                + "Therefore the node must have module name and it has to be in format \"moduleName:nodeName\"."
+                                + nodeName + "\" is added as augment from more than one module. "
+                                + "Therefore the node must have module name "
+                                + "and it has to be in format \"moduleName:nodeName\"."
                                 + "\nThe node is added as augment from modules with namespaces:\n"
                                 + strBuilder.toString(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
             }
@@ -640,7 +658,7 @@ public class ControllerContext implements SchemaContextListener {
             targetNode = potentialSchemaNodes.iterator().next();
         }
 
-        if (!ControllerContext.isListOrContainer(targetNode)) {
+        if (!isListOrContainer(targetNode)) {
             throw new RestconfDocumentedException("URI has bad format. Node \"" + head
                     + "\" must be Container or List yang type.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
@@ -655,11 +673,11 @@ public class ControllerContext implements SchemaContextListener {
             }
 
             final List<String> uriKeyValues = strings.subList(consumed, consumed + keysSize);
-            final HashMap<QName, Object> keyValues = new HashMap<QName, Object>();
-            int i = 0;
+            final HashMap<QName, Object> keyValues = new HashMap<>();
+            int index = 0;
             for (final QName key : listNode.getKeyDefinition()) {
                 {
-                    final String uriKeyValue = uriKeyValues.get(i);
+                    final String uriKeyValue = uriKeyValues.get(index);
                     if (uriKeyValue.equals(NULL_VALUE)) {
                         throw new RestconfDocumentedException("URI has bad format. List \""
                                 + listNode.getQName().getLocalName() + "\" cannot contain \"null\" value as a key.",
@@ -667,11 +685,11 @@ public class ControllerContext implements SchemaContextListener {
                     }
 
                     addKeyValue(keyValues, listNode.getDataChildByName(key), uriKeyValue, mountPoint);
-                    i++;
+                    index++;
                 }
             }
 
-            consumed = consumed + i;
+            consumed = consumed + index;
             builder.nodeWithKey(targetNode.getQName(), keyValues);
         } else {
             builder.node(targetNode.getQName());
@@ -683,60 +701,40 @@ public class ControllerContext implements SchemaContextListener {
                     returnJustMountPoint);
         }
 
-        return createContext(builder.build(), targetNode, mountPoint,mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
+        return createContext(builder.build(), targetNode, mountPoint,
+            mountPoint != null ? mountPoint.getSchemaContext() : this.globalSchema);
     }
 
-    private InstanceIdentifierContext<?> createContext(final YangInstanceIdentifier instance, final DataSchemaNode dataSchemaNode,
-            final DOMMountPoint mountPoint, final SchemaContext schemaContext) {
-
+    private static InstanceIdentifierContext<?> createContext(final YangInstanceIdentifier instance,
+            final DataSchemaNode dataSchemaNode, final DOMMountPoint mountPoint, final SchemaContext schemaContext) {
         final YangInstanceIdentifier instanceIdentifier = new DataNormalizer(schemaContext).toNormalized(instance);
-        return new InstanceIdentifierContext<>(instanceIdentifier, dataSchemaNode, mountPoint,schemaContext);
+        return new InstanceIdentifierContext<>(instanceIdentifier, dataSchemaNode, mountPoint, schemaContext);
     }
 
-    public static DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container, final String name,
-            final URI namespace) {
-        Preconditions.<URI> checkNotNull(namespace);
-
-        final List<DataSchemaNode> potentialSchemaNodes = findInstanceDataChildrenByName(container, name);
-
-        final Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
-            @Override
-            public boolean apply(final DataSchemaNode node) {
-                return Objects.equal(node.getQName().getNamespace(), namespace);
-            }
-        };
+    public static DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container,
+            final String name, final URI namespace) {
+        Preconditions.checkNotNull(namespace);
 
-        final Iterable<DataSchemaNode> result = Iterables.filter(potentialSchemaNodes, filter);
+        final Iterable<DataSchemaNode> result = Iterables.filter(findInstanceDataChildrenByName(container, name),
+            node -> namespace.equals(node.getQName().getNamespace()));
         return Iterables.getFirst(result, null);
     }
 
-    public static List<DataSchemaNode> findInstanceDataChildrenByName(final DataNodeContainer container, final String name) {
-        Preconditions.<DataNodeContainer> checkNotNull(container);
-        Preconditions.<String> checkNotNull(name);
+    public static List<DataSchemaNode> findInstanceDataChildrenByName(final DataNodeContainer container,
+            final String name) {
+        Preconditions.checkNotNull(container);
+        Preconditions.checkNotNull(name);
 
-        final List<DataSchemaNode> instantiatedDataNodeContainers = new ArrayList<DataSchemaNode>();
+        final List<DataSchemaNode> instantiatedDataNodeContainers = new ArrayList<>();
         collectInstanceDataNodeContainers(instantiatedDataNodeContainers, container, name);
         return instantiatedDataNodeContainers;
     }
 
-    private static final Function<ChoiceSchemaNode, Set<ChoiceCaseNode>> CHOICE_FUNCTION = new Function<ChoiceSchemaNode, Set<ChoiceCaseNode>>() {
-        @Override
-        public Set<ChoiceCaseNode> apply(final ChoiceSchemaNode node) {
-            return node.getCases();
-        }
-    };
-
     private static void collectInstanceDataNodeContainers(final List<DataSchemaNode> potentialSchemaNodes,
             final DataNodeContainer container, final String name) {
 
-        final Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
-            @Override
-            public boolean apply(final DataSchemaNode node) {
-                return Objects.equal(node.getQName().getLocalName(), name);
-            }
-        };
-
-        final Iterable<DataSchemaNode> nodes = Iterables.filter(container.getChildNodes(), filter);
+        final Iterable<DataSchemaNode> nodes = Iterables.filter(container.getChildNodes(),
+            node -> name.equals(node.getQName().getLocalName()));
 
         // Can't combine this loop with the filter above because the filter is
         // lazily-applied by Iterables.filter.
@@ -746,19 +744,18 @@ public class ControllerContext implements SchemaContextListener {
             }
         }
 
-        final Iterable<ChoiceSchemaNode> choiceNodes = Iterables.filter(container.getChildNodes(), ChoiceSchemaNode.class);
-        final Iterable<Set<ChoiceCaseNode>> map = Iterables.transform(choiceNodes, CHOICE_FUNCTION);
-
-        final Iterable<ChoiceCaseNode> allCases = Iterables.<ChoiceCaseNode> concat(map);
-        for (final ChoiceCaseNode caze : allCases) {
+        final Iterable<ChoiceSchemaNode> choiceNodes = Iterables.filter(container.getChildNodes(),
+            ChoiceSchemaNode.class);
+        final Iterable<Set<ChoiceCaseNode>> map = Iterables.transform(choiceNodes, ChoiceSchemaNode::getCases);
+        for (final ChoiceCaseNode caze : Iterables.concat(map)) {
             collectInstanceDataNodeContainers(potentialSchemaNodes, caze, name);
         }
     }
 
     public static boolean isInstantiatedDataSchema(final DataSchemaNode node) {
-        return node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode
-                || node instanceof ContainerSchemaNode || node instanceof ListSchemaNode
-                || node instanceof AnyXmlSchemaNode;
+        return (node instanceof LeafSchemaNode) || (node instanceof LeafListSchemaNode)
+                || (node instanceof ContainerSchemaNode) || (node instanceof ListSchemaNode)
+                || (node instanceof AnyXmlSchemaNode);
     }
 
     private void addKeyValue(final HashMap<QName, Object> map, final DataSchemaNode node, final String uriValue,
@@ -770,15 +767,19 @@ public class ControllerContext implements SchemaContextListener {
         TypeDefinition<?> typedef = ((LeafSchemaNode) node).getType();
         final TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(typedef);
         if (baseType instanceof LeafrefTypeDefinition) {
-            typedef = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) baseType, globalSchema, node);
+            typedef = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) baseType, this.globalSchema,
+                node);
         }
-        Codec<Object, Object> codec = RestCodec.from(typedef, mountPoint);
+        final Codec<Object, Object> codec = RestCodec.from(typedef, mountPoint);
         Object decoded = codec.deserialize(urlDecoded);
         String additionalInfo = "";
         if (decoded == null) {
-            if ((baseType instanceof IdentityrefTypeDefinition)) {
-                decoded = toQName(urlDecoded, null);
-                additionalInfo = "For key which is of type identityref it should be in format module_name:identity_name.";
+            if ((typedef instanceof IdentityrefTypeDefinition)) {
+                final SchemaContext schemaContext =
+                        mountPoint == null ? this.globalSchema : mountPoint.getSchemaContext();
+                decoded = toQName(schemaContext, urlDecoded, null);
+                additionalInfo =
+                        "For key which is of type identityref it should be in format module_name:identity_name.";
             }
         }
 
@@ -818,26 +819,26 @@ public class ControllerContext implements SchemaContextListener {
         return str.substring(idx + 1);
     }
 
-    private QName toQName(final String name, final Date revisionDate) {
+    private QName toQName(final SchemaContext schemaContext, final String name, final Date revisionDate) {
         checkPreconditions();
         final String module = toModuleName(name);
         final String node = toNodeName(name);
-        final Module m = globalSchema.findModuleByName(module, revisionDate);
+        final Module m = schemaContext.findModuleByName(module, revisionDate);
         return m == null ? null : QName.create(m.getQNameModule(), node);
     }
 
     private static boolean isListOrContainer(final DataSchemaNode node) {
-        return node instanceof ListSchemaNode || node instanceof ContainerSchemaNode;
+        return (node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode);
     }
 
     public RpcDefinition getRpcDefinition(final String name, final Date revisionDate) {
-        final QName validName = toQName(name, revisionDate);
-        return validName == null ? null : qnameToRpc.get().get(validName);
+        final QName validName = toQName(this.globalSchema, name, revisionDate);
+        return validName == null ? null : this.qnameToRpc.get().get(validName);
     }
 
-    private RpcDefinition getRpcDefinition(final Module module, final String rpcName) {
-        QName rpcQName = QName.create(module.getQNameModule(), rpcName);
-        for (RpcDefinition rpcDefinition : module.getRpcs()) {
+    private static RpcDefinition getRpcDefinition(final Module module, final String rpcName) {
+        final QName rpcQName = QName.create(module.getQNameModule(), rpcName);
+        for (final RpcDefinition rpcDefinition : module.getRpcs()) {
             if (rpcQName.equals(rpcDefinition.getQName())) {
                 return rpcDefinition;
             }
@@ -856,16 +857,16 @@ public class ControllerContext implements SchemaContextListener {
             }
 
             // FIXME: still not completely atomic
-            qnameToRpc.set(ImmutableMap.copyOf(newMap));
+            this.qnameToRpc.set(ImmutableMap.copyOf(newMap));
             setGlobalSchema(context);
         }
     }
 
     public static List<String> urlPathArgsDecode(final Iterable<String> strings) {
         try {
-            final List<String> decodedPathArgs = new ArrayList<String>();
+            final List<String> decodedPathArgs = new ArrayList<>();
             for (final String pathArg : strings) {
-                final String _decode = URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET);
+                final String _decode = URLDecoder.decode(pathArg, URI_ENCODING_CHARSET.name());
                 decodedPathArgs.add(_decode);
             }
             return decodedPathArgs;
@@ -878,7 +879,7 @@ public class ControllerContext implements SchemaContextListener {
     public String urlPathArgDecode(final String pathArg) {
         if (pathArg != null) {
             try {
-                return URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET);
+                return URLDecoder.decode(pathArg, URI_ENCODING_CHARSET.name());
             } catch (final UnsupportedEncodingException e) {
                 throw new RestconfDocumentedException("Invalid URL path arg '" + pathArg + "': " + e.getMessage(),
                         ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
@@ -888,21 +889,22 @@ public class ControllerContext implements SchemaContextListener {
         return null;
     }
 
-    private CharSequence convertToRestconfIdentifier(final PathArgument argument, final DataSchemaNode node, final DOMMountPoint mount) {
+    private CharSequence convertToRestconfIdentifier(final PathArgument argument, final DataSchemaNode node,
+            final DOMMountPoint mount) {
         if (argument instanceof NodeIdentifier) {
             return convertToRestconfIdentifier((NodeIdentifier) argument, mount);
-        } else if (argument instanceof NodeIdentifierWithPredicates && node instanceof ListSchemaNode) {
-            return convertToRestconfIdentifierWithPredicates((NodeIdentifierWithPredicates) argument, (ListSchemaNode) node, mount);
-        } else if (argument != null && node != null) {
+        } else if ((argument instanceof NodeIdentifierWithPredicates) && (node instanceof ListSchemaNode)) {
+            return convertToRestconfIdentifierWithPredicates((NodeIdentifierWithPredicates) argument,
+                (ListSchemaNode) node, mount);
+        } else if ((argument != null) && (node != null)) {
             throw new IllegalArgumentException("Conversion of generic path argument is not supported");
         } else {
-            throw new IllegalArgumentException("Unhandled parameter types: "
-                    + Arrays.<Object> asList(argument, node).toString());
+            throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.asList(argument, node));
         }
     }
 
     private CharSequence convertToRestconfIdentifier(final NodeIdentifier argument, final DOMMountPoint node) {
-        return "/" + this.toRestconfIdentifier(argument.getNodeType(),node);
+        return "/" + toRestconfIdentifier(argument.getNodeType(),node);
     }
 
     private CharSequence convertToRestconfIdentifierWithPredicates(final NodeIdentifierWithPredicates argument,
@@ -928,7 +930,8 @@ public class ControllerContext implements SchemaContextListener {
                     }
 
                     try {
-                        Preconditions.checkState(listChild instanceof LeafSchemaNode, "List key has to consist of leaves");
+                        Preconditions.checkState(listChild instanceof LeafSchemaNode,
+                            "List key has to consist of leaves, not %s", listChild);
                         builder.append(toUriString(keyValues.get(key), (LeafSchemaNode)listChild, mount));
                     } catch (final UnsupportedEncodingException e) {
                         LOG.error("Error parsing URI: {}", keyValues.get(key), e);
@@ -942,28 +945,9 @@ public class ControllerContext implements SchemaContextListener {
         return builder.toString();
     }
 
-    private static DataSchemaNode childByQName(final Object container, final QName name) {
-        if (container instanceof ChoiceCaseNode) {
-            return childByQName((ChoiceCaseNode) container, name);
-        } else if (container instanceof ChoiceSchemaNode) {
-            return childByQName((ChoiceSchemaNode) container, name);
-        } else if (container instanceof ContainerSchemaNode) {
-            return childByQName((ContainerSchemaNode) container, name);
-        } else if (container instanceof ListSchemaNode) {
-            return childByQName((ListSchemaNode) container, name);
-        } else if (container instanceof DataSchemaNode) {
-            return childByQName((DataSchemaNode) container, name);
-        } else if (container instanceof Module) {
-            return childByQName((Module) container, name);
-        } else {
-            throw new IllegalArgumentException("Unhandled parameter types: "
-                    + Arrays.<Object> asList(container, name).toString());
-        }
-    }
-
     public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
         try {
-            return dataNormalizer.toNormalized(legacy);
+            return this.dataNormalizer.toNormalized(legacy);
         } catch (final NullPointerException e) {
             throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e);
         }
@@ -971,7 +955,7 @@ public class ControllerContext implements SchemaContextListener {
 
     public YangInstanceIdentifier toXpathRepresentation(final YangInstanceIdentifier instanceIdentifier) {
         try {
-            return dataNormalizer.toLegacy(instanceIdentifier);
+            return this.dataNormalizer.toLegacy(instanceIdentifier);
         } catch (final NullPointerException e) {
             throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e);
         } catch (final DataNormalizationException e) {
@@ -982,7 +966,7 @@ public class ControllerContext implements SchemaContextListener {
     public boolean isNodeMixin(final YangInstanceIdentifier path) {
         final DataNormalizationOperation<?> operation;
         try {
-            operation = dataNormalizer.getOperation(path);
+            operation = this.dataNormalizer.getOperation(path);
         } catch (final DataNormalizationException e) {
             throw new RestconfDocumentedException("Data normalizer failed. Normalization isn't possible", e);
         }
@@ -990,7 +974,7 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public DataNormalizationOperation<?> getRootOperation() {
-        return dataNormalizer.getRootOperation();
+        return this.dataNormalizer.getRootOperation();
     }
 
 }