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;
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;
private volatile SchemaContext globalSchema;
private volatile DOMMountPointService mountService;
+ private DataNormalizer dataNormalizer;
+
public void setGlobalSchema(final SchemaContext globalSchema) {
this.globalSchema = globalSchema;
+ this.dataNormalizer = new DataNormalizer(globalSchema);
}
public void setMountService(final DOMMountPointService mountService) {
}
InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
- Module latestModule = this.getLatestModule(globalSchema, startModule);
+ Module latestModule = globalSchema.findModuleByName(startModule, null);
InstanceIdWithSchemaNode iiWithSchemaNode = this.collectPathArguments(builder, pathArgs, latestModule, null,
toMountPointIdentifier);
return list;
}
-
- 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);
- }
- };
-
- Iterable<Module> modules = Iterables.filter(schema.getModules(), filter);
- return this.filterLatestModule(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())) {
- latestModule = module;
- }
- }
- return latestModule;
- }
-
public Module findModuleByName(final String moduleName) {
this.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();
Preconditions.checkArgument(namespace != null);
-
- final Set<Module> 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<Module> 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) {
return findModuleByNameAndRevision(Draft02.RestConfModule.IETF_RESTCONF_QNAME);
}
+ private static final Predicate<GroupingDefinition> ERRORS_GROUPING_FILTER = new Predicate<GroupingDefinition>() {
+ @Override
+ public boolean apply(final GroupingDefinition g) {
+ return Draft02.RestConfModule.ERRORS_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName());
+ }
+ };
+
public DataSchemaNode getRestconfModuleErrorsSchemaNode() {
Module restconfModule = getRestconfModule();
if (restconfModule == null) {
Set<GroupingDefinition> groupings = restconfModule.getGroupings();
- 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);
- }
- };
-
- Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, filter);
+ Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, ERRORS_GROUPING_FILTER);
final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
return Iterables.getFirst(instanceDataChildrenByName, null);
}
+ private static final Predicate<GroupingDefinition> GROUPING_FILTER = new Predicate<GroupingDefinition>() {
+ @Override
+ public boolean apply(final GroupingDefinition g) {
+ return Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName());
+ }
+ };
+
public DataSchemaNode getRestconfModuleRestConfSchemaNode(final Module inRestconfModule, final String schemaNodeName) {
Module restconfModule = inRestconfModule;
if (restconfModule == null) {
}
Set<GroupingDefinition> groupings = restconfModule.getGroupings();
-
- 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);
- }
- };
-
- Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, filter);
-
+ Iterable<GroupingDefinition> filteredGroups = Iterables.filter(groupings, GROUPING_FILTER);
final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
List<DataSchemaNode> instanceDataChildrenByName = this.findInstanceDataChildrenByName(restconfGrouping,
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;
}
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);
Module module = null;
if (mountPoint == null) {
- module = this.getLatestModule(globalSchema, moduleName);
+ 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);
+ 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);
return instantiatedDataNodeContainers;
}
+ private static final Function<ChoiceNode, Set<ChoiceCaseNode>> CHOICE_FUNCTION = new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
+ @Override
+ public Set<ChoiceCaseNode> apply(final ChoiceNode node) {
+ return node.getCases();
+ }
+ };
+
private void collectInstanceDataNodeContainers(final List<DataSchemaNode> potentialSchemaNodes,
final DataNodeContainer container, final String name) {
}
}
- Iterable<ChoiceNode> choiceNodes = Iterables.<ChoiceNode> filter(container.getChildNodes(), ChoiceNode.class);
-
- final Function<ChoiceNode, Set<ChoiceCaseNode>> choiceFunction = new Function<ChoiceNode, Set<ChoiceCaseNode>>() {
- @Override
- public Set<ChoiceCaseNode> apply(final ChoiceNode node) {
- return node.getCases();
- }
- };
-
- Iterable<Set<ChoiceCaseNode>> map = Iterables.<ChoiceNode, Set<ChoiceCaseNode>> transform(choiceNodes,
- choiceFunction);
+ Iterable<ChoiceNode> choiceNodes = Iterables.filter(container.getChildNodes(), ChoiceNode.class);
+ Iterable<Set<ChoiceCaseNode>> map = Iterables.transform(choiceNodes, CHOICE_FUNCTION);
final Iterable<ChoiceCaseNode> allCases = Iterables.<ChoiceCaseNode> concat(map);
for (final ChoiceCaseNode caze : allCases) {
+ Arrays.<Object> asList(container, name).toString());
}
}
+
+ public Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final YangInstanceIdentifier legacy,
+ final CompositeNode compositeNode) {
+ try {
+ return dataNormalizer.toNormalized(legacy, compositeNode);
+ } catch (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 (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 (NullPointerException e) {
+ throw new RestconfDocumentedException("Data normalizer isn't set. Normalization isn't possible", e);
+ }
+ }
+
+ public DataNormalizationOperation<?> getRootOperation() {
+ return dataNormalizer.getRootOperation();
+ }
+
}