Merge "BUG 3057 - notify added event source by topics created before"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.java
index b0a6e0d6f3885f20242d483044c0cfa92af1dea6..6cc62e859c9bade70e4125d2fea72a8addc6f875 100644 (file)
@@ -27,10 +27,10 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import javax.ws.rs.core.Response.Status;
+import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationOperation;
 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
@@ -41,16 +41,15 @@ import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
 import org.opendaylight.yangtools.concepts.Codec;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 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.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -84,6 +83,8 @@ public class ControllerContext implements SchemaContextListener {
 
     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());
 
@@ -94,7 +95,7 @@ public class ControllerContext implements SchemaContextListener {
 
     public void setGlobalSchema(final SchemaContext globalSchema) {
         this.globalSchema = globalSchema;
-        this.dataNormalizer = new DataNormalizer(globalSchema);
+        dataNormalizer = new DataNormalizer(globalSchema);
     }
 
     public void setMountService(final DOMMountPointService mountService) {
@@ -115,23 +116,27 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public void setSchemas(final SchemaContext schemas) {
-        this.onGlobalContextUpdated(schemas);
+        onGlobalContextUpdated(schemas);
     }
 
-    public InstanceIdentifierContext toInstanceIdentifier(final String restconfInstance) {
-        return this.toIdentifier(restconfInstance, false);
+    public InstanceIdentifierContext<?> toInstanceIdentifier(final String restconfInstance) {
+        return toIdentifier(restconfInstance, false);
     }
 
     public SchemaContext getGlobalSchema() {
         return globalSchema;
     }
 
-    public InstanceIdentifierContext toMountPointIdentifier(final String restconfInstance) {
-        return this.toIdentifier(restconfInstance, true);
+    public InstanceIdentifierContext<?> toMountPointIdentifier(final String restconfInstance) {
+        return toIdentifier(restconfInstance, true);
     }
 
-    private InstanceIdentifierContext toIdentifier(final String restconfInstance, final boolean toMountPointIdentifier) {
-        this.checkPreconditions();
+    private InstanceIdentifierContext<?> toIdentifier(final String restconfInstance, final boolean toMountPointIdentifier) {
+        checkPreconditions();
+
+        if(restconfInstance == null) {
+            return new InstanceIdentifierContext<>(ROOT, globalSchema, null, globalSchema);
+        }
 
         final List<String> pathArgs = urlPathArgsDecode(SLASH_SPLITTER.split(restconfInstance));
         omitFirstAndLastEmptyString(pathArgs);
@@ -139,16 +144,21 @@ public class ControllerContext implements SchemaContextListener {
             return null;
         }
 
-        String first = pathArgs.iterator().next();
+        final 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);
         }
 
-        InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
-        Module latestModule = globalSchema.findModuleByName(startModule, null);
-        InstanceIdentifierContext iiWithSchemaNode = this.collectPathArguments(builder, pathArgs, latestModule, null,
+        final InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
+        final Module latestModule = globalSchema.findModuleByName(startModule, null);
+
+        if (latestModule == null) {
+            throw new RestconfDocumentedException("The module named '" + startModule + "' does not exist.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
+        }
+
+        final InstanceIdentifierContext<?> iiWithSchemaNode = collectPathArguments(builder, pathArgs, latestModule, null,
                 toMountPointIdentifier);
 
         if (iiWithSchemaNode == null) {
@@ -163,7 +173,7 @@ public class ControllerContext implements SchemaContextListener {
             return list;
         }
 
-        String head = list.iterator().next();
+        final String head = list.iterator().next();
         if (head.isEmpty()) {
             list.remove(0);
         }
@@ -172,7 +182,7 @@ public class ControllerContext implements SchemaContextListener {
             return list;
         }
 
-        String last = list.get(list.size() - 1);
+        final String last = list.get(list.size() - 1);
         if (last.isEmpty()) {
             list.remove(list.size() - 1);
         }
@@ -180,7 +190,7 @@ public class ControllerContext implements SchemaContextListener {
         return list;
     }
     public Module findModuleByName(final String moduleName) {
-        this.checkPreconditions();
+        checkPreconditions();
         Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
         return globalSchema.findModuleByName(moduleName, null);
     }
@@ -197,7 +207,7 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public Module findModuleByNamespace(final URI namespace) {
-        this.checkPreconditions();
+        checkPreconditions();
         Preconditions.checkArgument(namespace != null);
         return globalSchema.findModuleByNamespaceAndRevision(namespace, null);
     }
@@ -214,33 +224,33 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public Module findModuleByNameAndRevision(final QName module) {
-        this.checkPreconditions();
+        checkPreconditions();
         Preconditions.checkArgument(module != null && module.getLocalName() != null && module.getRevision() != null);
 
         return globalSchema.findModuleByName(module.getLocalName(), module.getRevision());
     }
 
     public Module findModuleByNameAndRevision(final DOMMountPoint mountPoint, final QName module) {
-        this.checkPreconditions();
+        checkPreconditions();
         Preconditions.checkArgument(module != null && module.getLocalName() != null && module.getRevision() != null
                 && mountPoint != null);
 
-        SchemaContext schemaContext = mountPoint.getSchemaContext();
+        final SchemaContext schemaContext = mountPoint.getSchemaContext();
         return schemaContext == null ? null : schemaContext.findModuleByName(module.getLocalName(),
                 module.getRevision());
     }
 
     public DataNodeContainer getDataNodeContainerFor(final YangInstanceIdentifier path) {
-        this.checkPreconditions();
+        checkPreconditions();
 
         final Iterable<PathArgument> elements = path.getPathArguments();
-        PathArgument head = elements.iterator().next();
+        final PathArgument head = elements.iterator().next();
         final QName startQName = head.getNodeType();
         final Module initialModule = globalSchema.findModuleByNamespaceAndRevision(startQName.getNamespace(),
                 startQName.getRevision());
         DataNodeContainer node = initialModule;
         for (final PathArgument element : elements) {
-            QName _nodeType = element.getNodeType();
+            final QName _nodeType = element.getNodeType();
             final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
             if (potentialNode == null || !ControllerContext.isListOrContainer(potentialNode)) {
                 return null;
@@ -251,31 +261,41 @@ public class ControllerContext implements SchemaContextListener {
         return node;
     }
 
-    public String toFullRestconfIdentifier(final YangInstanceIdentifier path) {
-        this.checkPreconditions();
+    public String toFullRestconfIdentifier(final YangInstanceIdentifier path, final DOMMountPoint mount) {
+        checkPreconditions();
 
         final Iterable<PathArgument> elements = path.getPathArguments();
         final StringBuilder builder = new StringBuilder();
-        PathArgument head = elements.iterator().next();
+        final PathArgument head = elements.iterator().next();
         final QName startQName = head.getNodeType();
-        final Module initialModule = globalSchema.findModuleByNamespaceAndRevision(startQName.getNamespace(),
+        final SchemaContext schemaContext;
+        if (mount != null) {
+            schemaContext = mount.getSchemaContext();
+        } else {
+            schemaContext = globalSchema;
+        }
+        final Module initialModule = schemaContext.findModuleByNamespaceAndRevision(startQName.getNamespace(),
                 startQName.getRevision());
         DataNodeContainer node = initialModule;
         for (final PathArgument element : elements) {
-            QName _nodeType = element.getNodeType();
-            final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
-            if (!ControllerContext.isListOrContainer(potentialNode)) {
-                return null;
+            if (!(element instanceof AugmentationIdentifier)) {
+                final QName _nodeType = element.getNodeType();
+                final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType);
+                if (!(element instanceof NodeIdentifier && potentialNode instanceof ListSchemaNode)) {
+                    if (!ControllerContext.isListOrContainer(potentialNode)) {
+                        return null;
+                    }
+                    builder.append(convertToRestconfIdentifier(element, (DataNodeContainer) potentialNode, mount));
+                    node = (DataNodeContainer) potentialNode;
+                }
             }
-            node = ((DataNodeContainer) potentialNode);
-            builder.append(this.convertToRestconfIdentifier(element, node));
         }
 
         return builder.toString();
     }
 
     public String findModuleNameByNamespace(final URI namespace) {
-        this.checkPreconditions();
+        checkPreconditions();
 
         final Module module = this.findModuleByNamespace(namespace);
         return module == null ? null : module.getName();
@@ -297,14 +317,14 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public Set<Module> getAllModules(final DOMMountPoint mountPoint) {
-        this.checkPreconditions();
+        checkPreconditions();
 
-        SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
+        final SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
         return schemaContext == null ? null : schemaContext.getModules();
     }
 
     public Set<Module> getAllModules() {
-        this.checkPreconditions();
+        checkPreconditions();
         return globalSchema.getModules();
     }
 
@@ -313,8 +333,20 @@ public class ControllerContext implements SchemaContextListener {
         return schema == null ? null : schema.getName() + ':' + qname.getLocalName();
     }
 
+    public CharSequence toRestconfIdentifier(final QName qname, final DOMMountPoint mount) {
+        final SchemaContext schema;
+        if (mount != null) {
+            schema = mount.getSchemaContext();
+        } else {
+            checkPreconditions();
+            schema = globalSchema;
+        }
+
+        return toRestconfIdentifier(schema, qname);
+    }
+
     public CharSequence toRestconfIdentifier(final QName qname) {
-        this.checkPreconditions();
+        checkPreconditions();
 
         return toRestconfIdentifier(globalSchema, qname);
     }
@@ -339,18 +371,18 @@ public class ControllerContext implements SchemaContextListener {
     };
 
     public DataSchemaNode getRestconfModuleErrorsSchemaNode() {
-        Module restconfModule = getRestconfModule();
+        final Module restconfModule = getRestconfModule();
         if (restconfModule == null) {
             return null;
         }
 
-        Set<GroupingDefinition> groupings = restconfModule.getGroupings();
+        final Set<GroupingDefinition> groupings = restconfModule.getGroupings();
 
-        Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, ERRORS_GROUPING_FILTER);
+        final Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, ERRORS_GROUPING_FILTER);
 
         final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
 
-        List<DataSchemaNode> instanceDataChildrenByName = findInstanceDataChildrenByName(restconfGrouping,
+        final List<DataSchemaNode> instanceDataChildrenByName = findInstanceDataChildrenByName(restconfGrouping,
                 Draft02.RestConfModule.ERRORS_CONTAINER_SCHEMA_NODE);
         return Iterables.getFirst(instanceDataChildrenByName, null);
     }
@@ -372,20 +404,20 @@ public class ControllerContext implements SchemaContextListener {
             return null;
         }
 
-        Set<GroupingDefinition> groupings = restconfModule.getGroupings();
-        Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, GROUPING_FILTER);
+        final Set<GroupingDefinition> groupings = restconfModule.getGroupings();
+        final Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, GROUPING_FILTER);
         final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
 
-        List<DataSchemaNode> instanceDataChildrenByName = findInstanceDataChildrenByName(restconfGrouping,
+        final List<DataSchemaNode> instanceDataChildrenByName = 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 = findInstanceDataChildrenByName(
+            final List<DataSchemaNode> instances = 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 = findInstanceDataChildrenByName(
+            final List<DataSchemaNode> instances = 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)) {
@@ -396,7 +428,7 @@ public class ControllerContext implements SchemaContextListener {
                     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 = findInstanceDataChildrenByName(
+            final List<DataSchemaNode> instances = 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)) {
@@ -407,7 +439,7 @@ public class ControllerContext implements SchemaContextListener {
                     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 = findInstanceDataChildrenByName(
+            final List<DataSchemaNode> instances = findInstanceDataChildrenByName(
                     ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
             return Iterables.getFirst(instances, null);
         }
@@ -415,7 +447,7 @@ public class ControllerContext implements SchemaContextListener {
         return null;
     }
 
-    private static DataSchemaNode childByQName(final ChoiceNode container, final QName name) {
+    private static DataSchemaNode childByQName(final ChoiceSchemaNode container, final QName name) {
         for (final ChoiceCaseNode caze : container.getCases()) {
             final DataSchemaNode ret = ControllerContext.childByQName(caze, name);
             if (ret != null) {
@@ -447,12 +479,12 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     private static DataSchemaNode dataNodeChildByQName(final DataNodeContainer container, final QName name) {
-        DataSchemaNode ret = container.getDataChildByName(name);
+        final DataSchemaNode ret = container.getDataChildByName(name);
         if (ret == null) {
             for (final DataSchemaNode node : container.getChildNodes()) {
-                if ((node instanceof ChoiceNode)) {
-                    final ChoiceNode choiceNode = ((ChoiceNode) node);
-                    DataSchemaNode childByQName = ControllerContext.childByQName(choiceNode, name);
+                if ((node instanceof ChoiceSchemaNode)) {
+                    final ChoiceSchemaNode choiceNode = ((ChoiceSchemaNode) node);
+                    final DataSchemaNode childByQName = ControllerContext.childByQName(choiceNode, name);
                     if (childByQName != null) {
                         return childByQName;
                     }
@@ -462,11 +494,12 @@ 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, 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);
     }
 
-    private InstanceIdentifierContext collectPathArguments(final InstanceIdentifierBuilder builder,
+    private InstanceIdentifierContext<?> collectPathArguments(final InstanceIdentifierBuilder builder,
             final List<String> strings, final DataNodeContainer parentNode, final DOMMountPoint mountPoint,
             final boolean returnJustMountPoint) {
         Preconditions.<List<String>> checkNotNull(strings);
@@ -476,10 +509,10 @@ public class ControllerContext implements SchemaContextListener {
         }
 
         if (strings.isEmpty()) {
-            return new InstanceIdentifierContext(builder.toInstance(), ((DataSchemaNode) parentNode), mountPoint,mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
+            return createContext(builder.toInstance(), ((DataSchemaNode) parentNode), mountPoint,mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
         }
 
-        String head = strings.iterator().next();
+        final String head = strings.iterator().next();
         final String nodeName = toNodeName(head);
         final String moduleName = ControllerContext.toModuleName(head);
 
@@ -498,14 +531,14 @@ public class ControllerContext implements SchemaContextListener {
                             ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
                 }
 
-                final YangInstanceIdentifier partialPath = builder.toInstance();
+                final YangInstanceIdentifier partialPath = dataNormalizer.toNormalized(builder.build());
                 final Optional<DOMMountPoint> mountOpt = 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,
                             ErrorTag.UNKNOWN_ELEMENT);
                 }
-                DOMMountPoint mount = mountOpt.get();
+                final DOMMountPoint mount = mountOpt.get();
 
                 final SchemaContext mountPointSchema = mount.getSchemaContext();
                 if (mountPointSchema == null) {
@@ -513,14 +546,9 @@ public class ControllerContext implements SchemaContextListener {
                             ErrorType.APPLICATION, ErrorTag.UNKNOWN_ELEMENT);
                 }
 
-                if (returnJustMountPoint) {
-                    YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance();
-                    return new InstanceIdentifierContext(instance, mountPointSchema, mount,mountPointSchema);
-                }
-
-                if (strings.size() == 1) {
-                    YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance();
-                    return new InstanceIdentifierContext(instance, mountPointSchema, mount,mountPointSchema);
+                if (returnJustMountPoint || strings.size() == 1) {
+                    final YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance();
+                    return new InstanceIdentifierContext<>(instance, mountPointSchema, mount,mountPointSchema);
                 }
 
                 final String moduleNameBehindMountPoint = toModuleName(strings.get(1));
@@ -536,20 +564,21 @@ public class ControllerContext implements SchemaContextListener {
                             + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                 }
 
-                List<String> subList = strings.subList(1, strings.size());
-                return this.collectPathArguments(YangInstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount,
+                final List<String> subList = strings.subList(1, strings.size());
+                return collectPathArguments(YangInstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount,
                         returnJustMountPoint);
             }
 
             Module module = null;
             if (mountPoint == null) {
+                checkPreconditions();
                 module = globalSchema.findModuleByName(moduleName, null);
                 if (module == null) {
                     throw new RestconfDocumentedException("\"" + moduleName + "\" module does not exist.",
                             ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                 }
             } else {
-                SchemaContext schemaContext = mountPoint.getSchemaContext();
+                final SchemaContext schemaContext = mountPoint.getSchemaContext();
                 if (schemaContext != null) {
                     module = schemaContext.findModuleByName(moduleName, null);
                 } else {
@@ -562,6 +591,15 @@ public class ControllerContext implements SchemaContextListener {
             }
 
             targetNode = findInstanceDataChildByNameAndNamespace(parentNode, nodeName, module.getNamespace());
+
+            if (targetNode == null && parentNode instanceof Module) {
+                final RpcDefinition rpc = ControllerContext.getInstance().getRpcDefinition(head);
+                if (rpc != null) {
+                    return new InstanceIdentifierContext<RpcDefinition>(builder.build(), rpc, mountPoint,
+                            mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
+                }
+            }
+
             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
@@ -619,7 +657,7 @@ public class ControllerContext implements SchemaContextListener {
                                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
                     }
 
-                    this.addKeyValue(keyValues, listNode.getDataChildByName(key), uriKeyValue, mountPoint);
+                    addKeyValue(keyValues, listNode.getDataChildByName(key), uriKeyValue, mountPoint);
                     i++;
                 }
             }
@@ -632,11 +670,18 @@ public class ControllerContext implements SchemaContextListener {
 
         if ((targetNode instanceof DataNodeContainer)) {
             final List<String> remaining = strings.subList(consumed, strings.size());
-            return this.collectPathArguments(builder, remaining, ((DataNodeContainer) targetNode), mountPoint,
+            return collectPathArguments(builder, remaining, ((DataNodeContainer) targetNode), mountPoint,
                     returnJustMountPoint);
         }
 
-        return new InstanceIdentifierContext(builder.toInstance(), targetNode, mountPoint,mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
+        return createContext(builder.build(), targetNode, mountPoint,mountPoint != null ? mountPoint.getSchemaContext() : globalSchema);
+    }
+
+    private 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);
     }
 
     public static DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container, final String name,
@@ -645,14 +690,14 @@ public class ControllerContext implements SchemaContextListener {
 
         final List<DataSchemaNode> potentialSchemaNodes = findInstanceDataChildrenByName(container, name);
 
-        Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
+        final Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
             @Override
             public boolean apply(final DataSchemaNode node) {
                 return Objects.equal(node.getQName().getNamespace(), namespace);
             }
         };
 
-        Iterable<DataSchemaNode> result = Iterables.filter(potentialSchemaNodes, filter);
+        final Iterable<DataSchemaNode> result = Iterables.filter(potentialSchemaNodes, filter);
         return Iterables.getFirst(result, null);
     }
 
@@ -660,14 +705,14 @@ public class ControllerContext implements SchemaContextListener {
         Preconditions.<DataNodeContainer> checkNotNull(container);
         Preconditions.<String> checkNotNull(name);
 
-        List<DataSchemaNode> instantiatedDataNodeContainers = new ArrayList<DataSchemaNode>();
+        final List<DataSchemaNode> instantiatedDataNodeContainers = new ArrayList<DataSchemaNode>();
         collectInstanceDataNodeContainers(instantiatedDataNodeContainers, container, name);
         return instantiatedDataNodeContainers;
     }
 
-    private static final Function<ChoiceNode, Set<ChoiceCaseNode>> CHOICE_FUNCTION = new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
+    private static final Function<ChoiceSchemaNode, Set<ChoiceCaseNode>> CHOICE_FUNCTION = new Function<ChoiceSchemaNode, Set<ChoiceCaseNode>>() {
         @Override
-        public Set<ChoiceCaseNode> apply(final ChoiceNode node) {
+        public Set<ChoiceCaseNode> apply(final ChoiceSchemaNode node) {
             return node.getCases();
         }
     };
@@ -675,14 +720,14 @@ public class ControllerContext implements SchemaContextListener {
     private static void collectInstanceDataNodeContainers(final List<DataSchemaNode> potentialSchemaNodes,
             final DataNodeContainer container, final String name) {
 
-        Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
+        final Predicate<DataSchemaNode> filter = new Predicate<DataSchemaNode>() {
             @Override
             public boolean apply(final DataSchemaNode node) {
                 return Objects.equal(node.getQName().getLocalName(), name);
             }
         };
 
-        Iterable<DataSchemaNode> nodes = Iterables.filter(container.getChildNodes(), filter);
+        final 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.
@@ -692,8 +737,8 @@ public class ControllerContext implements SchemaContextListener {
             }
         }
 
-        Iterable<ChoiceNode> choiceNodes = Iterables.filter(container.getChildNodes(), ChoiceNode.class);
-        Iterable<Set<ChoiceCaseNode>> map = Iterables.transform(choiceNodes, CHOICE_FUNCTION);
+        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) {
@@ -714,14 +759,14 @@ public class ControllerContext implements SchemaContextListener {
 
         final String urlDecoded = urlPathArgDecode(uriValue);
         final TypeDefinition<? extends Object> typedef = ((LeafSchemaNode) node).getType();
-        Codec<Object, Object> codec = RestCodec.from(typedef, mountPoint);
+        final Codec<Object, Object> codec = RestCodec.from(typedef, mountPoint);
 
         Object decoded = codec == null ? null : codec.deserialize(urlDecoded);
         String additionalInfo = "";
         if (decoded == null) {
-            TypeDefinition<? extends Object> baseType = RestUtil.resolveBaseTypeFrom(typedef);
+            final TypeDefinition<? extends Object> baseType = RestUtil.resolveBaseTypeFrom(typedef);
             if ((baseType instanceof IdentityrefTypeDefinition)) {
-                decoded = this.toQName(urlDecoded);
+                decoded = toQName(urlDecoded);
                 additionalInfo = "For key which is of type identityref it should be in format module_name:identity_name.";
             }
         }
@@ -763,6 +808,7 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     private QName toQName(final String name) {
+        checkPreconditions();
         final String module = toModuleName(name);
         final String node = toNodeName(name);
         final Module m = globalSchema.findModuleByName(module, null);
@@ -774,8 +820,8 @@ public class ControllerContext implements SchemaContextListener {
     }
 
     public RpcDefinition getRpcDefinition(final String name) {
-        final QName validName = this.toQName(name);
-        return validName == null ? null : this.qnameToRpc.get().get(validName);
+        final QName validName = toQName(name);
+        return validName == null ? null : qnameToRpc.get().get(validName);
     }
 
     @Override
@@ -789,20 +835,20 @@ public class ControllerContext implements SchemaContextListener {
             }
 
             // FIXME: still not completely atomic
-            this.qnameToRpc.set(ImmutableMap.copyOf(newMap));
-            this.setGlobalSchema(context);
+            qnameToRpc.set(ImmutableMap.copyOf(newMap));
+            setGlobalSchema(context);
         }
     }
 
     public static List<String> urlPathArgsDecode(final Iterable<String> strings) {
         try {
-            List<String> decodedPathArgs = new ArrayList<String>();
+            final List<String> decodedPathArgs = new ArrayList<String>();
             for (final String pathArg : strings) {
-                String _decode = URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET);
+                final String _decode = URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET);
                 decodedPathArgs.add(_decode);
             }
             return decodedPathArgs;
-        } catch (UnsupportedEncodingException e) {
+        } catch (final UnsupportedEncodingException e) {
             throw new RestconfDocumentedException("Invalid URL path '" + strings + "': " + e.getMessage(),
                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
@@ -812,7 +858,7 @@ public class ControllerContext implements SchemaContextListener {
         if (pathArg != null) {
             try {
                 return URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET);
-            } catch (UnsupportedEncodingException e) {
+            } catch (final UnsupportedEncodingException e) {
                 throw new RestconfDocumentedException("Invalid URL path arg '" + pathArg + "': " + e.getMessage(),
                         ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
             }
@@ -821,11 +867,11 @@ public class ControllerContext implements SchemaContextListener {
         return null;
     }
 
-    private CharSequence convertToRestconfIdentifier(final PathArgument argument, final DataNodeContainer node) {
+    private CharSequence convertToRestconfIdentifier(final PathArgument argument, final DataNodeContainer node, final DOMMountPoint mount) {
         if (argument instanceof NodeIdentifier && node instanceof ContainerSchemaNode) {
-            return convertToRestconfIdentifier((NodeIdentifier) argument, (ContainerSchemaNode) node);
+            return convertToRestconfIdentifier((NodeIdentifier) argument, mount);
         } else if (argument instanceof NodeIdentifierWithPredicates && node instanceof ListSchemaNode) {
-            return convertToRestconfIdentifier((NodeIdentifierWithPredicates) argument, (ListSchemaNode) node);
+            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 {
@@ -834,35 +880,41 @@ public class ControllerContext implements SchemaContextListener {
         }
     }
 
-    private CharSequence convertToRestconfIdentifier(final NodeIdentifier argument, final ContainerSchemaNode node) {
-        return "/" + this.toRestconfIdentifier(argument.getNodeType());
+    private CharSequence convertToRestconfIdentifier(final NodeIdentifier argument, final DOMMountPoint node) {
+        return "/" + this.toRestconfIdentifier(argument.getNodeType(),node);
     }
 
-    private CharSequence convertToRestconfIdentifier(final NodeIdentifierWithPredicates argument,
-            final ListSchemaNode node) {
-        QName nodeType = argument.getNodeType();
-        final CharSequence nodeIdentifier = this.toRestconfIdentifier(nodeType);
+    private CharSequence convertToRestconfIdentifierWithPredicates(final NodeIdentifierWithPredicates argument,
+            final ListSchemaNode node, final DOMMountPoint mount) {
+        final QName nodeType = argument.getNodeType();
+        final CharSequence nodeIdentifier = this.toRestconfIdentifier(nodeType, mount);
         final Map<QName, Object> keyValues = argument.getKeyValues();
 
-        StringBuilder builder = new StringBuilder();
+        final StringBuilder builder = new StringBuilder();
         builder.append('/');
         builder.append(nodeIdentifier);
         builder.append('/');
 
-        List<QName> keyDefinition = node.getKeyDefinition();
+        final List<QName> keyDefinition = node.getKeyDefinition();
         boolean hasElements = false;
         for (final QName key : keyDefinition) {
-            if (!hasElements) {
-                hasElements = true;
-            } else {
-                builder.append('/');
-            }
+            for (final DataSchemaNode listChild : node.getChildNodes()) {
+                if (listChild.getQName().equals(key)) {
+                    if (!hasElements) {
+                        hasElements = true;
+                    } else {
+                        builder.append('/');
+                    }
 
-            try {
-                builder.append(this.toUriString(keyValues.get(key)));
-            } catch (UnsupportedEncodingException e) {
-                LOG.error("Error parsing URI: {}", keyValues.get(key), e);
-                return null;
+                    try {
+                        Preconditions.checkState(listChild instanceof LeafSchemaNode, "List key has to consist of leaves");
+                        builder.append(toUriString(keyValues.get(key), (LeafSchemaNode)listChild, mount));
+                    } catch (final UnsupportedEncodingException e) {
+                        LOG.error("Error parsing URI: {}", keyValues.get(key), e);
+                        return null;
+                    }
+                    break;
+                }
             }
         }
 
@@ -872,8 +924,8 @@ public class ControllerContext implements SchemaContextListener {
     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 ChoiceSchemaNode) {
+            return childByQName((ChoiceSchemaNode) container, name);
         } else if (container instanceof ContainerSchemaNode) {
             return childByQName((ContainerSchemaNode) container, name);
         } else if (container instanceof ListSchemaNode) {
@@ -888,30 +940,32 @@ public class ControllerContext implements SchemaContextListener {
         }
     }
 
-    public Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final YangInstanceIdentifier legacy,
-            final CompositeNode compositeNode) {
+    public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
         try {
-            return dataNormalizer.toNormalized(legacy, compositeNode);
-        } catch (NullPointerException e) {
+            return dataNormalizer.toNormalized(legacy);
+        } catch (final NullPointerException e) {
             throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e);
         }
     }
 
-    public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
+    public YangInstanceIdentifier toXpathRepresentation(final YangInstanceIdentifier instanceIdentifier) {
         try {
-            return dataNormalizer.toNormalized(legacy);
-        } catch (NullPointerException e) {
+            return dataNormalizer.toLegacy(instanceIdentifier);
+        } catch (final NullPointerException e) {
             throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e);
+        } catch (final DataNormalizationException e) {
+            throw new RestconfDocumentedException("Data normalizer failed. Normalization isn't possible", e);
         }
     }
 
-    public CompositeNode toLegacy(final YangInstanceIdentifier instanceIdentifier,
-            final NormalizedNode<?,?> normalizedNode) {
+    public boolean isNodeMixin(final YangInstanceIdentifier path) {
+        final DataNormalizationOperation<?> operation;
         try {
-            return dataNormalizer.toLegacy(instanceIdentifier, normalizedNode);
-        } catch (NullPointerException e) {
-            throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e);
+            operation = dataNormalizer.getOperation(path);
+        } catch (final DataNormalizationException e) {
+            throw new RestconfDocumentedException("Data normalizer failed. Normalization isn't possible", e);
         }
+        return operation.isMixin();
     }
 
     public DataNormalizationOperation<?> getRootOperation() {