Search in sources :

Example 36 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class RestconfOperationsServiceImpl method getOperations.

@Override
public NormalizedNodePayload getOperations(final String identifier, final UriInfo uriInfo) {
    if (!identifier.contains(RestconfConstants.MOUNT)) {
        final String errMsg = "URI has bad format. If operations behind mount point should be showed, URI has to " + " end with " + RestconfConstants.MOUNT;
        LOG.debug("{} for {}", errMsg, identifier);
        throw new RestconfDocumentedException(errMsg + RestconfConstants.MOUNT, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    final InstanceIdentifierContext<?> mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(identifier, schemaContextHandler.get(), Optional.of(mountPointService));
    final DOMMountPoint mountPoint = mountPointIdentifier.getMountPoint();
    final var entry = OperationsResourceUtils.contextForModelContext(modelContext(mountPoint), mountPoint);
    return NormalizedNodePayload.of(entry.getKey(), entry.getValue());
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint)

Example 37 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint 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)

Example 38 with DOMMountPoint

use of org.opendaylight.mdsal.dom.api.DOMMountPoint in project netconf by opendaylight.

the class TestJsonBodyReaderMountPoint method checkExpectValueNormalizeNodeContext.

protected void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode, final NormalizedNodeContext 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();
        assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
    } else {
        assertTrue(mountDataSchemaNode.equals(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)

Aggregations

DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)38 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)16 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)15 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)13 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)9 NormalizedNodeContext (org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext)8 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)8 MdsalRestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy)7 RestconfStrategy (org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy)7 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)6 Test (org.junit.Test)5 DOMRpcService (org.opendaylight.mdsal.dom.api.DOMRpcService)5 QName (org.opendaylight.yangtools.yang.common.QName)5 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ExecutionException (java.util.concurrent.ExecutionException)4 DOMDataTreeReadWriteTransaction (org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction)4 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)4 SchemaNode (org.opendaylight.yangtools.yang.model.api.SchemaNode)4