Search in sources :

Example 61 with NetconfMessage

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");
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) QName(org.opendaylight.yangtools.yang.common.QName) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) Test(org.junit.Test)

Example 62 with NetconfMessage

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
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) QName(org.opendaylight.yangtools.yang.common.QName) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 63 with NetconfMessage

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() );
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage)

Example 64 with NetconfMessage

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);
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) QName(org.opendaylight.yangtools.yang.common.QName) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 65 with NetconfMessage

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());
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) InputSchemaNode(org.opendaylight.yangtools.yang.model.api.InputSchemaNode) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) IOException(java.io.IOException) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition)

Aggregations

NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)125 Test (org.junit.Test)72 AbstractBaseSchemasTest (org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest)40 Document (org.w3c.dom.Document)28 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)23 QName (org.opendaylight.yangtools.yang.common.QName)17 DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)15 Test (org.junit.jupiter.api.Test)13 SimpleNetconfClientSessionListener (org.opendaylight.netconf.client.SimpleNetconfClientSessionListener)13 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)13 Node (org.w3c.dom.Node)13 NetconfClientSession (org.opendaylight.netconf.client.NetconfClientSession)12 ArrayList (java.util.ArrayList)11 Element (org.w3c.dom.Element)11 DOMRpcResult (org.opendaylight.mdsal.dom.api.DOMRpcResult)10 UnkeyedListNode (org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode)10 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)9 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)9 ChannelFuture (io.netty.channel.ChannelFuture)8 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)8