use of org.opendaylight.mdsal.dom.api.DOMRpcService in project mdsal by opendaylight.
the class DOMMountPointServiceImplTest method testMountPointRegistration.
@Test
public void testMountPointRegistration() {
final DOMMountPointListener mountPointListener = mock(DOMMountPointListener.class);
doNothing().when(mountPointListener).onMountPointCreated(PATH);
mountPointService.registerProvisionListener(mountPointListener);
// Create a mount point with schema context and a DOMService
final DOMMountPointBuilder mountPointBuilder = mountPointService.createMountPoint(PATH);
final DOMRpcService rpcService = mock(DOMRpcService.class);
mountPointBuilder.addService(DOMRpcService.class, rpcService);
mountPointBuilder.register();
// Verify listener has been notified and mount point is accessible from mount point service
verify(mountPointListener).onMountPointCreated(eq(PATH));
assertTrue(mountPointService.getMountPoint(PATH).isPresent());
// Verify mount point schema context and service
final DOMMountPoint mountPoint = mountPointService.getMountPoint(PATH).get();
assertTrue(mountPoint.getService(DOMRpcService.class).isPresent());
assertEquals(rpcService, mountPoint.getService(DOMRpcService.class).get());
}
use of org.opendaylight.mdsal.dom.api.DOMRpcService in project netconf by opendaylight.
the class RestconfImplTest method testRpcForMountpoint.
@Test
public void testRpcForMountpoint() throws Exception {
final UriInfo uriInfo = mock(UriInfo.class);
doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
final InstanceIdentifierContext<?> iiCtx = mock(InstanceIdentifierContext.class);
doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
final SchemaNode schemaNode = mock(SchemaNode.class);
doReturn(schemaNode).when(iiCtx).getSchemaNode();
doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
final DOMMountPoint mount = mock(DOMMountPoint.class);
doReturn(mount).when(iiCtx).getMountPoint();
final DOMRpcService rpcService = mock(DOMRpcService.class);
doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
doReturn(immediateFluentFuture(mock(DOMRpcResult.class))).when(rpcService).invokeRpc(any(QName.class), any(NormalizedNode.class));
this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
verify(rpcService, times(2)).invokeRpc(any(QName.class), any());
}
use of org.opendaylight.mdsal.dom.api.DOMRpcService in project netconf by opendaylight.
the class RestconfImpl method invokeRpc.
@SuppressFBWarnings(value = "NP_LOAD_OF_KNOWN_NULL_VALUE", justification = "Looks like a false positive, see below FIXME")
private NormalizedNodeContext invokeRpc(final String identifier, final UriInfo uriInfo) {
final DOMMountPoint mountPoint;
final String identifierEncoded;
final EffectiveModelContext schemaContext;
if (identifier.contains(ControllerContext.MOUNT)) {
// mounted RPC call - look up mount instance.
final InstanceIdentifierContext<?> mountPointId = controllerContext.toMountPointIdentifier(identifier);
mountPoint = mountPointId.getMountPoint();
schemaContext = modelContext(mountPoint);
final int startOfRemoteRpcName = identifier.lastIndexOf(ControllerContext.MOUNT) + ControllerContext.MOUNT.length() + 1;
final String remoteRpcName = identifier.substring(startOfRemoteRpcName);
identifierEncoded = remoteRpcName;
} else if (identifier.indexOf('/') == CHAR_NOT_FOUND) {
identifierEncoded = identifier;
mountPoint = null;
schemaContext = controllerContext.getGlobalSchema();
} else {
LOG.debug("Identifier {} cannot contain slash character (/).", identifier);
throw new RestconfDocumentedException(String.format("Identifier %n%s%ncan\'t contain slash character (/).%n" + "If slash is part of identifier name then use %%2F placeholder.", identifier), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
final String identifierDecoded = ControllerContext.urlPathArgDecode(identifierEncoded);
final RpcDefinition rpc;
if (mountPoint == null) {
rpc = controllerContext.getRpcDefinition(identifierDecoded);
} else {
rpc = findRpc(modelContext(mountPoint), identifierDecoded);
}
if (rpc == null) {
LOG.debug("RPC {} does not exist.", identifierDecoded);
throw new RestconfDocumentedException("RPC does not exist.", ErrorType.RPC, ErrorTag.UNKNOWN_ELEMENT);
}
if (!rpc.getInput().getChildNodes().isEmpty()) {
LOG.debug("No input specified for RPC {} with an input section", rpc);
throw new RestconfDocumentedException("No input specified for RPC " + rpc + " with an input section defined", ErrorType.RPC, ErrorTag.MISSING_ELEMENT);
}
final ContainerNode input = defaultInput(rpc.getQName());
final ListenableFuture<? extends DOMRpcResult> response;
if (mountPoint != null) {
final Optional<DOMRpcService> mountRpcServices = mountPoint.getService(DOMRpcService.class);
if (mountRpcServices.isEmpty()) {
throw new RestconfDocumentedException("Rpc service is missing.");
}
response = mountRpcServices.get().invokeRpc(rpc.getQName(), input);
} else {
response = broker.invokeRpc(rpc.getQName(), input);
}
final NormalizedNode result = checkRpcResponse(response).getResult();
if (result != null && ((ContainerNode) result).isEmpty()) {
throw new WebApplicationException(Response.Status.NO_CONTENT);
}
// doing that work.
return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpc, mountPoint, schemaContext), result, QueryParametersParser.parseWriterParameters(uriInfo));
}
use of org.opendaylight.mdsal.dom.api.DOMRpcService in project netconf by opendaylight.
the class RestconfImpl method invokeRpc.
@Override
public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
if (payload == null) {
// no payload specified, reroute this to no payload invokeRpc implementation
return invokeRpc(identifier, uriInfo);
}
final SchemaNode schema = payload.getInstanceIdentifierContext().getSchemaNode();
final ListenableFuture<? extends DOMRpcResult> response;
final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
final NormalizedNode input = nonnullInput(schema, payload.getData());
final EffectiveModelContext schemaContext;
if (mountPoint != null) {
final Optional<DOMRpcService> mountRpcServices = mountPoint.getService(DOMRpcService.class);
if (mountRpcServices.isEmpty()) {
LOG.debug("Error: Rpc service is missing.");
throw new RestconfDocumentedException("Rpc service is missing.");
}
schemaContext = modelContext(mountPoint);
response = mountRpcServices.get().invokeRpc(schema.getQName(), input);
} else {
final XMLNamespace namespace = schema.getQName().getNamespace();
if (namespace.toString().equals(SAL_REMOTE_NAMESPACE)) {
if (identifier.contains(CREATE_DATA_SUBSCR)) {
response = invokeSalRemoteRpcSubscribeRPC(payload);
} else if (identifier.contains(CREATE_NOTIFICATION_STREAM)) {
response = invokeSalRemoteRpcNotifiStrRPC(payload);
} else {
final String msg = "Not supported operation";
LOG.warn(msg);
throw new RestconfDocumentedException(msg, ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED);
}
} else {
response = broker.invokeRpc(schema.getQName(), input);
}
schemaContext = controllerContext.getGlobalSchema();
}
final DOMRpcResult result = checkRpcResponse(response);
RpcDefinition resultNodeSchema = null;
final NormalizedNode resultData;
if (result != null && result.getResult() != null) {
resultData = result.getResult();
resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
} else {
resultData = null;
}
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));
}
}
use of org.opendaylight.mdsal.dom.api.DOMRpcService in project netconf by opendaylight.
the class MountedDeviceListener method trackNotificationsPerformance.
private void trackNotificationsPerformance(final YangInstanceIdentifier path) {
// 1. get nodeId from the path
final String nodeId = TestUtils.getNodeId(path).get();
// 2. extract needed services from the mount point
final DOMMountPoint mountPoint = mountPointService.getMountPoint(path).orElseThrow(() -> new RuntimeException("Unable to get mountpoint"));
final DOMRpcService rpcService = mountPoint.getService(DOMRpcService.class).orElseThrow(() -> new RuntimeException("Unable to get RPC Service from the mountpoint"));
final DOMNotificationService notificationService = mountPoint.getService(DOMNotificationService.class).orElseThrow(() -> new RuntimeException("Unable to get NotificationService from the mountpoint"));
// 3. create a listener for the notifications
listeners.put(path, notificationService.registerNotificationListener(new NotificationsCounter(nodeId, serializer), Absolute.of(VrfRouteNotification.QNAME)));
// 4. send 'create-subscription' request to the device
final StreamNameType streamNameType = new StreamNameType(STREAM_DEFAULT_NAME);
final CreateSubscriptionInputBuilder subscriptionInputBuilder = new CreateSubscriptionInputBuilder();
subscriptionInputBuilder.setStream(streamNameType);
final CreateSubscriptionInput input = subscriptionInputBuilder.build();
final ContainerNode inputNode = serializer.toNormalizedNodeRpcData(input);
final ListenableFuture<? extends DOMRpcResult> resultFuture = rpcService.invokeRpc(CREATE_SUBSCRIPTION_QNAME, inputNode);
Futures.addCallback(resultFuture, new FutureCallback<DOMRpcResult>() {
@Override
public void onSuccess(@Nullable final DOMRpcResult rpcResult) {
LOG.info("Notification stream subscription succesfully completed");
}
@Override
public void onFailure(final Throwable throwable) {
LOG.error("Notification stream subscription failed");
}
}, MoreExecutors.directExecutor());
}
Aggregations