Search in sources :

Example 41 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class PostDataTransactionUtil method submitData.

/**
 * Post data by type.
 *
 * @param path          path
 * @param data          data
 * @param strategy      object that perform the actual DS operations
 * @param schemaContext schema context of data
 * @param point         query parameter
 * @param insert        query parameter
 * @return {@link FluentFuture}
 */
private static FluentFuture<? extends CommitInfo> submitData(final YangInstanceIdentifier path, final NormalizedNode data, final RestconfStrategy strategy, final EffectiveModelContext schemaContext, final WriteDataParams params) {
    final RestconfTransaction transaction = strategy.prepareWriteExecution();
    final InsertParam insert = params.insert();
    if (insert == null) {
        makePost(path, data, schemaContext, transaction);
        return transaction.commit();
    }
    PutDataTransactionUtil.checkListAndOrderedType(schemaContext, path);
    final NormalizedNode readData;
    switch(insert) {
        case FIRST:
            readData = PutDataTransactionUtil.readList(strategy, path.getParent().getParent());
            if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                transaction.replace(path, data, schemaContext);
                return transaction.commit();
            }
            checkItemDoesNotExists(strategy.exists(LogicalDatastoreType.CONFIGURATION, path), path);
            transaction.remove(path.getParent().getParent());
            transaction.replace(path, data, schemaContext);
            transaction.replace(path.getParent().getParent(), readData, schemaContext);
            return transaction.commit();
        case LAST:
            makePost(path, data, schemaContext, transaction);
            return transaction.commit();
        case BEFORE:
            readData = PutDataTransactionUtil.readList(strategy, path.getParent().getParent());
            if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                transaction.replace(path, data, schemaContext);
                return transaction.commit();
            }
            checkItemDoesNotExists(strategy.exists(LogicalDatastoreType.CONFIGURATION, path), path);
            insertWithPointPost(path, data, schemaContext, params.getPoint(), (NormalizedNodeContainer<?>) readData, true, transaction);
            return transaction.commit();
        case AFTER:
            readData = PutDataTransactionUtil.readList(strategy, path.getParent().getParent());
            if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                transaction.replace(path, data, schemaContext);
                return transaction.commit();
            }
            checkItemDoesNotExists(strategy.exists(LogicalDatastoreType.CONFIGURATION, path), path);
            insertWithPointPost(path, data, schemaContext, params.getPoint(), (NormalizedNodeContainer<?>) readData, false, transaction);
            return transaction.commit();
        default:
            throw new RestconfDocumentedException("Used bad value of insert parameter. Possible values are first, last, before or after, but was: " + insert, ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE);
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) InsertParam(org.opendaylight.restconf.nb.rfc8040.InsertParam) RestconfTransaction(org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 42 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class PutDataTransactionUtil method submitData.

/**
 * Put data to DS.
 *
 * @param path          path of data
 * @param schemaContext {@link SchemaContext}
 * @param strategy      object that perform the actual DS operations
 * @param data          data
 * @param params        {@link WriteDataParams}
 * @return {@link FluentFuture}
 */
private static FluentFuture<? extends CommitInfo> submitData(final YangInstanceIdentifier path, final EffectiveModelContext schemaContext, final RestconfStrategy strategy, final NormalizedNode data, final WriteDataParams params) {
    final RestconfTransaction transaction = strategy.prepareWriteExecution();
    final InsertParam insert = params.insert();
    if (insert == null) {
        return makePut(path, schemaContext, transaction, data);
    }
    checkListAndOrderedType(schemaContext, path);
    final NormalizedNode readData;
    switch(insert) {
        case FIRST:
            readData = readList(strategy, path.getParent());
            if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                return makePut(path, schemaContext, transaction, data);
            }
            transaction.remove(path.getParent());
            transaction.replace(path, data, schemaContext);
            transaction.replace(path.getParent(), readData, schemaContext);
            return transaction.commit();
        case LAST:
            return makePut(path, schemaContext, transaction, data);
        case BEFORE:
            readData = readList(strategy, path.getParent());
            if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                return makePut(path, schemaContext, transaction, data);
            }
            insertWithPointPut(transaction, path, data, schemaContext, params.getPoint(), (NormalizedNodeContainer<?>) readData, true);
            return transaction.commit();
        case AFTER:
            readData = readList(strategy, path.getParent());
            if (readData == null || ((NormalizedNodeContainer<?>) readData).isEmpty()) {
                return makePut(path, schemaContext, transaction, data);
            }
            insertWithPointPut(transaction, path, data, schemaContext, params.getPoint(), (NormalizedNodeContainer<?>) readData, false);
            return transaction.commit();
        default:
            throw new RestconfDocumentedException("Used bad value of insert parameter. Possible values are first, last, before or after, " + "but was: " + insert, ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE);
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) InsertParam(org.opendaylight.restconf.nb.rfc8040.InsertParam) RestconfTransaction(org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 43 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class PutDataTransactionUtil method checkListAndOrderedType.

public static DataSchemaNode checkListAndOrderedType(final EffectiveModelContext ctx, final YangInstanceIdentifier path) {
    final YangInstanceIdentifier parent = path.getParent();
    final DataSchemaContextNode<?> node = DataSchemaContextTree.from(ctx).findChild(parent).orElseThrow();
    final DataSchemaNode dataSchemaNode = node.getDataSchemaNode();
    if (dataSchemaNode instanceof ListSchemaNode) {
        if (!((ListSchemaNode) dataSchemaNode).isUserOrdered()) {
            throw new RestconfDocumentedException("Insert parameter can be used only with ordered-by user list.", ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
        }
        return dataSchemaNode;
    }
    if (dataSchemaNode instanceof LeafListSchemaNode) {
        if (!((LeafListSchemaNode) dataSchemaNode).isUserOrdered()) {
            throw new RestconfDocumentedException("Insert parameter can be used only with ordered-by user leaf-list.", ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
        }
        return dataSchemaNode;
    }
    throw new RestconfDocumentedException("Insert parameter can be used only with list or leaf-list", ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 44 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class ReadDataTransactionUtil method validateNodeMerge.

/**
 * Validates whether the two NormalizedNodes can be merged.
 *
 * @param stateDataNode  data node of state data
 * @param configDataNode data node of config data
 */
private static void validateNodeMerge(@NonNull final NormalizedNode stateDataNode, @NonNull final NormalizedNode configDataNode) {
    final QNameModule moduleOfStateData = stateDataNode.getIdentifier().getNodeType().getModule();
    final QNameModule moduleOfConfigData = configDataNode.getIdentifier().getNodeType().getModule();
    if (!moduleOfStateData.equals(moduleOfConfigData)) {
        throw new RestconfDocumentedException("Unable to merge data from different modules.");
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule)

Example 45 with RestconfDocumentedException

use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.

the class ReadDataTransactionUtil method prepareDataByParamWithDef.

private static NormalizedNode prepareDataByParamWithDef(final NormalizedNode result, final YangInstanceIdentifier path, final WithDefaultsParam withDefa, final EffectiveModelContext ctx) {
    boolean trim;
    switch(withDefa) {
        case TRIM:
            trim = true;
            break;
        case EXPLICIT:
            trim = false;
            break;
        default:
            throw new RestconfDocumentedException("Unsupported with-defaults value " + withDefa.paramValue());
    }
    final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
    final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.findChild(path).orElseThrow().getDataSchemaNode();
    if (result instanceof ContainerNode) {
        final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) baseSchemaNode);
        buildCont(builder, (ContainerNode) result, baseSchemaCtxTree, path, trim);
        return builder.build();
    } else {
        final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) baseSchemaNode);
        buildMapEntryBuilder(builder, (MapEntryNode) result, baseSchemaCtxTree, path, trim, ((ListSchemaNode) baseSchemaNode).getKeyDefinition());
        return builder.build();
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) DataSchemaContextTree(org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree)

Aggregations

RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)136 Test (org.junit.Test)65 InputStream (java.io.InputStream)30 DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)20 RestconfError (org.opendaylight.restconf.common.errors.RestconfError)20 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)19 QName (org.opendaylight.yangtools.yang.common.QName)17 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)17 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)16 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)14 List (java.util.List)13 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)13 ArrayList (java.util.ArrayList)12 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)11 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)9 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)9 IOException (java.io.IOException)8 ExecutionException (java.util.concurrent.ExecutionException)8 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)8 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)8