import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.nb.rfc8040.Insert.PointNormalizer;
import org.opendaylight.restconf.server.api.DatabindContext;
+import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Action;
import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Data;
import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Rpc;
import org.opendaylight.yangtools.yang.common.ErrorTag;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContext.PathMixin;
import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
OutputEffectiveStatement outputStatement();
}
- private final @NonNull EffectiveModelContext modelContext;
private final @NonNull DatabindContext databind;
public ApiPathNormalizer(final DatabindContext databind) {
this.databind = requireNonNull(databind);
- modelContext = databind.modelContext();
}
public @NonNull Path normalizePath(final ApiPath apiPath) {
final var it = apiPath.steps().iterator();
if (!it.hasNext()) {
- return new Data(Inference.ofDataTreePath(modelContext), YangInstanceIdentifier.of(),
+ return new Data(Inference.ofDataTreePath(databind.modelContext()), YangInstanceIdentifier.of(),
databind.schemaTree().getRoot());
}
var qname = step.identifier().bindTo(namespace);
// We go through more modern APIs here to get this special out of the way quickly
+ final var modelContext = databind.modelContext();
final var optRpc = modelContext.findModuleStatement(namespace).orElseThrow()
.findSchemaTreeNode(RpcEffectiveStatement.class, qname);
if (optRpc.isPresent()) {
final var stack = SchemaInferenceStack.of(modelContext);
final var stmt = stack.enterSchemaTree(rpc.argument());
verify(rpc.equals(stmt), "Expecting %s, inferred %s", rpc, stmt);
- return new OperationPath.Rpc(stack.toInference(), rpc);
+ return new Rpc(stack.toInference(), rpc);
}
final var stack = SchemaInferenceStack.of(modelContext);
final var actionStmt = action.asEffectiveStatement();
verify(actionStmt.equals(stmt), "Expecting %s, inferred %s", actionStmt, stmt);
- return new OperationPath.Action(stack.toInference(), YangInstanceIdentifier.of(path), actionStmt);
+ return new Action(stack.toInference(), YangInstanceIdentifier.of(path), actionStmt);
}
}
if (step instanceof ListInstance listStep) {
final var values = listStep.keyValues();
final var schema = childNode.dataSchemaNode();
- pathArg = schema instanceof ListSchemaNode listSchema
- ? prepareNodeWithPredicates(stack, qname, listSchema, values)
- // FIXME: recognize only leaf-list here, which has only a single item
- : prepareNodeWithValue(stack, qname, schema, values);
+ if (schema instanceof ListSchemaNode listSchema) {
+ pathArg = prepareNodeWithPredicates(stack, qname, listSchema, values);
+ } else if (schema instanceof LeafListSchemaNode leafListSchema) {
+ if (values.size() != 1) {
+ throw new RestconfDocumentedException("Entry '" + qname + "' requires one value predicate.",
+ ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE);
+ }
+ pathArg = new NodeWithValue<>(qname, parserJsonValue(stack, leafListSchema, values.get(0)));
+ } else {
+ throw new RestconfDocumentedException(
+ "Entry '" + qname + "' does not take a key or value predicate.",
+ ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE);
+ }
} else {
if (childNode.dataSchemaNode() instanceof ListSchemaNode list && !list.getKeyDefinition().isEmpty()) {
throw new RestconfDocumentedException(
final var namespace = resolveNamespace(firstModule);
final var qname = step.identifier().bindTo(namespace);
- final var stack = SchemaInferenceStack.of(modelContext);
+ final var stack = SchemaInferenceStack.of(databind.modelContext());
final SchemaTreeEffectiveStatement<?> stmt;
try {
stmt = stack.enterSchemaTree(qname);
if (path instanceof Data dataPath) {
return dataPath;
}
- if (path instanceof OperationPath.Action actionPath) {
+ if (path instanceof Action actionPath) {
return actionPath;
}
throw new RestconfDocumentedException("Unexpected path " + path, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
}
}
- private NodeWithValue<?> prepareNodeWithValue(final SchemaInferenceStack stack, final QName qname,
- final DataSchemaNode schema, final List<String> keyValues) {
- // TODO: qname should be always equal to schema.getQName(), right?
- return new NodeWithValue<>(qname, prepareValueByType(stack, schema,
- // FIXME: ahem: we probably want to do something differently here
- keyValues.get(0)));
- }
-
private @NonNull QNameModule resolveNamespace(final String moduleName) {
- final var it = modelContext.findModuleStatements(moduleName).iterator();
+ final var it = databind.modelContext().findModuleStatements(moduleName).iterator();
if (it.hasNext()) {
return it.next().localQNameModule();
}