*/
package org.opendaylight.restconf.common.context;
+import static java.util.Objects.requireNonNull;
+
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.dom.api.DOMMountPoint;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.ContainerLike;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
public final class InstanceIdentifierContext {
private final DOMMountPoint mountPoint;
private final EffectiveModelContext schemaContext;
+ private InstanceIdentifierContext(final EffectiveModelContext context, final DOMMountPoint mountPoint) {
+ instanceIdentifier = YangInstanceIdentifier.empty();
+ schemaContext = requireNonNull(context);
+ schemaNode = schemaContext;
+ this.mountPoint = mountPoint;
+ }
+
+ private InstanceIdentifierContext(final EffectiveModelContext context, final RpcDefinition rpc,
+ final DOMMountPoint mountPoint) {
+ instanceIdentifier = null;
+ schemaContext = requireNonNull(context);
+ schemaNode = requireNonNull(rpc);
+ this.mountPoint = mountPoint;
+ }
+
+ private InstanceIdentifierContext(final EffectiveModelContext context, final ContainerLike rpcInputOutput,
+ final DOMMountPoint mountPoint) {
+ instanceIdentifier = null;
+ schemaContext = requireNonNull(context);
+ schemaNode = requireNonNull(rpcInputOutput);
+ this.mountPoint = mountPoint;
+ }
+
+ @Deprecated(forRemoval = true)
public InstanceIdentifierContext(final YangInstanceIdentifier instanceIdentifier, final SchemaNode schemaNode,
final DOMMountPoint mountPoint, final EffectiveModelContext context) {
this.instanceIdentifier = instanceIdentifier;
schemaContext = context;
}
+ public static @NonNull InstanceIdentifierContext ofLocalRoot(final EffectiveModelContext context) {
+ return new InstanceIdentifierContext(context, null);
+ }
+
+ public static @NonNull InstanceIdentifierContext ofLocalRpcInput(final EffectiveModelContext context,
+ // FIXME: this this method really needed?
+ final RpcDefinition rpc) {
+ return new InstanceIdentifierContext(context, rpc.getInput(), null);
+ }
+
+ public static @NonNull InstanceIdentifierContext ofLocalRpcOutput(final EffectiveModelContext context,
+ // FIXME: we want to re-validate this, so might as well take a QName
+ final RpcDefinition rpc) {
+ return new InstanceIdentifierContext(context, rpc, null);
+ }
+
+ public static @NonNull InstanceIdentifierContext ofMountPointRoot(final DOMMountPoint mountPoint,
+ final EffectiveModelContext mountContext) {
+ return new InstanceIdentifierContext(mountContext, requireNonNull(mountPoint));
+ }
+
+ public static @NonNull InstanceIdentifierContext ofMountPointRpcOutput(final DOMMountPoint mountPoint,
+ final EffectiveModelContext mountContext, final RpcDefinition rpc) {
+ return new InstanceIdentifierContext(mountContext, rpc, requireNonNull(mountPoint));
+ }
+
public YangInstanceIdentifier getInstanceIdentifier() {
return instanceIdentifier;
}
checkPreconditions();
if (restconfInstance == null) {
- return new InstanceIdentifierContext(YangInstanceIdentifier.empty(), globalSchema, null, globalSchema);
+ return InstanceIdentifierContext.ofLocalRoot(globalSchema);
}
final List<String> pathArgs = urlPathArgsDecode(SLASH_SPLITTER.split(restconfInstance));
}
if (returnJustMountPoint || strings.size() == 1) {
- return new InstanceIdentifierContext(YangInstanceIdentifier.empty(), mountPointSchema, mount,
- mountPointSchema);
+ return InstanceIdentifierContext.ofMountPointRoot(mount, mountPointSchema);
}
final String moduleNameBehindMountPoint = toModuleName(strings.get(1));
if (resultData != null && ((ContainerNode) resultData).isEmpty()) {
throw new WebApplicationException(Response.Status.NO_CONTENT);
- } else {
- return new NormalizedNodeContext(
- new InstanceIdentifierContext(null, resultNodeSchema, mountPoint, schemaContext),
- resultData, QueryParametersParser.parseWriterParameters(uriInfo));
}
+
+ return new NormalizedNodeContext(
+ new InstanceIdentifierContext(null, resultNodeSchema, mountPoint, schemaContext),
+ resultData, QueryParametersParser.parseWriterParameters(uriInfo));
}
@SuppressFBWarnings(value = "NP_LOAD_OF_KNOWN_NULL_VALUE",
//
// This is legacy code, so if anybody cares to do that refactor, feel free to contribute, but I am not
// doing that work.
- return new NormalizedNodeContext(new InstanceIdentifierContext(null, rpc, mountPoint, schemaContext), result,
- QueryParametersParser.parseWriterParameters(uriInfo));
+ final var iic = mountPoint == null ? InstanceIdentifierContext.ofLocalRpcOutput(schemaContext, rpc)
+ : InstanceIdentifierContext.ofMountPointRpcOutput(mountPoint, schemaContext, rpc);
+ return new NormalizedNodeContext(iic, result, QueryParametersParser.parseWriterParameters(uriInfo));
}
private static @NonNull NormalizedNode nonnullInput(final SchemaNode rpc, final NormalizedNode input) {
dataContSchemaContNode.withChild(modul2.build());
final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext(
- new InstanceIdentifierContext(null, schemaContext, null, schemaContext),
+ InstanceIdentifierContext.ofLocalRoot(schemaContext),
dataContSchemaContNode.build());
return testNormalizedNodeContext;
final QName rpcQName = QName.create(rpcModule.getQNameModule(), "make-toast");
RpcDefinition rpcDef = null;
- ContainerLike rpcInputSchemaNode = null;
for (final RpcDefinition rpc : rpcModule.getRpcs()) {
if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
- rpcInputSchemaNode = rpc.getInput();
rpcDef = rpc;
break;
}
}
assertNotNull(rpcDef);
- assertNotNull(rpcInputSchemaNode);
- final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder =
- SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
- final NormalizedNodeContext payload =
- new NormalizedNodeContext(new InstanceIdentifierContext(null, rpcInputSchemaNode,
- null, schemaContext), containerBuilder.build());
+ final NormalizedNodeContext payload = new NormalizedNodeContext(
+ InstanceIdentifierContext.ofLocalRpcInput(schemaContext, rpcDef),
+ SchemaAwareBuilders.containerBuilder(rpcDef.getInput()).build());
doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(path), any(NormalizedNode.class));
private static InstanceIdentifierContext createIIdContext(final EffectiveModelContext schemaContext,
final String url, final @Nullable DOMMountPoint mountPoint) {
final YangInstanceIdentifier urlPath = IdentifierCodec.deserialize(url, schemaContext);
+ // First things first: an empty path means data invocation on SchemaContext
+ if (urlPath.isEmpty()) {
+ return mountPoint != null ? InstanceIdentifierContext.ofMountPointRoot(mountPoint, schemaContext)
+ : InstanceIdentifierContext.ofLocalRoot(schemaContext);
+ }
+
return new InstanceIdentifierContext(urlPath, getPathSchema(schemaContext, urlPath), mountPoint, schemaContext);
}
private static SchemaNode getPathSchema(final EffectiveModelContext schemaContext,
final YangInstanceIdentifier urlPath) {
- // First things first: an empty path means data invocation on SchemaContext
- if (urlPath.isEmpty()) {
- return schemaContext;
- }
-
// Peel the last component and locate the parent data node, empty path resolves to SchemaContext
final DataSchemaContextNode<?> parent = DataSchemaContextTree.from(schemaContext)
.findChild(verifyNotNull(urlPath.getParent()))
package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.ws.rs.core.MediaType;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class XmlNormalizedNodeBodyWriterTest {
@Test
- public void testWriteEmptyRootContainer() throws Exception {
+ public void testWriteEmptyRootContainer() throws IOException {
final EffectiveModelContext schemaContext = mock(EffectiveModelContext.class);
-
- final SchemaNode schemaNode = mock(SchemaNode.class);
- doReturn(SchemaPath.ROOT).when(schemaNode).getPath();
+ doCallRealMethod().when(schemaContext).getPath();
final NormalizedNodePayload nodePayload = NormalizedNodePayload.of(
- new InstanceIdentifierContext(YangInstanceIdentifier.empty(), schemaNode, null, schemaContext),
+ InstanceIdentifierContext.ofLocalRoot(schemaContext),
Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).build());
final ByteArrayOutputStream output = new ByteArrayOutputStream();
}
@Test
- public void testRootContainerWrite() throws Exception {
+ public void testRootContainerWrite() throws IOException {
final EffectiveModelContext schemaContext =
TestRestconfUtils.loadSchemaContext("/instanceidentifier/yang", null);
- final SchemaNode schemaNode = mock(SchemaNode.class);
- doReturn(SchemaPath.ROOT).when(schemaNode).getPath();
-
final NormalizedNodePayload nodePayload = NormalizedNodePayload.of(
- new InstanceIdentifierContext(YangInstanceIdentifier.empty(), schemaNode, null, schemaContext),
+ InstanceIdentifierContext.ofLocalRoot(schemaContext),
Builders.containerBuilder()
.withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
.withChild(Builders.containerBuilder()
private InstanceIdentifierContext identifierJukebox;
private InstanceIdentifierContext identifierTestServices;
+ // FIXME: remove all this mocking -- just parse the underlying model and be done with it
+
// container jukebox
@Mock
private ContainerSchemaNode containerJukebox;