use of org.opendaylight.yangtools.yang.model.api.DataNodeContainer in project netconf by opendaylight.
the class XmlBodyReaderTest method moduleSubContainerDataPutTest.
@Test
public void moduleSubContainerDataPutTest() throws Exception {
final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
final String uri = "instance-identifier-module:cont/cont1";
mockBodyReader(uri, xmlBodyReader, false);
final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, mediaType, null, XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
checkNormalizedNodePayload(payload);
checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, payload, dataII);
}
use of org.opendaylight.yangtools.yang.model.api.DataNodeContainer in project netconf by opendaylight.
the class XmlBodyReaderMountPointTest method checkExpectValueNormalizeNodeContext.
private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode, final NormalizedNodePayload nnContext, final QName qualifiedName) {
YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
final DataSchemaNode mountDataSchemaNode = modelContext(mountPoint).getDataChildByName(dataSchemaNode.getQName());
assertNotNull(mountDataSchemaNode);
if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
assertEquals(nnContext.getInstanceIdentifierContext().getSchemaNode(), child);
} else {
assertEquals(mountDataSchemaNode, dataSchemaNode);
}
assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
}
use of org.opendaylight.yangtools.yang.model.api.DataNodeContainer in project netconf by opendaylight.
the class TestRestconfUtils method parse.
private static NormalizedNode parse(final InstanceIdentifierContext<?> iiContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
final SchemaNode schemaNodeContext = iiContext.getSchemaNode();
DataSchemaNode schemaNode = null;
if (schemaNodeContext instanceof RpcDefinition) {
if ("input".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) {
schemaNode = ((RpcDefinition) schemaNodeContext).getInput();
} else if ("output".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) {
schemaNode = ((RpcDefinition) schemaNodeContext).getOutput();
} else {
throw new IllegalStateException("Unknown Rpc input node");
}
} else if (schemaNodeContext instanceof DataSchemaNode) {
schemaNode = (DataSchemaNode) schemaNodeContext;
} else {
throw new IllegalStateException("Unknow SchemaNode");
}
final String docRootElm = doc.getDocumentElement().getLocalName();
final String schemaNodeName = iiContext.getSchemaNode().getQName().getLocalName();
if (!schemaNodeName.equalsIgnoreCase(docRootElm)) {
for (final DataSchemaNode child : ((DataNodeContainer) schemaNode).getChildNodes()) {
if (child.getQName().getLocalName().equalsIgnoreCase(docRootElm)) {
schemaNode = child;
break;
}
}
}
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.ofInstantiatedPath(iiContext.getSchemaContext(), schemaNode.getPath()).toInference());
if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode) {
xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
return resultHolder.getResult();
}
// FIXME : add another DataSchemaNode extensions e.g. LeafSchemaNode
return null;
}
use of org.opendaylight.yangtools.yang.model.api.DataNodeContainer in project netconf by opendaylight.
the class ControllerContext method getRestconfModuleRestConfSchemaNode.
public DataSchemaNode getRestconfModuleRestConfSchemaNode(final Module inRestconfModule, final String schemaNodeName) {
Module restconfModule = inRestconfModule;
if (restconfModule == null) {
restconfModule = getRestconfModule();
}
if (restconfModule == null) {
return null;
}
final Collection<? extends GroupingDefinition> groupings = restconfModule.getGroupings();
final Iterable<? extends GroupingDefinition> filteredGroups = Iterables.filter(groupings, g -> RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName()));
final GroupingDefinition restconfGrouping = Iterables.getFirst(filteredGroups, null);
final List<DataSchemaNode> instanceDataChildrenByName = findInstanceDataChildrenByName(restconfGrouping, RestConfModule.RESTCONF_CONTAINER_SCHEMA_NODE);
final DataSchemaNode restconfContainer = Iterables.getFirst(instanceDataChildrenByName, null);
if (RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE.equals(schemaNodeName)) {
final List<DataSchemaNode> instances = findInstanceDataChildrenByName((DataNodeContainer) restconfContainer, RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE);
return Iterables.getFirst(instances, null);
} else if (RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE.equals(schemaNodeName)) {
final List<DataSchemaNode> instances = findInstanceDataChildrenByName((DataNodeContainer) restconfContainer, RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
return Iterables.getFirst(instances, null);
} else if (RestConfModule.STREAM_LIST_SCHEMA_NODE.equals(schemaNodeName)) {
List<DataSchemaNode> instances = findInstanceDataChildrenByName((DataNodeContainer) restconfContainer, RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
final DataSchemaNode modules = Iterables.getFirst(instances, null);
instances = findInstanceDataChildrenByName((DataNodeContainer) modules, RestConfModule.STREAM_LIST_SCHEMA_NODE);
return Iterables.getFirst(instances, null);
} else if (RestConfModule.MODULES_CONTAINER_SCHEMA_NODE.equals(schemaNodeName)) {
final List<DataSchemaNode> instances = findInstanceDataChildrenByName((DataNodeContainer) restconfContainer, RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
return Iterables.getFirst(instances, null);
} else if (RestConfModule.MODULE_LIST_SCHEMA_NODE.equals(schemaNodeName)) {
List<DataSchemaNode> instances = findInstanceDataChildrenByName((DataNodeContainer) restconfContainer, RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
final DataSchemaNode modules = Iterables.getFirst(instances, null);
instances = findInstanceDataChildrenByName((DataNodeContainer) modules, RestConfModule.MODULE_LIST_SCHEMA_NODE);
return Iterables.getFirst(instances, null);
} else if (RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE.equals(schemaNodeName)) {
final List<DataSchemaNode> instances = findInstanceDataChildrenByName((DataNodeContainer) restconfContainer, RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
return Iterables.getFirst(instances, null);
}
return null;
}
use of org.opendaylight.yangtools.yang.model.api.DataNodeContainer in project netconf by opendaylight.
the class ControllerContext method collectPathArguments.
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification = "Unrecognised NullableDecl")
private InstanceIdentifierContext<?> collectPathArguments(final InstanceIdentifierBuilder builder, final List<String> strings, final DataNodeContainer parentNode, final DOMMountPoint mountPoint, final boolean returnJustMountPoint) {
requireNonNull(strings);
if (parentNode == null) {
return null;
}
if (strings.isEmpty()) {
return createContext(builder.build(), (DataSchemaNode) parentNode, mountPoint, mountPoint != null ? getModelContext(mountPoint) : globalSchema);
}
final String head = strings.iterator().next();
if (head.isEmpty()) {
final List<String> remaining = strings.subList(1, strings.size());
return collectPathArguments(builder, remaining, parentNode, mountPoint, returnJustMountPoint);
}
final String nodeName = toNodeName(head);
final String moduleName = toModuleName(head);
DataSchemaNode targetNode = null;
if (!Strings.isNullOrEmpty(moduleName)) {
if (MOUNT_MODULE.equals(moduleName) && MOUNT_NODE.equals(nodeName)) {
if (mountPoint != null) {
throw new RestconfDocumentedException("Restconf supports just one mount point in URI.", ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
}
if (mountService == null) {
throw new RestconfDocumentedException("MountService was not found. Finding behind mount points does not work.", ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED);
}
final YangInstanceIdentifier partialPath = dataNormalizer.toNormalized(builder.build());
final Optional<DOMMountPoint> mountOpt = mountService.getMountPoint(partialPath);
if (mountOpt.isEmpty()) {
LOG.debug("Instance identifier to missing mount point: {}", partialPath);
throw new RestconfDocumentedException("Mount point does not exist.", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
}
final DOMMountPoint mount = mountOpt.get();
final EffectiveModelContext mountPointSchema = getModelContext(mount);
if (mountPointSchema == null) {
throw new RestconfDocumentedException("Mount point does not contain any schema with modules.", ErrorType.APPLICATION, ErrorTag.UNKNOWN_ELEMENT);
}
if (returnJustMountPoint || strings.size() == 1) {
return new InstanceIdentifierContext<>(YangInstanceIdentifier.empty(), mountPointSchema, mount, mountPointSchema);
}
final String moduleNameBehindMountPoint = toModuleName(strings.get(1));
if (moduleNameBehindMountPoint == null) {
throw new RestconfDocumentedException("First node after mount point in URI has to be in format \"moduleName:nodeName\"", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
final Iterator<? extends Module> it = mountPointSchema.findModules(moduleNameBehindMountPoint).iterator();
if (!it.hasNext()) {
throw new RestconfDocumentedException("\"" + moduleNameBehindMountPoint + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
}
final List<String> subList = strings.subList(1, strings.size());
return collectPathArguments(YangInstanceIdentifier.builder(), subList, it.next(), mount, returnJustMountPoint);
}
Module module = null;
if (mountPoint == null) {
checkPreconditions();
module = globalSchema.findModules(moduleName).stream().findFirst().orElse(null);
if (module == null) {
throw new RestconfDocumentedException("\"" + moduleName + "\" module does not exist.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
}
} else {
final EffectiveModelContext schemaContext = getModelContext(mountPoint);
if (schemaContext != null) {
module = schemaContext.findModules(moduleName).stream().findFirst().orElse(null);
} else {
module = null;
}
if (module == null) {
throw new RestconfDocumentedException("\"" + moduleName + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
}
}
targetNode = findInstanceDataChildByNameAndNamespace(parentNode, nodeName, module.getNamespace());
if (targetNode == null && parentNode instanceof Module) {
final RpcDefinition rpc;
if (mountPoint == null) {
rpc = getRpcDefinition(head, module.getRevision());
} else {
final String rpcName = toNodeName(head);
rpc = getRpcDefinition(module, rpcName);
}
if (rpc != null) {
return new InstanceIdentifierContext<>(builder.build(), rpc, mountPoint, mountPoint != null ? getModelContext(mountPoint) : 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 + "\" is behind mount point. Then it should be in format \"/" + MOUNT + "/" + head + "\".", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
} else {
final List<DataSchemaNode> potentialSchemaNodes = findInstanceDataChildrenByName(parentNode, nodeName);
if (potentialSchemaNodes.size() > 1) {
final StringBuilder strBuilder = new StringBuilder();
for (final DataSchemaNode potentialNodeSchema : potentialSchemaNodes) {
strBuilder.append(" ").append(potentialNodeSchema.getQName().getNamespace()).append("\n");
}
throw new RestconfDocumentedException("URI has bad format. Node \"" + nodeName + "\" is added as augment from more than one module. " + "Therefore the node must have module name " + "and it has to be in format \"moduleName:nodeName\"." + "\nThe node is added as augment from modules with namespaces:\n" + strBuilder.toString(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
if (potentialSchemaNodes.isEmpty()) {
throw new RestconfDocumentedException("\"" + nodeName + "\" in URI was not found in parent data node", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
}
targetNode = potentialSchemaNodes.iterator().next();
}
if (!isListOrContainer(targetNode)) {
throw new RestconfDocumentedException("URI has bad format. Node \"" + head + "\" must be Container or List yang type.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
int consumed = 1;
if (targetNode instanceof ListSchemaNode) {
final ListSchemaNode listNode = (ListSchemaNode) targetNode;
final int keysSize = listNode.getKeyDefinition().size();
if (strings.size() - consumed < keysSize) {
throw new RestconfDocumentedException("Missing key for list \"" + listNode.getQName().getLocalName() + "\".", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
}
final List<String> uriKeyValues = strings.subList(consumed, consumed + keysSize);
final HashMap<QName, Object> keyValues = new HashMap<>();
int index = 0;
for (final QName key : listNode.getKeyDefinition()) {
{
final String uriKeyValue = uriKeyValues.get(index);
if (uriKeyValue.equals(NULL_VALUE)) {
throw new RestconfDocumentedException("URI has bad format. List \"" + listNode.getQName().getLocalName() + "\" cannot contain \"null\" value as a key.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
addKeyValue(keyValues, listNode.getDataChildByName(key), uriKeyValue, mountPoint);
index++;
}
}
consumed = consumed + index;
builder.nodeWithKey(targetNode.getQName(), keyValues);
} else {
builder.node(targetNode.getQName());
}
if (targetNode instanceof DataNodeContainer) {
final List<String> remaining = strings.subList(consumed, strings.size());
return collectPathArguments(builder, remaining, (DataNodeContainer) targetNode, mountPoint, returnJustMountPoint);
}
return createContext(builder.build(), targetNode, mountPoint, mountPoint != null ? getModelContext(mountPoint) : globalSchema);
}
Aggregations