use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class NetconfDeviceCommunicatorTest method testSendRequestWithNoSession.
@Test
public void testSendRequestWithNoSession() throws Exception {
NetconfMessage message = new NetconfMessage(UntrustedXML.newDocumentBuilder().newDocument());
QName rpc = QName.create("", "mockRpc");
ListenableFuture<RpcResult<NetconfMessage>> resultFuture = communicator.sendRequest(message, rpc);
assertNotNull("ListenableFuture is null", resultFuture);
// Should have an immediate result
RpcResult<NetconfMessage> rpcResult = resultFuture.get(3, TimeUnit.MILLISECONDS);
verifyErrorRpcResult(rpcResult, RpcError.ErrorType.TRANSPORT, "operation-failed");
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class NetconfDeviceCommunicatorTest method testSendRequest.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSendRequest() throws Exception {
setupSession();
NetconfMessage message = new NetconfMessage(UntrustedXML.newDocumentBuilder().newDocument());
QName rpc = QName.create("", "mockRpc");
ArgumentCaptor<GenericFutureListener> futureListener = ArgumentCaptor.forClass(GenericFutureListener.class);
ChannelFuture mockChannelFuture = mock(ChannelFuture.class);
doReturn(mockChannelFuture).when(mockChannelFuture).addListener(futureListener.capture());
doReturn(mockChannelFuture).when(mockSession).sendMessage(same(message));
ListenableFuture<RpcResult<NetconfMessage>> resultFuture = communicator.sendRequest(message, rpc);
verify(mockSession).sendMessage(same(message));
assertNotNull("ListenableFuture is null", resultFuture);
verify(mockChannelFuture).addListener(futureListener.capture());
Future<Void> operationFuture = mock(Future.class);
doReturn(true).when(operationFuture).isSuccess();
futureListener.getValue().operationComplete(operationFuture);
try {
// verify it's not cancelled or has an error set
resultFuture.get(1, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
LOG.info("Operation failed due timeout.");
}
// expected
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class NetconfDeviceCommunicatorTest method verifyResponseMessage.
private static void verifyResponseMessage(final RpcResult<NetconfMessage> rpcResult, final String dataText) {
assertNotNull("RpcResult is null", rpcResult);
assertTrue("isSuccessful", rpcResult.isSuccessful());
NetconfMessage messageResult = rpcResult.getResult();
assertNotNull("getResult", messageResult);
// List<SimpleNode<?>> nodes = messageResult.getSimpleNodesByName(
// QName.create( URI.create( "ns" ), null, "data" ) );
// assertNotNull( "getSimpleNodesByName", nodes );
// assertEquals( "List<SimpleNode<?>> size", 1, nodes.size() );
// assertEquals( "SimpleNode value", dataText, nodes.iterator().next().getValue() );
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class NetconfMessageTransformer method toRpcRequest.
@Override
public NetconfMessage toRpcRequest(final QName rpc, final NormalizedNode payload) {
// In case no input for rpc is defined, we can simply construct the payload here
// Determine whether a base netconf operation is being invoked
// and also check if the device exposed model for base netconf.
// If no, use pre built base netconf operations model
final boolean needToUseBaseCtx = mappedRpcs.get(rpc) == null && isBaseOrNotificationRpc(rpc);
final ImmutableMap<QName, ? extends RpcDefinition> currentMappedRpcs;
if (needToUseBaseCtx) {
currentMappedRpcs = baseSchema.getMappedRpcs();
} else {
currentMappedRpcs = mappedRpcs;
}
final RpcDefinition mappedRpc = Preconditions.checkNotNull(currentMappedRpcs.get(rpc), "Unknown rpc %s, available rpcs: %s", rpc, currentMappedRpcs.keySet());
if (mappedRpc.getInput().getChildNodes().isEmpty()) {
return new NetconfMessage(NetconfMessageTransformUtil.prepareDomResultForRpcRequest(rpc, counter).getNode().getOwnerDocument());
}
Preconditions.checkNotNull(payload, "Transforming an rpc with input: %s, payload cannot be null", rpc);
Preconditions.checkArgument(payload instanceof ContainerNode, "Transforming an rpc with input: %s, payload has to be a container, but was: %s", rpc, payload);
final DOMResult result = NetconfMessageTransformUtil.prepareDomResultForRpcRequest(rpc, counter);
try {
// If the schema context for netconf device does not contain model for base netconf operations,
// use default pre build context with just the base model
// This way operations like lock/unlock are supported even if the source for base model was not provided
final EffectiveModelContext ctx = needToUseBaseCtx ? baseSchema.getEffectiveModelContext() : mountContext.getEffectiveModelContext();
NetconfMessageTransformUtil.writeNormalizedOperationInput((ContainerNode) payload, result, Absolute.of(rpc), ctx);
} catch (final XMLStreamException | IOException | IllegalStateException e) {
throw new IllegalStateException("Unable to serialize input of " + rpc, e);
}
final Document node = result.getNode().getOwnerDocument();
return new NetconfMessage(node);
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class NetconfMessageTransformer method toActionRequest.
@Override
public NetconfMessage toActionRequest(final Absolute action, final DOMDataTreeIdentifier domDataTreeIdentifier, final NormalizedNode payload) {
final ActionDefinition actionDef = actions.get(action);
Preconditions.checkArgument(actionDef != null, "Action does not exist: %s", action);
final InputSchemaNode inputDef = actionDef.getInput();
if (inputDef.getChildNodes().isEmpty()) {
return new NetconfMessage(NetconfMessageTransformUtil.prepareDomResultForActionRequest(contextTree, domDataTreeIdentifier, counter, actionDef.getQName()).getNode().getOwnerDocument());
}
Preconditions.checkNotNull(payload, "Transforming an action with input: %s, payload cannot be null", action);
Preconditions.checkArgument(payload instanceof ContainerNode, "Transforming an action with input: %s, payload has to be a container, but was: %s", action, payload);
final DOMResult result = NetconfMessageTransformUtil.prepareDomResultForActionRequest(contextTree, domDataTreeIdentifier, counter, actionDef.getQName());
try {
NetconfMessageTransformUtil.writeNormalizedOperationInput((ContainerNode) payload, result, action, mountContext.getEffectiveModelContext());
} catch (final XMLStreamException | IOException | IllegalStateException e) {
throw new IllegalStateException("Unable to serialize input of " + action, e);
}
return new NetconfMessage(result.getNode().getOwnerDocument());
}
Aggregations