X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FControllerContext.java;h=f8bcbe3c616ab7910f1d2346543f898c8548cdac;hp=671bd6da8a1dbbb8a6d51cc33c2ac99d52f03101;hb=f21247cc587c0a5155cee4e89e2d22cd2e53105d;hpb=7b82869e73c5a9c8137521f1bf9b4e446d48440c 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 671bd6da8a..f8bcbe3c61 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 @@ -9,13 +9,11 @@ package org.opendaylight.controller.sal.restconf.impl; import com.google.common.base.Function; import com.google.common.base.Objects; -import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.BiMap; -import com.google.common.collect.FluentIterable; import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -28,7 +26,6 @@ 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; @@ -45,11 +42,11 @@ 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.InstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder; -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.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.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; import org.opendaylight.yangtools.yang.model.api.ChoiceNode; @@ -86,8 +83,6 @@ public class ControllerContext implements SchemaContextListener { private static final Splitter SLASH_SPLITTER = Splitter.on('/'); - private static final Splitter COLON_SPLITTER = Splitter.on(':'); - private final BiMap uriToModuleName = HashBiMap. create(); private final Map moduleNameToUri = uriToModuleName.inverse(); @@ -147,7 +142,7 @@ public class ControllerContext implements SchemaContextListener { ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); } - InstanceIdentifierBuilder builder = InstanceIdentifier.builder(); + InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder(); Module latestModule = this.getLatestModule(globalSchema, startModule); InstanceIdWithSchemaNode iiWithSchemaNode = this.collectPathArguments(builder, pathArgs, latestModule, null, toMountPointIdentifier); @@ -252,7 +247,7 @@ public class ControllerContext implements SchemaContextListener { module.getRevision()); } - public DataNodeContainer getDataNodeContainerFor(final InstanceIdentifier path) { + public DataNodeContainer getDataNodeContainerFor(final YangInstanceIdentifier path) { this.checkPreconditions(); final Iterable elements = path.getPathArguments(); @@ -264,7 +259,7 @@ public class ControllerContext implements SchemaContextListener { for (final PathArgument element : elements) { QName _nodeType = element.getNodeType(); final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType); - if (potentialNode == null || !this.isListOrContainer(potentialNode)) { + if (potentialNode == null || !ControllerContext.isListOrContainer(potentialNode)) { return null; } node = (DataNodeContainer) potentialNode; @@ -273,7 +268,7 @@ public class ControllerContext implements SchemaContextListener { return node; } - public String toFullRestconfIdentifier(final InstanceIdentifier path) { + public String toFullRestconfIdentifier(final YangInstanceIdentifier path) { this.checkPreconditions(); final Iterable elements = path.getPathArguments(); @@ -286,7 +281,7 @@ public class ControllerContext implements SchemaContextListener { for (final PathArgument element : elements) { QName _nodeType = element.getNodeType(); final DataSchemaNode potentialNode = ControllerContext.childByQName(node, _nodeType); - if (!this.isListOrContainer(potentialNode)) { + if (!ControllerContext.isListOrContainer(potentialNode)) { return null; } node = ((DataNodeContainer) potentialNode); @@ -513,9 +508,9 @@ public class ControllerContext implements SchemaContextListener { 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); + DataSchemaNode childByQName = ControllerContext.childByQName(choiceNode, name); if (childByQName != null) { return childByQName; } @@ -561,7 +556,7 @@ public class ControllerContext implements SchemaContextListener { ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED); } - final InstanceIdentifier partialPath = builder.toInstance(); + final YangInstanceIdentifier partialPath = builder.toInstance(); final MountInstance mount = mountService.getMountPoint(partialPath); if (mount == null) { LOG.debug("Instance identifier to missing mount point: {}", partialPath); @@ -576,12 +571,12 @@ public class ControllerContext implements SchemaContextListener { } if (returnJustMountPoint) { - InstanceIdentifier instance = InstanceIdentifier.builder().toInstance(); + YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance(); return new InstanceIdWithSchemaNode(instance, mountPointSchema, mount); } if (strings.size() == 1) { - InstanceIdentifier instance = InstanceIdentifier.builder().toInstance(); + YangInstanceIdentifier instance = YangInstanceIdentifier.builder().toInstance(); return new InstanceIdWithSchemaNode(instance, mountPointSchema, mount); } @@ -600,7 +595,7 @@ public class ControllerContext implements SchemaContextListener { } List subList = strings.subList(1, strings.size()); - return this.collectPathArguments(InstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount, + return this.collectPathArguments(YangInstanceIdentifier.builder(), subList, moduleBehindMountPoint, mount, returnJustMountPoint); } @@ -652,7 +647,7 @@ public class ControllerContext implements SchemaContextListener { targetNode = potentialSchemaNodes.iterator().next(); } - if (!this.isListOrContainer(targetNode)) { + if (!ControllerContext.isListOrContainer(targetNode)) { throw new RestconfDocumentedException("URI has bad format. Node \"" + head + "\" must be Container or List yang type.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); } @@ -796,60 +791,41 @@ public class ControllerContext implements SchemaContextListener { } private static String toModuleName(final String str) { - Preconditions. checkNotNull(str); - if (str.indexOf(':') != -1) { - final Iterable args = COLON_SPLITTER.split(str); - if (Iterables.size(args) == 2) { - return args.iterator().next(); - } + final int idx = str.indexOf(':'); + if (idx == -1) { + return null; } - return null; + + // Make sure there is only one occurrence + if (str.indexOf(':', idx + 1) != -1) { + return null; + } + + return str.substring(0, idx); } private static String toNodeName(final String str) { - if (str.indexOf(':') != -1) { - final Iterable args = COLON_SPLITTER.split(str); - if (Iterables.size(args) == 2) { - return Iterables.get(args, 1); - } + final int idx = str.indexOf(':'); + if (idx == -1) { + return str; + } + + // Make sure there is only one occurrence + if (str.indexOf(':', idx + 1) != -1) { + return str; } - return str; + + return str.substring(idx + 1); } private QName toQName(final String name) { final String module = toModuleName(name); final String node = toNodeName(name); - Set modules = globalSchema.getModules(); - - final Comparator comparator = new Comparator() { - @Override - public int compare(final Module o1, final Module o2) { - return o1.getRevision().compareTo(o2.getRevision()); - } - }; - - List sorted = new ArrayList(modules); - Collections. sort(new ArrayList(modules), comparator); - - final Function transform = new Function() { - @Override - public QName apply(final Module m) { - return QName.create(m.getNamespace(), m.getRevision(), m.getName()); - } - }; - - final Predicate findFirst = new Predicate() { - @Override - public boolean apply(final QName qn) { - return Objects.equal(module, qn.getLocalName()); - } - }; - - Optional namespace = FluentIterable.from(sorted).transform(transform).firstMatch(findFirst); - return namespace.isPresent() ? QName.create(namespace.get(), node) : null; + 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; }