Search in sources :

Example 21 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class NetconfDeviceRpc method invokeRpc.

@Override
@SuppressWarnings("checkstyle:IllegalCatch")
public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
    final ListenableFuture<RpcResult<NetconfMessage>> delegateFuture = communicator.sendRequest(transformer.toRpcRequest(type, input), type);
    final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
    Futures.addCallback(delegateFuture, new FutureCallback<RpcResult<NetconfMessage>>() {

        @Override
        public void onSuccess(final RpcResult<NetconfMessage> result) {
            try {
                ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type) : new DefaultDOMRpcResult(result.getErrors()));
            } catch (Exception cause) {
                ret.setException(new DefaultDOMRpcException("Unable to parse rpc reply. type: " + type + " input: " + input, cause));
            }
        }

        @Override
        public void onFailure(final Throwable cause) {
            ret.setException(new DOMRpcImplementationNotAvailableException(cause, "Unable to invoke rpc %s", type));
        }
    }, MoreExecutors.directExecutor());
    return ret;
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) DOMRpcImplementationNotAvailableException(org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) DOMRpcImplementationNotAvailableException(org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException) DefaultDOMRpcException(org.opendaylight.mdsal.dom.api.DefaultDOMRpcException) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) DefaultDOMRpcException(org.opendaylight.mdsal.dom.api.DefaultDOMRpcException)

Example 22 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class NotificationHandler method onRemoteSchemaUp.

/**
 * Forward all cached notifications and pass all notifications from this point directly to sal facade.
 * @param transformer Message transformer
 */
synchronized void onRemoteSchemaUp(final MessageTransformer<NetconfMessage> transformer) {
    this.messageTransformer = requireNonNull(transformer);
    passNotifications = true;
    for (final NetconfMessage cachedNotification : queue) {
        passNotification(transformNotification(cachedNotification));
    }
    queue.clear();
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage)

Example 23 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class NetconfMessageUtilTest method testNetconfMessageUtil.

@Test
public void testNetconfMessageUtil() throws Exception {
    final NetconfMessage okMessage = new NetconfMessage(XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml"));
    assertTrue(NetconfMessageUtil.isOKMessage(okMessage));
    assertFalse(NetconfMessageUtil.isErrorMessage(okMessage));
    final NetconfMessage errorMessage = new NetconfMessage(XmlFileLoader.xmlFileToDocument("netconfMessages/communicationError/testClientSendsRpcReply_expectedResponse.xml"));
    assertTrue(NetconfMessageUtil.isErrorMessage(errorMessage));
    assertFalse(NetconfMessageUtil.isOKMessage(errorMessage));
    final Collection<String> caps = NetconfMessageUtil.extractCapabilitiesFromHello(XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml"));
    assertTrue(caps.contains("urn:ietf:params:netconf:base:1.0"));
    assertTrue(caps.contains("urn:ietf:params:netconf:base:1.1"));
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Test(org.junit.Test)

Example 24 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class CallHomeMountSessionContextTest method activationOfListenerSupportsSessionMessages.

@Test
public void activationOfListenerSupportsSessionMessages() {
    // given
    when(mockActivator.activate(any(NetconfClientSessionListener.class))).thenAnswer(invocationOnMock -> {
        NetconfClientSession mockSession = mock(NetconfClientSession.class);
        NetconfMessage mockMsg = mock(NetconfMessage.class);
        Object arg = invocationOnMock.getArguments()[0];
        ((NetconfClientSessionListener) arg).onMessage(mockSession, mockMsg);
        return null;
    });
    // given
    NetconfClientSessionListener mockListener = mock(NetconfClientSessionListener.class);
    // when
    mockActivator.activate(mockListener);
    // then
    verify(mockListener, times(1)).onMessage(any(NetconfClientSession.class), any(NetconfMessage.class));
}
Also used : NetconfClientSessionListener(org.opendaylight.netconf.client.NetconfClientSessionListener) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) NetconfClientSession(org.opendaylight.netconf.client.NetconfClientSession) Test(org.junit.Test)

Example 25 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class AsyncExecutionStrategy method invoke.

@Override
public void invoke() {
    final AtomicInteger responseCounter = new AtomicInteger(0);
    final List<ListenableFuture<RpcResult<NetconfMessage>>> futures = new ArrayList<>();
    int batchI = 0;
    for (final Integer editBatch : getEditBatches()) {
        for (int i = 0; i < editBatch; i++) {
            final int msgId = i + batchI * getParams().editBatchSize;
            final NetconfMessage msg = getPreparedMessages().get(msgId);
            LOG.debug("Sending message {}", msgId);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Sending message {}", XmlUtil.toString(msg.getDocument()));
            }
            final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture = getSessionListener().sendRequest(msg, StressClient.EDIT_QNAME);
            futures.add(netconfMessageFuture);
        }
        batchI++;
        LOG.info("Batch {} with size {} sent. Committing", batchI, editBatch);
        if (getParams().candidateDatastore) {
            futures.add(getSessionListener().sendRequest(StressClient.COMMIT_MSG, StressClient.COMMIT_QNAME));
        }
    }
    LOG.info("All batches sent. Waiting for responses");
    // Wait for every future
    for (final ListenableFuture<RpcResult<NetconfMessage>> future : futures) {
        try {
            final RpcResult<NetconfMessage> netconfMessageRpcResult = future.get(getParams().msgTimeout, TimeUnit.SECONDS);
            if (netconfMessageRpcResult.isSuccessful()) {
                responseCounter.incrementAndGet();
                LOG.debug("Received response {}", responseCounter.get());
            } else {
                LOG.warn("Request failed {}", netconfMessageRpcResult);
            }
        } catch (final InterruptedException e) {
            throw new RuntimeException(e);
        } catch (final ExecutionException | TimeoutException e) {
            throw new RuntimeException("Request not finished", e);
        }
    }
    Preconditions.checkState(responseCounter.get() == getEditAmount() + (getParams().candidateDatastore ? getEditBatches().size() : 0), "Not all responses were received, only %s from %s", responseCounter.get(), getParams().editCount + getEditBatches().size());
}
Also used : ArrayList(java.util.ArrayList) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

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