Improve RESTCONF QName parsing speed
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.java
index b0916f4500d65d9dfb3291f4435e7c2aad31cc1b..8f9913d30dc1f119fd0a2c4a2761071e9fcc8a6d 100644 (file)
@@ -7,19 +7,30 @@
  */
 package org.opendaylight.controller.sal.restconf.impl;
 
+import com.google.common.base.Function;
+import com.google.common.base.Objects;
+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.BiMap;
+import com.google.common.collect.HashBiMap;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicReference;
 
 import javax.ws.rs.core.Response.Status;
 
@@ -36,6 +47,7 @@ import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdent
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
@@ -54,21 +66,8 @@ import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-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.BiMap;
-import com.google.common.collect.FluentIterable;
-import com.google.common.collect.HashBiMap;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
-
 public class ControllerContext implements SchemaContextListener {
-    private final static Logger LOG = LoggerFactory.getLogger( ControllerContext.class );
+    private final static Logger LOG = LoggerFactory.getLogger(ControllerContext.class);
 
     private final static ControllerContext INSTANCE = new ControllerContext();
 
@@ -82,20 +81,23 @@ public class ControllerContext implements SchemaContextListener {
 
     private final static String URI_ENCODING_CHAR_SET = "ISO-8859-1";
 
+    private static final Splitter SLASH_SPLITTER = Splitter.on('/');
+
     private final BiMap<URI, String> uriToModuleName = HashBiMap.<URI, String> create();
 
     private final Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
 
-    private final Map<QName, RpcDefinition> qnameToRpc = new ConcurrentHashMap<>();
+    private final AtomicReference<Map<QName, RpcDefinition>> qnameToRpc =
+            new AtomicReference<>(Collections.<QName, RpcDefinition>emptyMap());
 
     private volatile SchemaContext globalSchema;
     private volatile MountService mountService;
 
-    public void setGlobalSchema( final SchemaContext globalSchema ) {
+    public void setGlobalSchema(final SchemaContext globalSchema) {
         this.globalSchema = globalSchema;
     }
 
-    public void setMountService( final MountService mountService ) {
+    public void setMountService(final MountService mountService) {
         this.mountService = mountService;
     }
 
@@ -107,164 +109,157 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     private void checkPreconditions() {
-        if( globalSchema == null ) {
-            throw new RestconfDocumentedException( Status.SERVICE_UNAVAILABLE );
+        if (globalSchema == null) {
+            throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
         }
     }
 
-    public void setSchemas( final SchemaContext schemas ) {
-        this.onGlobalContextUpdated( schemas );
+    public void setSchemas(final SchemaContext schemas) {
+        this.onGlobalContextUpdated(schemas);
     }
 
-    public InstanceIdWithSchemaNode toInstanceIdentifier( final String restconfInstance ) {
-        return this.toIdentifier( restconfInstance, false );
+    public InstanceIdWithSchemaNode toInstanceIdentifier(final String restconfInstance) {
+        return this.toIdentifier(restconfInstance, false);
     }
 
-    public InstanceIdWithSchemaNode toMountPointIdentifier( final String restconfInstance ) {
-        return this.toIdentifier( restconfInstance, true );
+    public InstanceIdWithSchemaNode toMountPointIdentifier(final String restconfInstance) {
+        return this.toIdentifier(restconfInstance, true);
     }
 
-    private InstanceIdWithSchemaNode toIdentifier( final String restconfInstance,
-            final boolean toMountPointIdentifier ) {
+    private InstanceIdWithSchemaNode toIdentifier(final String restconfInstance, final boolean toMountPointIdentifier) {
         this.checkPreconditions();
 
-        Iterable<String> split = Splitter.on( "/" ).split( restconfInstance );
-        final ArrayList<String> encodedPathArgs = Lists.<String> newArrayList( split );
-        final List<String> pathArgs = this.urlPathArgsDecode( encodedPathArgs );
-        this.omitFirstAndLastEmptyString( pathArgs );
-        if( pathArgs.isEmpty() ) {
+        final List<String> pathArgs = urlPathArgsDecode(SLASH_SPLITTER.split(restconfInstance));
+        omitFirstAndLastEmptyString(pathArgs);
+        if (pathArgs.isEmpty()) {
             return null;
         }
 
         String first = pathArgs.iterator().next();
-        final String startModule = ControllerContext.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 String startModule = ControllerContext.toModuleName(first);
+        if (startModule == null) {
+            throw new RestconfDocumentedException("First node in URI has to be in format \"moduleName:nodeName\"",
+                    ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
 
         InstanceIdentifierBuilder builder = InstanceIdentifier.builder();
-        Module latestModule = this.getLatestModule( globalSchema, startModule );
-        InstanceIdWithSchemaNode iiWithSchemaNode = this.collectPathArguments( builder, pathArgs,
-                latestModule, null, toMountPointIdentifier );
+        Module latestModule = this.getLatestModule(globalSchema, startModule);
+        InstanceIdWithSchemaNode iiWithSchemaNode = this.collectPathArguments(builder, pathArgs, latestModule, null,
+                toMountPointIdentifier);
 
-        if( iiWithSchemaNode == null ) {
-            throw new RestconfDocumentedException(
-                    "URI has bad format", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+        if (iiWithSchemaNode == null) {
+            throw new RestconfDocumentedException("URI has bad format", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
 
         return iiWithSchemaNode;
     }
 
-    private List<String> omitFirstAndLastEmptyString( final List<String> list ) {
-        if( list.isEmpty() ) {
+    private static List<String> omitFirstAndLastEmptyString(final List<String> list) {
+        if (list.isEmpty()) {
             return list;
         }
 
         String head = list.iterator().next();
-        if( head.isEmpty() ) {
-            list.remove( 0 );
+        if (head.isEmpty()) {
+            list.remove(0);
         }
 
-        if( list.isEmpty() ) {
+        if (list.isEmpty()) {
             return list;
         }
 
-        String last = list.get( list.size() - 1 );
-        if( last.isEmpty() ) {
-            list.remove( list.size() - 1 );
+        String last = list.get(list.size() - 1);
+        if (last.isEmpty()) {
+            list.remove(list.size() - 1);
         }
 
         return list;
     }
 
-    private Module getLatestModule( final SchemaContext schema, final String moduleName ) {
-        Preconditions.checkArgument( schema != null );
-        Preconditions.checkArgument( moduleName != null && !moduleName.isEmpty() );
+    private Module getLatestModule(final SchemaContext schema, final String moduleName) {
+        Preconditions.checkArgument(schema != null);
+        Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
 
         Predicate<Module> filter = new Predicate<Module>() {
             @Override
-            public boolean apply( final Module m ) {
-                return Objects.equal( m.getName(), moduleName );
+            public boolean apply(final Module m) {
+                return Objects.equal(m.getName(), moduleName);
             }
         };
 
-        Iterable<Module> modules = Iterables.filter( schema.getModules(), filter );
-        return this.filterLatestModule( modules );
+        Iterable<Module> modules = Iterables.filter(schema.getModules(), filter);
+        return this.filterLatestModule(modules);
     }
 
-    private Module filterLatestModule( final Iterable<Module> modules ) {
+    private Module filterLatestModule(final Iterable<Module> modules) {
         Module latestModule = modules.iterator().hasNext() ? modules.iterator().next() : null;
-        for( final Module module : modules ) {
-            if( module.getRevision().after( latestModule.getRevision() ) ) {
+        for (final Module module : modules) {
+            if (module.getRevision().after(latestModule.getRevision())) {
                 latestModule = module;
             }
         }
         return latestModule;
     }
 
-    public Module findModuleByName( final String moduleName ) {
+    public Module findModuleByName(final String moduleName) {
         this.checkPreconditions();
-        Preconditions.checkArgument( moduleName != null && !moduleName.isEmpty() );
-        return this.getLatestModule( globalSchema, moduleName );
+        Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
+        return this.getLatestModule(globalSchema, moduleName);
     }
 
-    public Module findModuleByName( final MountInstance mountPoint, final String moduleName ) {
-        Preconditions.checkArgument( moduleName != null && mountPoint != null );
+    public Module findModuleByName(final MountInstance mountPoint, final String moduleName) {
+        Preconditions.checkArgument(moduleName != null && mountPoint != null);
 
         final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
-        return mountPointSchema == null ? null : this.getLatestModule( mountPointSchema, moduleName );
+        return mountPointSchema == null ? null : this.getLatestModule(mountPointSchema, moduleName);
     }
 
-    public Module findModuleByNamespace( final URI namespace ) {
+    public Module findModuleByNamespace(final URI namespace) {
         this.checkPreconditions();
-        Preconditions.checkArgument( namespace != null );
+        Preconditions.checkArgument(namespace != null);
 
-        final Set<Module> moduleSchemas = globalSchema.findModuleByNamespace( namespace );
-        return moduleSchemas == null ? null : this.filterLatestModule( moduleSchemas );
+        final Set<Module> moduleSchemas = globalSchema.findModuleByNamespace(namespace);
+        return moduleSchemas == null ? null : this.filterLatestModule(moduleSchemas);
     }
 
-    public Module findModuleByNamespace( final MountInstance mountPoint, final URI namespace ) {
-        Preconditions.checkArgument( namespace != null && mountPoint != null );
+    public Module findModuleByNamespace(final MountInstance mountPoint, final URI namespace) {
+        Preconditions.checkArgument(namespace != null && mountPoint != null);
 
         final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
-        Set<Module> moduleSchemas = mountPointSchema == null ? null :
-            mountPointSchema.findModuleByNamespace( namespace );
-        return moduleSchemas == null ? null : this.filterLatestModule( moduleSchemas );
+        Set<Module> moduleSchemas = mountPointSchema == null ? null : mountPointSchema.findModuleByNamespace(namespace);
+        return moduleSchemas == null ? null : this.filterLatestModule(moduleSchemas);
     }
 
-    public Module findModuleByNameAndRevision( final QName module ) {
+    public Module findModuleByNameAndRevision(final QName module) {
         this.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 globalSchema.findModuleByName(module.getLocalName(), module.getRevision());
     }
 
-    public Module findModuleByNameAndRevision( final MountInstance mountPoint, final QName module ) {
+    public Module findModuleByNameAndRevision(final MountInstance mountPoint, final QName module) {
         this.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);
 
         SchemaContext schemaContext = mountPoint.getSchemaContext();
-        return schemaContext == null ? null :
-            schemaContext.findModuleByName( module.getLocalName(), module.getRevision() );
+        return schemaContext == null ? null : schemaContext.findModuleByName(module.getLocalName(),
+                module.getRevision());
     }
 
-    public DataNodeContainer getDataNodeContainerFor( final InstanceIdentifier path ) {
+    public DataNodeContainer getDataNodeContainerFor(final InstanceIdentifier path) {
         this.checkPreconditions();
 
-        final List<PathArgument> elements = path.getPath();
+        final Iterable<PathArgument> elements = path.getPathArguments();
         PathArgument head = elements.iterator().next();
         final QName startQName = head.getNodeType();
-        final Module initialModule = globalSchema.findModuleByNamespaceAndRevision(
-                startQName.getNamespace(), startQName.getRevision() );
+        final Module initialModule = globalSchema.findModuleByNamespaceAndRevision(startQName.getNamespace(),
+                startQName.getRevision());
         DataNodeContainer node = initialModule;
-        for( final PathArgument element : elements ) {
+        for (final PathArgument element : elements) {
             QName _nodeType = element.getNodeType();
-            final DataSchemaNode potentialNode = ControllerContext.childByQName( node, _nodeType );
-            if( potentialNode == null || !this.isListOrContainer( potentialNode ) ) {
+            final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
+            if (potentialNode == null || !this.isListOrContainer(potentialNode)) {
                 return null;
             }
             node = (DataNodeContainer) potentialNode;
@@ -273,68 +268,68 @@ public class ControllerContext implements SchemaContextListener {
         return node;
     }
 
-    public String toFullRestconfIdentifier( final InstanceIdentifier path ) {
+    public String toFullRestconfIdentifier(final InstanceIdentifier path) {
         this.checkPreconditions();
 
-        final List<PathArgument> elements = path.getPath();
+        final Iterable<PathArgument> elements = path.getPathArguments();
         final StringBuilder builder = new StringBuilder();
         PathArgument head = elements.iterator().next();
         final QName startQName = head.getNodeType();
-        final Module initialModule = globalSchema.findModuleByNamespaceAndRevision(
-                startQName.getNamespace(), startQName.getRevision() );
+        final Module initialModule = globalSchema.findModuleByNamespaceAndRevision(startQName.getNamespace(),
+                startQName.getRevision());
         DataNodeContainer node = initialModule;
-        for( final PathArgument element : elements ) {
+        for (final PathArgument element : elements) {
             QName _nodeType = element.getNodeType();
-            final DataSchemaNode potentialNode = ControllerContext.childByQName( node, _nodeType );
-            if( !this.isListOrContainer( potentialNode ) ) {
+            final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
+            if (!this.isListOrContainer(potentialNode)) {
                 return null;
             }
             node = ((DataNodeContainer) potentialNode);
-            builder.append( this.convertToRestconfIdentifier( element, node ) );
+            builder.append(this.convertToRestconfIdentifier(element, node));
         }
 
         return builder.toString();
     }
 
-    public String findModuleNameByNamespace( final URI namespace ) {
+    public String findModuleNameByNamespace(final URI namespace) {
         this.checkPreconditions();
 
-        String moduleName = this.uriToModuleName.get( namespace );
-        if( moduleName == null ) {
-            final Module module = this.findModuleByNamespace( namespace );
-            if( module != null ) {
+        String moduleName = this.uriToModuleName.get(namespace);
+        if (moduleName == null) {
+            final Module module = this.findModuleByNamespace(namespace);
+            if (module != null) {
                 moduleName = module.getName();
-                this.uriToModuleName.put( namespace, moduleName );
+                this.uriToModuleName.put(namespace, moduleName);
             }
         }
 
         return moduleName;
     }
 
-    public String findModuleNameByNamespace( final MountInstance mountPoint, final URI namespace ) {
-        final Module module = this.findModuleByNamespace( mountPoint, namespace );
+    public String findModuleNameByNamespace(final MountInstance mountPoint, final URI namespace) {
+        final Module module = this.findModuleByNamespace(mountPoint, namespace);
         return module == null ? null : module.getName();
     }
 
-    public URI findNamespaceByModuleName( final String moduleName ) {
-        URI namespace = this.moduleNameToUri.get( moduleName );
-        if( namespace == null ) {
-            Module module = this.findModuleByName( moduleName );
-            if( module != null ) {
+    public URI findNamespaceByModuleName(final String moduleName) {
+        URI namespace = this.moduleNameToUri.get(moduleName);
+        if (namespace == null) {
+            Module module = this.findModuleByName(moduleName);
+            if (module != null) {
                 URI _namespace = module.getNamespace();
                 namespace = _namespace;
-                this.uriToModuleName.put( namespace, moduleName );
+                this.uriToModuleName.put(namespace, moduleName);
             }
         }
         return namespace;
     }
 
-    public URI findNamespaceByModuleName( final MountInstance mountPoint, final String moduleName ) {
-        final Module module = this.findModuleByName( mountPoint, moduleName );
+    public URI findNamespaceByModuleName(final MountInstance mountPoint, final String moduleName) {
+        final Module module = this.findModuleByName(mountPoint, moduleName);
         return module == null ? null : module.getNamespace();
     }
 
-    public Set<Module> getAllModules( final MountInstance mountPoint ) {
+    public Set<Module> getAllModules(final MountInstance mountPoint) {
         this.checkPreconditions();
 
         SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
@@ -346,55 +341,55 @@ public class ControllerContext implements SchemaContextListener {
         return globalSchema.getModules();
     }
 
-    public CharSequence toRestconfIdentifier( final QName qname ) {
+    public CharSequence toRestconfIdentifier(final QName qname) {
         this.checkPreconditions();
 
-        String module = this.uriToModuleName.get( qname.getNamespace() );
-        if( module == null ) {
-            final Module moduleSchema = globalSchema.findModuleByNamespaceAndRevision(
-                    qname.getNamespace(), qname.getRevision() );
-            if( moduleSchema == null ) {
+        String module = this.uriToModuleName.get(qname.getNamespace());
+        if (module == null) {
+            final Module moduleSchema = globalSchema.findModuleByNamespaceAndRevision(qname.getNamespace(),
+                    qname.getRevision());
+            if (moduleSchema == null) {
                 return null;
             }
 
-            this.uriToModuleName.put( qname.getNamespace(), moduleSchema.getName() );
+            this.uriToModuleName.put(qname.getNamespace(), moduleSchema.getName());
             module = moduleSchema.getName();
         }
 
         StringBuilder builder = new StringBuilder();
-        builder.append( module );
-        builder.append( ":" );
-        builder.append( qname.getLocalName() );
+        builder.append(module);
+        builder.append(":");
+        builder.append(qname.getLocalName());
         return builder.toString();
     }
 
-    public CharSequence toRestconfIdentifier( final MountInstance mountPoint, final QName qname ) {
-        if( mountPoint == null ) {
+    public CharSequence toRestconfIdentifier(final MountInstance mountPoint, final QName qname) {
+        if (mountPoint == null) {
             return null;
         }
 
         SchemaContext schemaContext = mountPoint.getSchemaContext();
 
-        final Module moduleSchema = schemaContext.findModuleByNamespaceAndRevision(
-                qname.getNamespace(), qname.getRevision() );
-        if( moduleSchema == null ) {
+        final Module moduleSchema = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
+                qname.getRevision());
+        if (moduleSchema == null) {
             return null;
         }
 
         StringBuilder builder = new StringBuilder();
-        builder.append( moduleSchema.getName() );
-        builder.append( ":" );
-        builder.append( qname.getLocalName() );
+        builder.append(moduleSchema.getName());
+        builder.append(":");
+        builder.append(qname.getLocalName());
         return builder.toString();
     }
 
     public Module getRestconfModule() {
-        return findModuleByNameAndRevision( Draft02.RestConfModule.IETF_RESTCONF_QNAME );
+        return findModuleByNameAndRevision(Draft02.RestConfModule.IETF_RESTCONF_QNAME);
     }
 
     public DataSchemaNode getRestconfModuleErrorsSchemaNode() {
         Module restconfModule = getRestconfModule();
-        if( restconfModule == null ) {
+        if (restconfModule == null) {
             return null;
         }
 
@@ -403,8 +398,7 @@ public class ControllerContext implements SchemaContextListener {
         final Predicate<GroupingDefinition> filter = new Predicate<GroupingDefinition>() {
             @Override
             public boolean apply(final GroupingDefinition g) {
-                return Objects.equal(g.getQName().getLocalName(),
-                        Draft02.RestConfModule.ERRORS_GROUPING_SCHEMA_NODE);
+                return Objects.equal(g.getQName().getLocalName(), Draft02.RestConfModule.ERRORS_GROUPING_SCHEMA_NODE);
             }
         };
 
@@ -412,20 +406,18 @@ public class ControllerContext implements SchemaContextListener {
 
         final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
 
-        List<DataSchemaNode> instanceDataChildrenByName =
-                this.findInstanceDataChildrenByName(restconfGrouping,
-                        Draft02.RestConfModule.ERRORS_CONTAINER_SCHEMA_NODE);
+        List<DataSchemaNode> instanceDataChildrenByName = this.findInstanceDataChildrenByName(restconfGrouping,
+                Draft02.RestConfModule.ERRORS_CONTAINER_SCHEMA_NODE);
         return Iterables.getFirst(instanceDataChildrenByName, null);
     }
 
-    public DataSchemaNode getRestconfModuleRestConfSchemaNode( final Module inRestconfModule,
-            final String schemaNodeName ) {
+    public DataSchemaNode getRestconfModuleRestConfSchemaNode(final Module inRestconfModule, final String schemaNodeName) {
         Module restconfModule = inRestconfModule;
-        if( restconfModule == null ) {
+        if (restconfModule == null) {
             restconfModule = getRestconfModule();
         }
 
-        if( restconfModule == null ) {
+        if (restconfModule == null) {
             return null;
         }
 
@@ -434,8 +426,7 @@ public class ControllerContext implements SchemaContextListener {
         final Predicate<GroupingDefinition> filter = new Predicate<GroupingDefinition>() {
             @Override
             public boolean apply(final GroupingDefinition g) {
-                return Objects.equal(g.getQName().getLocalName(),
-                        Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE);
+                return Objects.equal(g.getQName().getLocalName(), Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE);
             }
         };
 
@@ -443,61 +434,49 @@ public class ControllerContext implements SchemaContextListener {
 
         final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
 
-        List<DataSchemaNode> instanceDataChildrenByName =
-                this.findInstanceDataChildrenByName(restconfGrouping,
-                        Draft02.RestConfModule.RESTCONF_CONTAINER_SCHEMA_NODE);
+        List<DataSchemaNode> instanceDataChildrenByName = this.findInstanceDataChildrenByName(restconfGrouping,
+                Draft02.RestConfModule.RESTCONF_CONTAINER_SCHEMA_NODE);
         final DataSchemaNode restconfContainer = Iterables.getFirst(instanceDataChildrenByName, null);
 
         if (Objects.equal(schemaNodeName, Draft02.RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE)) {
-            List<DataSchemaNode> instances =
-                    this.findInstanceDataChildrenByName(((DataNodeContainer) restconfContainer),
-                            Draft02.RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE);
+            List<DataSchemaNode> instances = this.findInstanceDataChildrenByName(
+                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        }
-        else if(Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE)) {
-            List<DataSchemaNode> instances =
-                    this.findInstanceDataChildrenByName(((DataNodeContainer) restconfContainer),
-                            Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
+        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE)) {
+            List<DataSchemaNode> instances = this.findInstanceDataChildrenByName(
+                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        }
-        else if(Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE)) {
-            List<DataSchemaNode> instances =
-                    this.findInstanceDataChildrenByName(((DataNodeContainer) restconfContainer),
-                            Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
+        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE)) {
+            List<DataSchemaNode> instances = this.findInstanceDataChildrenByName(
+                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
             final DataSchemaNode modules = Iterables.getFirst(instances, null);
             instances = this.findInstanceDataChildrenByName(((DataNodeContainer) modules),
                     Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        }
-        else if(Objects.equal(schemaNodeName, Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE)) {
-            List<DataSchemaNode> instances =
-                    this.findInstanceDataChildrenByName(((DataNodeContainer) restconfContainer),
-                            Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
+        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE)) {
+            List<DataSchemaNode> instances = this.findInstanceDataChildrenByName(
+                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        }
-        else if(Objects.equal(schemaNodeName, Draft02.RestConfModule.MODULE_LIST_SCHEMA_NODE)) {
-            List<DataSchemaNode> instances =
-                    this.findInstanceDataChildrenByName(((DataNodeContainer) restconfContainer),
-                            Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
+        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.MODULE_LIST_SCHEMA_NODE)) {
+            List<DataSchemaNode> instances = this.findInstanceDataChildrenByName(
+                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
             final DataSchemaNode modules = Iterables.getFirst(instances, null);
             instances = this.findInstanceDataChildrenByName(((DataNodeContainer) modules),
                     Draft02.RestConfModule.MODULE_LIST_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
-        }
-        else if(Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE)) {
-            List<DataSchemaNode> instances =
-                    this.findInstanceDataChildrenByName(((DataNodeContainer) restconfContainer),
-                            Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
+        } else if (Objects.equal(schemaNodeName, Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE)) {
+            List<DataSchemaNode> instances = this.findInstanceDataChildrenByName(
+                    ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
         }
 
         return null;
     }
 
-    private static DataSchemaNode childByQName( final ChoiceNode container, final QName name ) {
-        for( final ChoiceCaseNode caze : container.getCases() ) {
-            final DataSchemaNode ret = ControllerContext.childByQName( caze, name );
-            if( ret != null ) {
+    private static DataSchemaNode childByQName(final ChoiceNode container, final QName name) {
+        for (final ChoiceCaseNode caze : container.getCases()) {
+            final DataSchemaNode ret = ControllerContext.childByQName(caze, name);
+            if (ret != null) {
                 return ret;
             }
         }
@@ -505,34 +484,34 @@ public class ControllerContext implements SchemaContextListener {
         return null;
     }
 
-    private static DataSchemaNode childByQName( final ChoiceCaseNode container, final QName name ) {
-        return container.getDataChildByName( name );
+    private static DataSchemaNode childByQName(final ChoiceCaseNode container, final QName name) {
+        return container.getDataChildByName(name);
     }
 
-    private static DataSchemaNode childByQName( final ContainerSchemaNode container, final QName name ) {
-        return ControllerContext.dataNodeChildByQName( container, name );
+    private static DataSchemaNode childByQName(final ContainerSchemaNode container, final QName name) {
+        return ControllerContext.dataNodeChildByQName(container, name);
     }
 
-    private static DataSchemaNode childByQName( final ListSchemaNode container, final QName name ) {
-        return ControllerContext.dataNodeChildByQName( container, name );
+    private static DataSchemaNode childByQName(final ListSchemaNode container, final QName name) {
+        return ControllerContext.dataNodeChildByQName(container, name);
     }
 
-    private static DataSchemaNode childByQName( final Module container, final QName name ) {
-        return ControllerContext.dataNodeChildByQName( container, name );
+    private static DataSchemaNode childByQName(final Module container, final QName name) {
+        return ControllerContext.dataNodeChildByQName(container, name);
     }
 
-    private static DataSchemaNode childByQName( final DataSchemaNode container, final QName name ) {
+    private static DataSchemaNode childByQName(final DataSchemaNode container, final QName name) {
         return null;
     }
 
-    private static DataSchemaNode dataNodeChildByQName( final DataNodeContainer container, final QName name ) {
-        DataSchemaNode ret = container.getDataChildByName( name );
-        if( ret == null ) {
-            for( final DataSchemaNode node : container.getChildNodes() ) {
-                if( (node instanceof ChoiceCaseNode) ) {
+    private static DataSchemaNode dataNodeChildByQName(final DataNodeContainer container, final QName name) {
+        DataSchemaNode ret = container.getDataChildByName(name);
+        if (ret == null) {
+            for (final DataSchemaNode node : container.getChildNodes()) {
+                if ((node instanceof ChoiceCaseNode)) {
                     final ChoiceCaseNode caseNode = ((ChoiceCaseNode) node);
-                    DataSchemaNode childByQName = ControllerContext.childByQName( caseNode, name );
-                    if( childByQName != null ) {
+                    DataSchemaNode childByQName = ControllerContext.childByQName(caseNode, name);
+                    if (childByQName != null) {
                         return childByQName;
                     }
                 }
@@ -541,462 +520,408 @@ public class ControllerContext implements SchemaContextListener {
         return ret;
     }
 
-    private String toUriString( final Object object ) throws UnsupportedEncodingException {
-        return object == null ? "" :
-            URLEncoder.encode( object.toString(), ControllerContext.URI_ENCODING_CHAR_SET );
+    private String toUriString(final Object object) throws UnsupportedEncodingException {
+        return object == null ? "" : URLEncoder.encode(object.toString(), ControllerContext.URI_ENCODING_CHAR_SET);
     }
 
-    private InstanceIdWithSchemaNode collectPathArguments( final InstanceIdentifierBuilder builder,
+    private InstanceIdWithSchemaNode collectPathArguments(final InstanceIdentifierBuilder builder,
             final List<String> strings, final DataNodeContainer parentNode, final MountInstance mountPoint,
-            final boolean returnJustMountPoint ) {
-        Preconditions.<List<String>> checkNotNull( strings );
+            final boolean returnJustMountPoint) {
+        Preconditions.<List<String>> checkNotNull(strings);
 
-        if( parentNode == null ) {
+        if (parentNode == null) {
             return null;
         }
 
-        if( strings.isEmpty() ) {
-            return new InstanceIdWithSchemaNode( builder.toInstance(),
-                    ((DataSchemaNode) parentNode), mountPoint );
+        if (strings.isEmpty()) {
+            return new InstanceIdWithSchemaNode(builder.toInstance(), ((DataSchemaNode) parentNode), mountPoint);
         }
 
         String head = strings.iterator().next();
-        final String nodeName = this.toNodeName( head );
-        final String moduleName = ControllerContext.toModuleName( head );
+        final String nodeName = toNodeName(head);
+        final String moduleName = ControllerContext.toModuleName(head);
 
         DataSchemaNode targetNode = null;
-        if( !Strings.isNullOrEmpty( moduleName ) ) {
-            if( Objects.equal( moduleName, ControllerContext.MOUNT_MODULE ) &&
-                    Objects.equal( nodeName, ControllerContext.MOUNT_NODE ) ) {
-                if( mountPoint != null ) {
-                    throw new RestconfDocumentedException(
-                            "Restconf supports just one mount point in URI.",
-                            ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED );
+        if (!Strings.isNullOrEmpty(moduleName)) {
+            if (Objects.equal(moduleName, ControllerContext.MOUNT_MODULE)
+                    && Objects.equal(nodeName, ControllerContext.MOUNT_NODE)) {
+                if (mountPoint != null) {
+                    throw new RestconfDocumentedException("Restconf supports just one mount point in URI.",
+                            ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
                 }
 
-                if( mountService == null ) {
+                if (mountService == null) {
                     throw new RestconfDocumentedException(
                             "MountService was not found. Finding behind mount points does not work.",
-                            ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED );
+                            ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
                 }
 
                 final InstanceIdentifier partialPath = builder.toInstance();
-                final MountInstance mount = mountService.getMountPoint( partialPath );
-                if( mount == null ) {
-                    LOG.debug( "Instance identifier to missing mount point: {}", partialPath );
-                    throw new RestconfDocumentedException(
-                            "Mount point does not exist.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT );
+                final MountInstance mount = mountService.getMountPoint(partialPath);
+                if (mount == null) {
+                    LOG.debug("Instance identifier to missing mount point: {}", partialPath);
+                    throw new RestconfDocumentedException("Mount point does not exist.", ErrorType.PROTOCOL,
+                            ErrorTag.UNKNOWN_ELEMENT);
                 }
 
                 final SchemaContext mountPointSchema = mount.getSchemaContext();
-                if( mountPointSchema == null ) {
-                    throw new RestconfDocumentedException(
-                            "Mount point does not contain any schema with modules.",
-                            ErrorType.APPLICATION, ErrorTag.UNKNOWN_ELEMENT );
+                if (mountPointSchema == null) {
+                    throw new RestconfDocumentedException("Mount point does not contain any schema with modules.",
+                            ErrorType.APPLICATION, ErrorTag.UNKNOWN_ELEMENT);
                 }
 
-                if( returnJustMountPoint ) {
+                if (returnJustMountPoint) {
                     InstanceIdentifier instance = InstanceIdentifier.builder().toInstance();
-                    return new InstanceIdWithSchemaNode( instance, mountPointSchema, mount );
+                    return new InstanceIdWithSchemaNode(instance, mountPointSchema, mount);
                 }
 
-                if( strings.size() == 1 ) {
+                if (strings.size() == 1) {
                     InstanceIdentifier instance = InstanceIdentifier.builder().toInstance();
-                    return new InstanceIdWithSchemaNode( instance, mountPointSchema, mount );
+                    return new InstanceIdWithSchemaNode(instance, mountPointSchema, mount);
                 }
 
-                final String moduleNameBehindMountPoint = toModuleName(  strings.get( 1 ) );
-                if( moduleNameBehindMountPoint == null ) {
+                final String moduleNameBehindMountPoint = toModuleName(strings.get(1));
+                if (moduleNameBehindMountPoint == null) {
                     throw new RestconfDocumentedException(
                             "First node after mount point in URI has to be in format \"moduleName:nodeName\"",
-                            ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+                            ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
                 }
 
-                final Module moduleBehindMountPoint = this.getLatestModule( mountPointSchema,
-                        moduleNameBehindMountPoint );
-                if( moduleBehindMountPoint == null ) {
-                    throw new RestconfDocumentedException(
-                            "\"" +moduleName + "\" module does not exist in mount point.",
-                            ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT );
+                final Module moduleBehindMountPoint = this
+                        .getLatestModule(mountPointSchema, moduleNameBehindMountPoint);
+                if (moduleBehindMountPoint == null) {
+                    throw new RestconfDocumentedException("\"" + moduleName
+                            + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                 }
 
-                List<String> subList = strings.subList( 1, strings.size() );
-                return this.collectPathArguments( InstanceIdentifier.builder(), subList, moduleBehindMountPoint,
-                        mount, returnJustMountPoint );
+                List<String> subList = strings.subList(1, strings.size());
+                return this.collectPathArguments(InstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount,
+                        returnJustMountPoint);
             }
 
             Module module = null;
-            if( mountPoint == null ) {
-                module = this.getLatestModule( globalSchema, moduleName );
-                if( module == null ) {
-                    throw new RestconfDocumentedException(
-                            "\"" + moduleName + "\" module does not exist.",
-                            ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT );
+            if (mountPoint == null) {
+                module = this.getLatestModule(globalSchema, moduleName);
+                if (module == null) {
+                    throw new RestconfDocumentedException("\"" + moduleName + "\" module does not exist.",
+                            ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                 }
-            }
-            else {
+            } else {
                 SchemaContext schemaContext = mountPoint.getSchemaContext();
-                module = schemaContext == null ? null :
-                    this.getLatestModule( schemaContext, moduleName );
-                if( module == null ) {
-                    throw new RestconfDocumentedException(
-                            "\"" + moduleName + "\" module does not exist in mount point.",
-                            ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT );
+                module = schemaContext == null ? null : this.getLatestModule(schemaContext, moduleName);
+                if (module == null) {
+                    throw new RestconfDocumentedException("\"" + moduleName
+                            + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                 }
             }
 
-            targetNode = this.findInstanceDataChildByNameAndNamespace(
-                    parentNode, nodeName, module.getNamespace() );
-            if( targetNode == null ) {
-                throw new RestconfDocumentedException(
-                        "URI has bad format. Possible reasons:\n" +
-                                " 1. \"" + head + "\" was not found in parent data node.\n" +
-                                " 2. \"" + head + "\" is behind mount point. Then it should be in format \"/" +
-                                MOUNT + "/" + head + "\".", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+            targetNode = this.findInstanceDataChildByNameAndNamespace(parentNode, nodeName, module.getNamespace());
+            if (targetNode == null) {
+                throw new RestconfDocumentedException("URI has bad format. Possible reasons:\n" + " 1. \"" + head
+                        + "\" was not found in parent data node.\n" + " 2. \"" + head
+                        + "\" is behind mount point. Then it should be in format \"/" + MOUNT + "/" + head + "\".",
+                        ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
             }
         } else {
-            final List<DataSchemaNode> potentialSchemaNodes =
-                    this.findInstanceDataChildrenByName( parentNode, nodeName );
-            if( potentialSchemaNodes.size() > 1 ) {
+            final List<DataSchemaNode> potentialSchemaNodes = this.findInstanceDataChildrenByName(parentNode, nodeName);
+            if (potentialSchemaNodes.size() > 1) {
                 final StringBuilder strBuilder = new StringBuilder();
-                for( final DataSchemaNode potentialNodeSchema : potentialSchemaNodes ) {
-                    strBuilder.append( "   " )
-                    .append( potentialNodeSchema.getQName().getNamespace() )
-                    .append( "\n" );
+                for (final DataSchemaNode potentialNodeSchema : potentialSchemaNodes) {
+                    strBuilder.append("   ").append(potentialNodeSchema.getQName().getNamespace()).append("\n");
                 }
 
                 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\"." +
-                        "\nThe node is added as augment from modules with namespaces:\n" +
-                        strBuilder.toString(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+                        "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\"."
+                                + "\nThe node is added as augment from modules with namespaces:\n"
+                                + strBuilder.toString(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
             }
 
-            if( potentialSchemaNodes.isEmpty() ) {
-                throw new RestconfDocumentedException(
-                        "\"" + nodeName + "\" in URI was not found in parent data node",
-                        ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT );
+            if (potentialSchemaNodes.isEmpty()) {
+                throw new RestconfDocumentedException("\"" + nodeName + "\" in URI was not found in parent data node",
+                        ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
             }
 
             targetNode = potentialSchemaNodes.iterator().next();
         }
 
-        if( !this.isListOrContainer( targetNode ) ) {
-            throw new RestconfDocumentedException(
-                    "URI has bad format. Node \"" + head + "\" must be Container or List yang type.",
-                    ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+        if (!this.isListOrContainer(targetNode)) {
+            throw new RestconfDocumentedException("URI has bad format. Node \"" + head
+                    + "\" must be Container or List yang type.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
 
         int consumed = 1;
-        if( (targetNode instanceof ListSchemaNode) ) {
+        if ((targetNode instanceof ListSchemaNode)) {
             final ListSchemaNode listNode = ((ListSchemaNode) targetNode);
             final int keysSize = listNode.getKeyDefinition().size();
-            if( (strings.size() - consumed) < keysSize ) {
-                throw new RestconfDocumentedException(
-                        "Missing key for list \"" + listNode.getQName().getLocalName() + "\".",
-                        ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+            if ((strings.size() - consumed) < keysSize) {
+                throw new RestconfDocumentedException("Missing key for list \"" + listNode.getQName().getLocalName()
+                        + "\".", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
             }
 
-            final List<String> uriKeyValues = strings.subList( consumed, consumed + keysSize );
+            final List<String> uriKeyValues = strings.subList(consumed, consumed + keysSize);
             final HashMap<QName, Object> keyValues = new HashMap<QName, Object>();
             int i = 0;
-            for( final QName key : listNode.getKeyDefinition() ) {
+            for (final QName key : listNode.getKeyDefinition()) {
                 {
-                    final String uriKeyValue = uriKeyValues.get( i );
-                    if( uriKeyValue.equals( NULL_VALUE ) ) {
-                        throw new RestconfDocumentedException(
-                                "URI has bad format. List \"" + listNode.getQName().getLocalName() +
-                                "\" cannot contain \"null\" value as a key.",
-                                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+                    final String uriKeyValue = uriKeyValues.get(i);
+                    if (uriKeyValue.equals(NULL_VALUE)) {
+                        throw new RestconfDocumentedException("URI has bad format. List \""
+                                + listNode.getQName().getLocalName() + "\" cannot contain \"null\" value as a key.",
+                                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
                     }
 
-                    this.addKeyValue( keyValues, listNode.getDataChildByName( key ),
-                            uriKeyValue, mountPoint );
+                    this.addKeyValue(keyValues, listNode.getDataChildByName(key), uriKeyValue, mountPoint);
                     i++;
                 }
             }
 
             consumed = consumed + i;
-            builder.nodeWithKey( targetNode.getQName(), keyValues );
-        }
-        else {
-            builder.node( targetNode.getQName() );
+            builder.nodeWithKey(targetNode.getQName(), keyValues);
+        } else {
+            builder.node(targetNode.getQName());
         }
 
-        if( (targetNode instanceof DataNodeContainer) ) {
-            final List<String> remaining = strings.subList( consumed, strings.size() );
-            return this.collectPathArguments( builder, remaining,
-                    ((DataNodeContainer) targetNode), mountPoint, returnJustMountPoint );
+        if ((targetNode instanceof DataNodeContainer)) {
+            final List<String> remaining = strings.subList(consumed, strings.size());
+            return this.collectPathArguments(builder, remaining, ((DataNodeContainer) targetNode), mountPoint,
+                    returnJustMountPoint);
         }
 
-        return new InstanceIdWithSchemaNode( builder.toInstance(), targetNode, mountPoint );
+        return new InstanceIdWithSchemaNode(builder.toInstance(), targetNode, mountPoint);
     }
 
-    public DataSchemaNode findInstanceDataChildByNameAndNamespace( final DataNodeContainer container,
-            final String name, final URI namespace ) {
-        Preconditions.<URI> checkNotNull( namespace );
+    public DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container, final String name,
+            final URI namespace) {
+        Preconditions.<URI> checkNotNull(namespace);
 
-        final List<DataSchemaNode> potentialSchemaNodes = this.findInstanceDataChildrenByName( container, name );
+        final List<DataSchemaNode> potentialSchemaNodes = this.findInstanceDataChildrenByName(container, name);
 
         Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
             @Override
-            public boolean apply( final DataSchemaNode node ) {
-                return Objects.equal( node.getQName().getNamespace(), namespace );
+            public boolean apply(final DataSchemaNode node) {
+                return Objects.equal(node.getQName().getNamespace(), namespace);
             }
         };
 
-        Iterable<DataSchemaNode> result = Iterables.filter( potentialSchemaNodes, filter );
-        return Iterables.getFirst( result, null );
+        Iterable<DataSchemaNode> result = Iterables.filter(potentialSchemaNodes, filter);
+        return Iterables.getFirst(result, null);
     }
 
-    public List<DataSchemaNode> findInstanceDataChildrenByName( final DataNodeContainer container,
-            final String name ) {
-        Preconditions.<DataNodeContainer> checkNotNull( container );
-        Preconditions.<String> checkNotNull( name );
+    public List<DataSchemaNode> findInstanceDataChildrenByName(final DataNodeContainer container, final String name) {
+        Preconditions.<DataNodeContainer> checkNotNull(container);
+        Preconditions.<String> checkNotNull(name);
 
         List<DataSchemaNode> instantiatedDataNodeContainers = new ArrayList<DataSchemaNode>();
-        this.collectInstanceDataNodeContainers( instantiatedDataNodeContainers, container, name );
+        this.collectInstanceDataNodeContainers(instantiatedDataNodeContainers, container, name);
         return instantiatedDataNodeContainers;
     }
 
-    private void collectInstanceDataNodeContainers( final List<DataSchemaNode> potentialSchemaNodes,
-            final DataNodeContainer container, final String name ) {
-
-        Set<DataSchemaNode> childNodes = container.getChildNodes();
+    private void collectInstanceDataNodeContainers(final List<DataSchemaNode> potentialSchemaNodes,
+            final DataNodeContainer container, final String name) {
 
         Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
             @Override
-            public boolean apply( final DataSchemaNode node ) {
-                return Objects.equal( node.getQName().getLocalName(), name );
+            public boolean apply(final DataSchemaNode node) {
+                return Objects.equal(node.getQName().getLocalName(), name);
             }
         };
 
-        Iterable<DataSchemaNode> nodes = Iterables.filter( childNodes, filter );
+        Iterable<DataSchemaNode> nodes = Iterables.filter(container.getChildNodes(), filter);
 
-        // Can't combine this loop with the filter above because the filter is lazily-applied
-        // by Iterables.filter.
-        for( final DataSchemaNode potentialNode : nodes ) {
-            if( this.isInstantiatedDataSchema( potentialNode ) ) {
-                potentialSchemaNodes.add( potentialNode );
+        // Can't combine this loop with the filter above because the filter is
+        // lazily-applied by Iterables.filter.
+        for (final DataSchemaNode potentialNode : nodes) {
+            if (this.isInstantiatedDataSchema(potentialNode)) {
+                potentialSchemaNodes.add(potentialNode);
             }
         }
 
-        Iterable<ChoiceNode> choiceNodes = Iterables.<ChoiceNode> filter( container.getChildNodes(),
-                ChoiceNode.class );
+        Iterable<ChoiceNode> choiceNodes = Iterables.<ChoiceNode> filter(container.getChildNodes(), ChoiceNode.class);
 
-        final Function<ChoiceNode, Set<ChoiceCaseNode>> choiceFunction =
-                new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
+        final Function<ChoiceNode, Set<ChoiceCaseNode>> choiceFunction = new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
             @Override
-            public Set<ChoiceCaseNode> apply( final ChoiceNode node ) {
+            public Set<ChoiceCaseNode> apply(final ChoiceNode node) {
                 return node.getCases();
             }
         };
 
-        Iterable<Set<ChoiceCaseNode>> map = Iterables.<ChoiceNode, Set<ChoiceCaseNode>> transform(
-                choiceNodes, choiceFunction );
+        Iterable<Set<ChoiceCaseNode>> map = Iterables.<ChoiceNode, Set<ChoiceCaseNode>> transform(choiceNodes,
+                choiceFunction);
 
-        final Iterable<ChoiceCaseNode> allCases = Iterables.<ChoiceCaseNode> concat( map );
-        for( final ChoiceCaseNode caze : allCases ) {
-            this.collectInstanceDataNodeContainers( potentialSchemaNodes, caze, name );
+        final Iterable<ChoiceCaseNode> allCases = Iterables.<ChoiceCaseNode> concat(map);
+        for (final ChoiceCaseNode caze : allCases) {
+            this.collectInstanceDataNodeContainers(potentialSchemaNodes, caze, name);
         }
     }
 
-    public boolean isInstantiatedDataSchema( final DataSchemaNode node ) {
-        return node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode ||
-                node instanceof ContainerSchemaNode || node instanceof ListSchemaNode;
+    public boolean isInstantiatedDataSchema(final DataSchemaNode node) {
+        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, final MountInstance mountPoint ) {
-        Preconditions.<String> checkNotNull( uriValue );
-        Preconditions.checkArgument( (node instanceof LeafSchemaNode) );
+    private void addKeyValue(final HashMap<QName, Object> map, final DataSchemaNode node, final String uriValue,
+            final MountInstance mountPoint) {
+        Preconditions.<String> checkNotNull(uriValue);
+        Preconditions.checkArgument((node instanceof LeafSchemaNode));
 
-        final String urlDecoded = urlPathArgDecode( uriValue );
+        final String urlDecoded = urlPathArgDecode(uriValue);
         final TypeDefinition<? extends Object> typedef = ((LeafSchemaNode) node).getType();
-        Codec<Object, Object> codec = RestCodec.from( typedef, mountPoint );
+        Codec<Object, Object> codec = RestCodec.from(typedef, mountPoint);
 
-        Object decoded = codec == null ? null : codec.deserialize( urlDecoded );
+        Object decoded = codec == null ? null : codec.deserialize(urlDecoded);
         String additionalInfo = "";
-        if( decoded == null ) {
-            TypeDefinition<? extends Object> baseType = RestUtil.resolveBaseTypeFrom( typedef );
-            if( (baseType instanceof IdentityrefTypeDefinition) ) {
-                decoded = this.toQName( urlDecoded );
+        if (decoded == null) {
+            TypeDefinition<? extends Object> baseType = RestUtil.resolveBaseTypeFrom(typedef);
+            if ((baseType instanceof IdentityrefTypeDefinition)) {
+                decoded = this.toQName(urlDecoded);
                 additionalInfo = "For key which is of type identityref it should be in format module_name:identity_name.";
             }
         }
 
-        if( decoded == null ) {
-            throw new RestconfDocumentedException(
-                    uriValue + " from URI can't be resolved. " + additionalInfo,
-                    ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+        if (decoded == null) {
+            throw new RestconfDocumentedException(uriValue + " from URI can't be resolved. " + additionalInfo,
+                    ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
 
-        map.put( node.getQName(), decoded );
+        map.put(node.getQName(), decoded);
     }
 
-    private static String toModuleName( final String str ) {
-        Preconditions.<String> checkNotNull( str );
-        if( str.contains( ":" ) ) {
-            final String[] args = str.split( ":" );
-            if( args.length == 2 ) {
-                return args[0];
-            }
+    private static String toModuleName(final String str) {
+        final int idx = str.indexOf(':');
+        if (idx == -1) {
+            return null;
         }
-        return null;
-    }
 
-    private String toNodeName( final String str ) {
-        if( str.contains( ":" ) ) {
-            final String[] args = str.split( ":" );
-            if( args.length == 2 ) {
-                return args[1];
-            }
+        // Make sure there is only one occurrence
+        if (str.indexOf(':', idx + 1) != -1) {
+            return null;
         }
-        return str;
-    }
-
-    private QName toQName( final String name ) {
-        final String module = toModuleName( name );
-        final String node = this.toNodeName( name );
-        Set<Module> modules = globalSchema.getModules();
 
-        final Comparator<Module> comparator = new Comparator<Module>() {
-            @Override
-            public int compare( final Module o1, final Module o2 ) {
-                return o1.getRevision().compareTo( o2.getRevision() );
-            }
-        };
+        return str.substring(0, idx);
+    }
 
-        List<Module> sorted = new ArrayList<Module>( modules );
-        Collections.<Module> sort( new ArrayList<Module>( modules ), comparator );
+    private static String toNodeName(final String str) {
+        final int idx = str.indexOf(':');
+        if (idx == -1) {
+            return str;
+        }
 
-        final Function<Module, QName> transform = new Function<Module, QName>() {
-            @Override
-            public QName apply( final Module m ) {
-                return QName.create( m.getNamespace(), m.getRevision(), m.getName() );
-            }
-        };
+        // Make sure there is only one occurrence
+        if (str.indexOf(':', idx + 1) != -1) {
+            return str;
+        }
 
-        final Predicate<QName> findFirst = new Predicate<QName>() {
-            @Override
-            public boolean apply( final QName qn ) {
-                return Objects.equal( module, qn.getLocalName() );
-            }
-        };
+        return str.substring(idx + 1);
+    }
 
-        Optional<QName> namespace = FluentIterable.from( sorted )
-                .transform( transform )
-                .firstMatch( findFirst );
-        return namespace.isPresent() ? QName.create( namespace.get(), node ) : null;
+    private QName toQName(final String name) {
+        final String module = toModuleName(name);
+        final String node = toNodeName(name);
+        final Module m = globalSchema.findModuleByName(module, null);
+        return m == null ? null : QName.create(m.getQNameModule(), node);
     }
 
-    private boolean isListOrContainer( final DataSchemaNode node ) {
+    private static boolean isListOrContainer(final DataSchemaNode node) {
         return node instanceof ListSchemaNode || node instanceof ContainerSchemaNode;
     }
 
-    public RpcDefinition getRpcDefinition( final String name ) {
-        final QName validName = this.toQName( name );
-        return validName == null ? null : this.qnameToRpc.get( validName );
+    public RpcDefinition getRpcDefinition(final String name) {
+        final QName validName = this.toQName(name);
+        return validName == null ? null : this.qnameToRpc.get().get(validName);
     }
 
     @Override
-    public void onGlobalContextUpdated( final SchemaContext context ) {
-        if( context != null ) {
-            this.qnameToRpc.clear();
-            this.setGlobalSchema( context );
-            Set<RpcDefinition> _operations = context.getOperations();
-            for( final RpcDefinition operation : _operations ) {
-                {
-                    this.qnameToRpc.put( operation.getQName(), operation );
-                }
+    public void onGlobalContextUpdated(final SchemaContext context) {
+        if (context != null) {
+            final Collection<RpcDefinition> defs = context.getOperations();
+            final Map<QName, RpcDefinition> newMap = new HashMap<>(defs.size());
+
+            for (final RpcDefinition operation : defs) {
+                newMap.put(operation.getQName(), operation);
             }
+
+            // FIXME: still not completely atomic
+            this.qnameToRpc.set(ImmutableMap.copyOf(newMap));
+            this.setGlobalSchema(context);
         }
     }
 
-    public List<String> urlPathArgsDecode( final List<String> strings ) {
+    public static List<String> urlPathArgsDecode(final Iterable<String> strings) {
         try {
             List<String> decodedPathArgs = new ArrayList<String>();
-            for( final String pathArg : strings ) {
-                String _decode = URLDecoder.decode( pathArg, URI_ENCODING_CHAR_SET );
-                decodedPathArgs.add( _decode );
+            for (final String pathArg : strings) {
+                String _decode = URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET);
+                decodedPathArgs.add(_decode);
             }
             return decodedPathArgs;
-        }
-        catch( UnsupportedEncodingException e ) {
-            throw new RestconfDocumentedException(
-                    "Invalid URL path '" + strings + "': " + e.getMessage(),
-                    ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+        } catch (UnsupportedEncodingException e) {
+            throw new RestconfDocumentedException("Invalid URL path '" + strings + "': " + e.getMessage(),
+                    ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
     }
 
-    public String urlPathArgDecode( final String pathArg ) {
-        if( pathArg != null ) {
+    public String urlPathArgDecode(final String pathArg) {
+        if (pathArg != null) {
             try {
-                return URLDecoder.decode( pathArg, URI_ENCODING_CHAR_SET );
-            }
-            catch( UnsupportedEncodingException e ) {
-                throw new RestconfDocumentedException(
-                        "Invalid URL path arg '" + pathArg + "': " + e.getMessage(),
-                        ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+                return URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET);
+            } catch (UnsupportedEncodingException e) {
+                throw new RestconfDocumentedException("Invalid URL path arg '" + pathArg + "': " + e.getMessage(),
+                        ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
             }
         }
 
         return null;
     }
 
-    private CharSequence convertToRestconfIdentifier( final PathArgument argument,
-            final DataNodeContainer node ) {
-        if( argument instanceof NodeIdentifier && node instanceof ContainerSchemaNode ) {
-            return convertToRestconfIdentifier( (NodeIdentifier) argument, (ContainerSchemaNode) node );
-        }
-        else if( argument instanceof NodeIdentifierWithPredicates && node instanceof ListSchemaNode ) {
-            return convertToRestconfIdentifier( (NodeIdentifierWithPredicates) argument, (ListSchemaNode) node );
-        }
-        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() );
+    private CharSequence convertToRestconfIdentifier(final PathArgument argument, final DataNodeContainer node) {
+        if (argument instanceof NodeIdentifier && node instanceof ContainerSchemaNode) {
+            return convertToRestconfIdentifier((NodeIdentifier) argument, (ContainerSchemaNode) node);
+        } else if (argument instanceof NodeIdentifierWithPredicates && node instanceof ListSchemaNode) {
+            return convertToRestconfIdentifier((NodeIdentifierWithPredicates) argument, (ListSchemaNode) node);
+        } 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());
         }
     }
 
-    private CharSequence convertToRestconfIdentifier( final NodeIdentifier argument,
-            final ContainerSchemaNode node ) {
+    private CharSequence convertToRestconfIdentifier(final NodeIdentifier argument, final ContainerSchemaNode node) {
         StringBuilder builder = new StringBuilder();
-        builder.append( "/" );
+        builder.append("/");
         QName nodeType = argument.getNodeType();
-        builder.append( this.toRestconfIdentifier( nodeType ) );
+        builder.append(this.toRestconfIdentifier(nodeType));
         return builder.toString();
     }
 
-    private CharSequence convertToRestconfIdentifier( final NodeIdentifierWithPredicates argument,
-            final ListSchemaNode node ) {
+    private CharSequence convertToRestconfIdentifier(final NodeIdentifierWithPredicates argument,
+            final ListSchemaNode node) {
         QName nodeType = argument.getNodeType();
-        final CharSequence nodeIdentifier = this.toRestconfIdentifier( nodeType );
+        final CharSequence nodeIdentifier = this.toRestconfIdentifier(nodeType);
         final Map<QName, Object> keyValues = argument.getKeyValues();
 
         StringBuilder builder = new StringBuilder();
-        builder.append( "/" );
-        builder.append( nodeIdentifier );
-        builder.append( "/" );
+        builder.append("/");
+        builder.append(nodeIdentifier);
+        builder.append("/");
 
         List<QName> keyDefinition = node.getKeyDefinition();
         boolean hasElements = false;
-        for( final QName key : keyDefinition ) {
-            if( !hasElements ) {
+        for (final QName key : keyDefinition) {
+            if (!hasElements) {
                 hasElements = true;
-            }
-            else {
-                builder.append( "/" );
+            } else {
+                builder.append("/");
             }
 
             try {
-                builder.append( this.toUriString( keyValues.get( key ) ) );
-            } catch( UnsupportedEncodingException e ) {
-                LOG.error( "Error parsing URI: " + keyValues.get( key ), e );
+                builder.append(this.toUriString(keyValues.get(key)));
+            } catch (UnsupportedEncodingException e) {
+                LOG.error("Error parsing URI: {}", keyValues.get(key), e);
                 return null;
             }
         }
@@ -1004,28 +929,22 @@ 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 ChoiceNode ) {
-            return childByQName( (ChoiceNode) 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 childByQName(final Object container, final QName name) {
+        if (container instanceof ChoiceCaseNode) {
+            return childByQName((ChoiceCaseNode) container, name);
+        } else if (container instanceof ChoiceNode) {
+            return childByQName((ChoiceNode) 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());
         }
     }
 }