Search in sources :

Example 31 with DataNodeContainer

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);
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) NormalizedNodePayload(org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 32 with DataNodeContainer

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));
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 33 with DataNodeContainer

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;
}
Also used : ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) DOMSource(javax.xml.transform.dom.DOMSource) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer)

Example 34 with DataNodeContainer

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;
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) List(java.util.List) ArrayList(java.util.ArrayList) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) Module(org.opendaylight.yangtools.yang.model.api.Module) RestConfModule(org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule) GroupingDefinition(org.opendaylight.yangtools.yang.model.api.GroupingDefinition)

Example 35 with DataNodeContainer

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);
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) Module(org.opendaylight.yangtools.yang.model.api.Module) RestConfModule(org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

DataNodeContainer (org.opendaylight.yangtools.yang.model.api.DataNodeContainer)41 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)34 QName (org.opendaylight.yangtools.yang.common.QName)15 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)14 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)12 ChoiceSchemaNode (org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)12 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)11 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)10 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)9 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)9 ArrayList (java.util.ArrayList)7 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)7 RestConfModule (org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 Test (org.junit.Test)5 AugmentationSchemaNode (org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode)5 Module (org.opendaylight.yangtools.yang.model.api.Module)5 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ArrayDeque (java.util.ArrayDeque)3