X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FControllerContext.java;h=5f6604c68de3692343668266cadd4fcdb1e5a956;hb=dddd84ae73bdefcfb2ef1de77a98b9462eeff66d;hp=6db4e63d10f74216cf041f847c5c43be6b573d47;hpb=bdcd6c4baea3357499a1fcdff459259b56373baa;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java index 6db4e63d10..5f6604c68d 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java @@ -14,8 +14,6 @@ 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; @@ -29,9 +27,12 @@ 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.DataNormalizationOperation; +import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.controller.sal.rest.api.Draft02; @@ -40,11 +41,13 @@ 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.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; @@ -81,18 +84,17 @@ public class ControllerContext implements SchemaContextListener { private static final Splitter SLASH_SPLITTER = Splitter.on('/'); - private final BiMap uriToModuleName = HashBiMap. create(); - - private final Map moduleNameToUri = uriToModuleName.inverse(); - private final AtomicReference> qnameToRpc = new AtomicReference<>(Collections.emptyMap()); private volatile SchemaContext globalSchema; private volatile DOMMountPointService mountService; + private DataNormalizer dataNormalizer; + public void setGlobalSchema(final SchemaContext globalSchema) { this.globalSchema = globalSchema; + dataNormalizer = new DataNormalizer(globalSchema); } public void setMountService(final DOMMountPointService mountService) { @@ -113,19 +115,23 @@ public class ControllerContext implements SchemaContextListener { } public void setSchemas(final SchemaContext schemas) { - this.onGlobalContextUpdated(schemas); + onGlobalContextUpdated(schemas); } - public InstanceIdWithSchemaNode toInstanceIdentifier(final String restconfInstance) { - return this.toIdentifier(restconfInstance, false); + public InstanceIdentifierContext toInstanceIdentifier(final String restconfInstance) { + return toIdentifier(restconfInstance, false); } - public InstanceIdWithSchemaNode toMountPointIdentifier(final String restconfInstance) { - return this.toIdentifier(restconfInstance, true); + public SchemaContext getGlobalSchema() { + return globalSchema; } - private InstanceIdWithSchemaNode toIdentifier(final String restconfInstance, final boolean toMountPointIdentifier) { - this.checkPreconditions(); + public InstanceIdentifierContext toMountPointIdentifier(final String restconfInstance) { + return toIdentifier(restconfInstance, true); + } + + private InstanceIdentifierContext toIdentifier(final String restconfInstance, final boolean toMountPointIdentifier) { + checkPreconditions(); final List pathArgs = urlPathArgsDecode(SLASH_SPLITTER.split(restconfInstance)); omitFirstAndLastEmptyString(pathArgs); @@ -133,16 +139,16 @@ 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 = this.getLatestModule(globalSchema, startModule); - InstanceIdWithSchemaNode iiWithSchemaNode = this.collectPathArguments(builder, pathArgs, latestModule, null, + final InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder(); + final Module latestModule = globalSchema.findModuleByName(startModule, null); + final InstanceIdentifierContext iiWithSchemaNode = collectPathArguments(builder, pathArgs, latestModule, null, toMountPointIdentifier); if (iiWithSchemaNode == null) { @@ -157,7 +163,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); } @@ -166,96 +172,75 @@ 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); } return list; } - - private Module getLatestModule(final SchemaContext schema, final String moduleName) { - Preconditions.checkArgument(schema != null); - Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty()); - - Predicate filter = new Predicate() { - @Override - public boolean apply(final Module m) { - return Objects.equal(m.getName(), moduleName); - } - }; - - Iterable modules = Iterables.filter(schema.getModules(), filter); - return this.filterLatestModule(modules); - } - - private Module filterLatestModule(final Iterable modules) { - Module latestModule = modules.iterator().hasNext() ? modules.iterator().next() : null; - for (final Module module : modules) { - if (module.getRevision().after(latestModule.getRevision())) { - latestModule = module; - } - } - return latestModule; - } - public Module findModuleByName(final String moduleName) { - this.checkPreconditions(); + checkPreconditions(); Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty()); - return this.getLatestModule(globalSchema, moduleName); + return globalSchema.findModuleByName(moduleName, null); } public Module findModuleByName(final DOMMountPoint mountPoint, final String moduleName) { Preconditions.checkArgument(moduleName != null && mountPoint != null); final SchemaContext mountPointSchema = mountPoint.getSchemaContext(); - return mountPointSchema == null ? null : this.getLatestModule(mountPointSchema, moduleName); + if (mountPointSchema == null) { + return null; + } + + return mountPointSchema.findModuleByName(moduleName, null); } public Module findModuleByNamespace(final URI namespace) { - this.checkPreconditions(); + checkPreconditions(); Preconditions.checkArgument(namespace != null); - - final Set moduleSchemas = globalSchema.findModuleByNamespace(namespace); - return moduleSchemas == null ? null : this.filterLatestModule(moduleSchemas); + return globalSchema.findModuleByNamespaceAndRevision(namespace, null); } public Module findModuleByNamespace(final DOMMountPoint mountPoint, final URI namespace) { Preconditions.checkArgument(namespace != null && mountPoint != null); final SchemaContext mountPointSchema = mountPoint.getSchemaContext(); - Set moduleSchemas = mountPointSchema == null ? null : mountPointSchema.findModuleByNamespace(namespace); - return moduleSchemas == null ? null : this.filterLatestModule(moduleSchemas); + if (mountPointSchema == null) { + return null; + } + + return mountPointSchema.findModuleByNamespaceAndRevision(namespace, null); } 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 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; @@ -267,17 +252,17 @@ public class ControllerContext implements SchemaContextListener { } public String toFullRestconfIdentifier(final YangInstanceIdentifier path) { - this.checkPreconditions(); + checkPreconditions(); final Iterable 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(), 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 (!ControllerContext.isListOrContainer(potentialNode)) { return null; @@ -290,18 +275,10 @@ public class ControllerContext implements SchemaContextListener { } 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) { - moduleName = module.getName(); - this.uriToModuleName.put(namespace, moduleName); - } - } + checkPreconditions(); - return moduleName; + final Module module = this.findModuleByNamespace(namespace); + return module == null ? null : module.getName(); } public String findModuleNameByNamespace(final DOMMountPoint mountPoint, final URI namespace) { @@ -310,16 +287,8 @@ public class ControllerContext implements SchemaContextListener { } 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); - } - } - return namespace; + final Module module = this.findModuleByName(moduleName); + return module == null ? null : module.getNamespace(); } public URI findNamespaceByModuleName(final DOMMountPoint mountPoint, final String moduleName) { @@ -328,37 +297,26 @@ public class ControllerContext implements SchemaContextListener { } public Set 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 getAllModules() { - this.checkPreconditions(); + checkPreconditions(); return globalSchema.getModules(); } - 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) { - return null; - } + private static final CharSequence toRestconfIdentifier(final SchemaContext context, final QName qname) { + final Module schema = context.findModuleByNamespaceAndRevision(qname.getNamespace(), qname.getRevision()); + return schema == null ? null : schema.getName() + ':' + qname.getLocalName(); + } - this.uriToModuleName.put(qname.getNamespace(), moduleSchema.getName()); - module = moduleSchema.getName(); - } + public CharSequence toRestconfIdentifier(final QName qname) { + checkPreconditions(); - StringBuilder builder = new StringBuilder(); - builder.append(module); - builder.append(":"); - builder.append(qname.getLocalName()); - return builder.toString(); + return toRestconfIdentifier(globalSchema, qname); } public CharSequence toRestconfIdentifier(final DOMMountPoint mountPoint, final QName qname) { @@ -366,49 +324,44 @@ public class ControllerContext implements SchemaContextListener { return null; } - SchemaContext schemaContext = mountPoint.getSchemaContext(); - - 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()); - return builder.toString(); + return toRestconfIdentifier(mountPoint.getSchemaContext(), qname); } public Module getRestconfModule() { return findModuleByNameAndRevision(Draft02.RestConfModule.IETF_RESTCONF_QNAME); } + private static final Predicate ERRORS_GROUPING_FILTER = new Predicate() { + @Override + public boolean apply(final GroupingDefinition g) { + return Draft02.RestConfModule.ERRORS_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName()); + } + }; + public DataSchemaNode getRestconfModuleErrorsSchemaNode() { - Module restconfModule = getRestconfModule(); + final Module restconfModule = getRestconfModule(); if (restconfModule == null) { return null; } - Set groupings = restconfModule.getGroupings(); + final Set groupings = restconfModule.getGroupings(); - final Predicate filter = new Predicate() { - @Override - public boolean apply(final GroupingDefinition g) { - return Objects.equal(g.getQName().getLocalName(), Draft02.RestConfModule.ERRORS_GROUPING_SCHEMA_NODE); - } - }; - - Iterable filteredGroups = Iterables.filter(groupings, filter); + final Iterable filteredGroups = Iterables.filter(groupings, ERRORS_GROUPING_FILTER); final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null); - List instanceDataChildrenByName = this.findInstanceDataChildrenByName(restconfGrouping, + final List instanceDataChildrenByName = findInstanceDataChildrenByName(restconfGrouping, Draft02.RestConfModule.ERRORS_CONTAINER_SCHEMA_NODE); return Iterables.getFirst(instanceDataChildrenByName, null); } + private static final Predicate GROUPING_FILTER = new Predicate() { + @Override + public boolean apply(final GroupingDefinition g) { + return Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName()); + } + }; + public DataSchemaNode getRestconfModuleRestConfSchemaNode(final Module inRestconfModule, final String schemaNodeName) { Module restconfModule = inRestconfModule; if (restconfModule == null) { @@ -419,51 +372,42 @@ public class ControllerContext implements SchemaContextListener { return null; } - Set groupings = restconfModule.getGroupings(); - - final Predicate filter = new Predicate() { - @Override - public boolean apply(final GroupingDefinition g) { - return Objects.equal(g.getQName().getLocalName(), Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE); - } - }; - - Iterable filteredGroups = Iterables.filter(groupings, filter); - + final Set groupings = restconfModule.getGroupings(); + final Iterable filteredGroups = Iterables.filter(groupings, GROUPING_FILTER); final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null); - List instanceDataChildrenByName = this.findInstanceDataChildrenByName(restconfGrouping, + final List 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 instances = this.findInstanceDataChildrenByName( + final List 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 instances = this.findInstanceDataChildrenByName( + final List 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)) { - List instances = this.findInstanceDataChildrenByName( + List instances = findInstanceDataChildrenByName( ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE); final DataSchemaNode modules = Iterables.getFirst(instances, null); - instances = this.findInstanceDataChildrenByName(((DataNodeContainer) modules), + instances = 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 instances = this.findInstanceDataChildrenByName( + final List 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)) { - List instances = this.findInstanceDataChildrenByName( + List instances = findInstanceDataChildrenByName( ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE); final DataSchemaNode modules = Iterables.getFirst(instances, null); - instances = this.findInstanceDataChildrenByName(((DataNodeContainer) modules), + instances = 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 instances = this.findInstanceDataChildrenByName( + final List instances = findInstanceDataChildrenByName( ((DataNodeContainer) restconfContainer), Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE); return Iterables.getFirst(instances, null); } @@ -503,12 +447,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 ChoiceCaseNode)) { - final ChoiceCaseNode caseNode = ((ChoiceCaseNode) node); - DataSchemaNode childByQName = ControllerContext.childByQName(caseNode, name); + if ((node instanceof ChoiceNode)) { + final ChoiceNode choiceNode = ((ChoiceNode) node); + final DataSchemaNode childByQName = ControllerContext.childByQName(choiceNode, name); if (childByQName != null) { return childByQName; } @@ -522,7 +466,7 @@ public class ControllerContext implements SchemaContextListener { return object == null ? "" : URLEncoder.encode(object.toString(), ControllerContext.URI_ENCODING_CHAR_SET); } - private InstanceIdWithSchemaNode collectPathArguments(final InstanceIdentifierBuilder builder, + private InstanceIdentifierContext collectPathArguments(final InstanceIdentifierBuilder builder, final List strings, final DataNodeContainer parentNode, final DOMMountPoint mountPoint, final boolean returnJustMountPoint) { Preconditions.> checkNotNull(strings); @@ -532,10 +476,10 @@ public class ControllerContext implements SchemaContextListener { } if (strings.isEmpty()) { - return new InstanceIdWithSchemaNode(builder.toInstance(), ((DataSchemaNode) parentNode), mountPoint); + return new InstanceIdentifierContext(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); @@ -561,7 +505,7 @@ public class ControllerContext implements SchemaContextListener { 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) { @@ -570,13 +514,13 @@ public class ControllerContext implements SchemaContextListener { } if (returnJustMountPoint) { - YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance(); - return new InstanceIdWithSchemaNode(instance, mountPointSchema, mount); + final YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance(); + return new InstanceIdentifierContext(instance, mountPointSchema, mount,mountPointSchema); } if (strings.size() == 1) { - YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance(); - return new InstanceIdWithSchemaNode(instance, mountPointSchema, mount); + final YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance(); + return new InstanceIdentifierContext(instance, mountPointSchema, mount,mountPointSchema); } final String moduleNameBehindMountPoint = toModuleName(strings.get(1)); @@ -586,35 +530,39 @@ public class ControllerContext implements SchemaContextListener { ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); } - final Module moduleBehindMountPoint = this - .getLatestModule(mountPointSchema, moduleNameBehindMountPoint); + final Module moduleBehindMountPoint = mountPointSchema.findModuleByName(moduleNameBehindMountPoint, null); if (moduleBehindMountPoint == null) { throw new RestconfDocumentedException("\"" + moduleName + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT); } - List subList = strings.subList(1, strings.size()); - return this.collectPathArguments(YangInstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount, + final List subList = strings.subList(1, strings.size()); + return collectPathArguments(YangInstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount, returnJustMountPoint); } Module module = null; if (mountPoint == null) { - module = this.getLatestModule(globalSchema, moduleName); + 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(); - module = schemaContext == null ? null : this.getLatestModule(schemaContext, moduleName); + final SchemaContext schemaContext = mountPoint.getSchemaContext(); + if (schemaContext != null) { + module = schemaContext.findModuleByName(moduleName, null); + } else { + module = null; + } 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()); + targetNode = 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 @@ -622,7 +570,7 @@ public class ControllerContext implements SchemaContextListener { ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); } } else { - final List potentialSchemaNodes = this.findInstanceDataChildrenByName(parentNode, nodeName); + final List potentialSchemaNodes = findInstanceDataChildrenByName(parentNode, nodeName); if (potentialSchemaNodes.size() > 1) { final StringBuilder strBuilder = new StringBuilder(); for (final DataSchemaNode potentialNodeSchema : potentialSchemaNodes) { @@ -672,7 +620,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++; } } @@ -685,78 +633,76 @@ public class ControllerContext implements SchemaContextListener { if ((targetNode instanceof DataNodeContainer)) { final List remaining = strings.subList(consumed, strings.size()); - return this.collectPathArguments(builder, remaining, ((DataNodeContainer) targetNode), mountPoint, + return collectPathArguments(builder, remaining, ((DataNodeContainer) targetNode), mountPoint, returnJustMountPoint); } - return new InstanceIdWithSchemaNode(builder.toInstance(), targetNode, mountPoint); + return new InstanceIdentifierContext(builder.toInstance(), targetNode, mountPoint,mountPoint != null ? mountPoint.getSchemaContext() : globalSchema); } - public DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container, final String name, + public static DataSchemaNode findInstanceDataChildByNameAndNamespace(final DataNodeContainer container, final String name, final URI namespace) { Preconditions. checkNotNull(namespace); - final List potentialSchemaNodes = this.findInstanceDataChildrenByName(container, name); + final List potentialSchemaNodes = findInstanceDataChildrenByName(container, name); - Predicate filter = new Predicate() { + final Predicate filter = new Predicate() { @Override public boolean apply(final DataSchemaNode node) { return Objects.equal(node.getQName().getNamespace(), namespace); } }; - Iterable result = Iterables.filter(potentialSchemaNodes, filter); + final Iterable result = Iterables.filter(potentialSchemaNodes, filter); return Iterables.getFirst(result, null); } - public List findInstanceDataChildrenByName(final DataNodeContainer container, final String name) { + public static List findInstanceDataChildrenByName(final DataNodeContainer container, final String name) { Preconditions. checkNotNull(container); Preconditions. checkNotNull(name); - List instantiatedDataNodeContainers = new ArrayList(); - this.collectInstanceDataNodeContainers(instantiatedDataNodeContainers, container, name); + final List instantiatedDataNodeContainers = new ArrayList(); + collectInstanceDataNodeContainers(instantiatedDataNodeContainers, container, name); return instantiatedDataNodeContainers; } - private void collectInstanceDataNodeContainers(final List potentialSchemaNodes, + private static final Function> CHOICE_FUNCTION = new Function>() { + @Override + public Set apply(final ChoiceNode node) { + return node.getCases(); + } + }; + + private static void collectInstanceDataNodeContainers(final List potentialSchemaNodes, final DataNodeContainer container, final String name) { - Predicate filter = new Predicate() { + final Predicate filter = new Predicate() { @Override public boolean apply(final DataSchemaNode node) { return Objects.equal(node.getQName().getLocalName(), name); } }; - Iterable nodes = Iterables.filter(container.getChildNodes(), filter); + final Iterable 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)) { + if (isInstantiatedDataSchema(potentialNode)) { potentialSchemaNodes.add(potentialNode); } } - Iterable choiceNodes = Iterables. filter(container.getChildNodes(), ChoiceNode.class); - - final Function> choiceFunction = new Function>() { - @Override - public Set apply(final ChoiceNode node) { - return node.getCases(); - } - }; - - Iterable> map = Iterables.> transform(choiceNodes, - choiceFunction); + final Iterable choiceNodes = Iterables.filter(container.getChildNodes(), ChoiceNode.class); + final Iterable> map = Iterables.transform(choiceNodes, CHOICE_FUNCTION); final Iterable allCases = Iterables. concat(map); for (final ChoiceCaseNode caze : allCases) { - this.collectInstanceDataNodeContainers(potentialSchemaNodes, caze, name); + collectInstanceDataNodeContainers(potentialSchemaNodes, caze, name); } } - public boolean isInstantiatedDataSchema(final DataSchemaNode node) { + public static boolean isInstantiatedDataSchema(final DataSchemaNode node) { return node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode || node instanceof ContainerSchemaNode || node instanceof ListSchemaNode || node instanceof AnyXmlSchemaNode; @@ -769,14 +715,14 @@ public class ControllerContext implements SchemaContextListener { final String urlDecoded = urlPathArgDecode(uriValue); final TypeDefinition typedef = ((LeafSchemaNode) node).getType(); - Codec codec = RestCodec.from(typedef, mountPoint); + final Codec codec = RestCodec.from(typedef, mountPoint); Object decoded = codec == null ? null : codec.deserialize(urlDecoded); String additionalInfo = ""; if (decoded == null) { - TypeDefinition baseType = RestUtil.resolveBaseTypeFrom(typedef); + final TypeDefinition 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."; } } @@ -818,6 +764,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); @@ -829,8 +776,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 @@ -844,20 +791,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 urlPathArgsDecode(final Iterable strings) { try { - List decodedPathArgs = new ArrayList(); + final List decodedPathArgs = new ArrayList(); 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); } @@ -867,7 +814,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); } @@ -890,36 +837,32 @@ public class ControllerContext implements SchemaContextListener { } private CharSequence convertToRestconfIdentifier(final NodeIdentifier argument, final ContainerSchemaNode node) { - StringBuilder builder = new StringBuilder(); - builder.append("/"); - QName nodeType = argument.getNodeType(); - builder.append(this.toRestconfIdentifier(nodeType)); - return builder.toString(); + return "/" + this.toRestconfIdentifier(argument.getNodeType()); } private CharSequence convertToRestconfIdentifier(final NodeIdentifierWithPredicates argument, final ListSchemaNode node) { - QName nodeType = argument.getNodeType(); + final QName nodeType = argument.getNodeType(); final CharSequence nodeIdentifier = this.toRestconfIdentifier(nodeType); final Map keyValues = argument.getKeyValues(); - StringBuilder builder = new StringBuilder(); - builder.append("/"); + final StringBuilder builder = new StringBuilder(); + builder.append('/'); builder.append(nodeIdentifier); - builder.append("/"); + builder.append('/'); - List keyDefinition = node.getKeyDefinition(); + final List keyDefinition = node.getKeyDefinition(); boolean hasElements = false; for (final QName key : keyDefinition) { if (!hasElements) { hasElements = true; } else { - builder.append("/"); + builder.append('/'); } try { - builder.append(this.toUriString(keyValues.get(key))); - } catch (UnsupportedEncodingException e) { + builder.append(toUriString(keyValues.get(key))); + } catch (final UnsupportedEncodingException e) { LOG.error("Error parsing URI: {}", keyValues.get(key), e); return null; } @@ -946,4 +889,35 @@ public class ControllerContext implements SchemaContextListener { + Arrays. asList(container, name).toString()); } } + + public Entry> toNormalized(final YangInstanceIdentifier legacy, + final CompositeNode compositeNode) { + try { + return dataNormalizer.toNormalized(legacy, compositeNode); + } catch (final NullPointerException e) { + throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e); + } + } + + public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) { + try { + return dataNormalizer.toNormalized(legacy); + } catch (final NullPointerException e) { + throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e); + } + } + + public CompositeNode toLegacy(final YangInstanceIdentifier instanceIdentifier, + final NormalizedNode normalizedNode) { + try { + return dataNormalizer.toLegacy(instanceIdentifier, normalizedNode); + } catch (final NullPointerException e) { + throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e); + } + } + + public DataNormalizationOperation getRootOperation() { + return dataNormalizer.getRootOperation(); + } + }