Search in sources :

Example 11 with NetconfMessage

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

the class NetconfServerSessionListenerTest method testOnMessageDocumentedFail.

@SuppressWarnings("checkstyle:RegexpSinglelineJava")
@Test
public void testOnMessageDocumentedFail() throws Exception {
    final Document reply = XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" + "<rpc-error>\n" + "<error-type>protocol</error-type>\n" + "<error-tag>unknown-element</error-tag>\n" + "<error-severity>error</error-severity>\n" + "<error-message>Unknown tag bad-rpc in message:\n" + "&lt;bad-rpc/&gt;\n" + "</error-message>\n" + "<error-info>\n" + "<bad-element>bad-rpc</bad-element>\n" + "</error-info>\n" + "</rpc-error>\n" + "</rpc-reply>");
    final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<bad-rpc/>"));
    listener.onMessage(session, msg);
    verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
    verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.OUT_RPC_ERROR)));
    channel.runPendingTasks();
    final NetconfMessage sentMsg = channel.readOutbound();
    System.out.println(XmlUtil.toString(sentMsg.getDocument()));
    System.out.println(XmlUtil.toString(reply));
    final Diff diff = XMLUnit.compareXML(reply, sentMsg.getDocument());
    assertTrue(diff.toString(), diff.similar());
}
Also used : Diff(org.custommonkey.xmlunit.Diff) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 12 with NetconfMessage

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

the class DefaultCloseSessionTest method testDefaultCloseSession.

@Test
public void testDefaultCloseSession() throws Exception {
    AutoCloseable res = mock(AutoCloseable.class);
    doNothing().when(res).close();
    DefaultCloseSession close = new DefaultCloseSession("", res);
    final Document doc = XmlUtil.newDocument();
    final XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("<elem/>"));
    final Channel channel = mock(Channel.class);
    doReturn("channel").when(channel).toString();
    mockEventLoop(channel);
    final ChannelFuture channelFuture = mock(ChannelFuture.class);
    doReturn(channelFuture).when(channel).close();
    doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
    final ChannelPromise sendFuture = mock(ChannelPromise.class);
    doAnswer(invocation -> {
        invocation.<GenericFutureListener>getArgument(0).operationComplete(sendFuture);
        return null;
    }).when(sendFuture).addListener(any(GenericFutureListener.class));
    doReturn(sendFuture).when(channel).newPromise();
    doReturn(sendFuture).when(channel).writeAndFlush(any(), any());
    doReturn(true).when(sendFuture).isSuccess();
    final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
    doNothing().when(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
    final NetconfServerSession session = new NetconfServerSession(listener, channel, 1L, NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
    close.setNetconfSession(session);
    close.handleWithNoSubsequentOperations(doc, elem);
    // Fake close response to trigger delayed close
    session.sendMessage(new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" + "<ok/>\n" + "</rpc-reply>")));
    verify(channel).close();
    verify(listener).onSessionTerminated(any(NetconfServerSession.class), any(NetconfTerminationReason.class));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NetconfTerminationReason(org.opendaylight.netconf.api.NetconfTerminationReason) NetconfServerSessionListener(org.opendaylight.netconf.impl.NetconfServerSessionListener) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Channel(io.netty.channel.Channel) NetconfServerSession(org.opendaylight.netconf.impl.NetconfServerSession) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) ChannelPromise(io.netty.channel.ChannelPromise) Document(org.w3c.dom.Document) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Test(org.junit.Test)

Example 13 with NetconfMessage

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

the class NetconfServerSessionListener method onMessage.

@SuppressWarnings("checkstyle:IllegalCatch")
@Override
public void onMessage(final NetconfServerSession session, final NetconfMessage netconfMessage) {
    try {
        Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
        // there is no validation since the document may contain yang schemas
        final NetconfMessage message = processDocument(netconfMessage, session);
        LOG.debug("Responding with message {}", message);
        session.sendMessage(message);
        monitoringSessionListener.onSessionEvent(SessionEvent.inRpcSuccess(session));
    } catch (final RuntimeException e) {
        // TODO: should send generic error or close session?
        LOG.error("Unexpected exception", e);
        session.onIncommingRpcFail();
        monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
        throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
    } catch (final DocumentedException e) {
        LOG.trace("Error occurred while processing message", e);
        session.onOutgoingRpcError();
        session.onIncommingRpcFail();
        monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
        monitoringSessionListener.onSessionEvent(SessionEvent.outRpcError(session));
        SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
    }
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) DocumentedException(org.opendaylight.netconf.api.DocumentedException)

Example 14 with NetconfMessage

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

the class NetconfServerSessionListener method processDocument.

private NetconfMessage processDocument(final NetconfMessage netconfMessage, final NetconfServerSession session) throws DocumentedException {
    final Document incomingDocument = netconfMessage.getDocument();
    final Node rootNode = incomingDocument.getDocumentElement();
    if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
        final Document responseDocument = XmlUtil.newDocument();
        checkMessageId(rootNode);
        Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
        rpcReply = SubtreeFilter.applyRpcSubtreeFilter(incomingDocument, rpcReply);
        session.onIncommingRpcSuccess();
        responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
        return new NetconfMessage(responseDocument);
    } else {
        /*
             * Tag: unknown-element Error-type: rpc, protocol, application
             * Severity: error Error-info: <bad-element> : name of the
             * unexpected element Description: An unexpected element is present.
             */
        throw new DocumentedException("Unknown tag " + rootNode.getNodeName() + " in message:\n" + netconfMessage, ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT, ErrorSeverity.ERROR, ImmutableMap.of("bad-element", rootNode.getNodeName()));
    }
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) DocumentedException(org.opendaylight.netconf.api.DocumentedException) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 15 with NetconfMessage

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

the class SendErrorExceptionUtil method sendErrorMessage.

public static void sendErrorMessage(final NetconfSession session, final DocumentedException sendErrorException, final NetconfMessage incommingMessage) {
    final Document errorDocument = createDocument(sendErrorException);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Sending error {}", XmlUtil.toString(errorDocument));
    }
    tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException);
    ChannelFuture channelFuture = session.sendMessage(new NetconfMessage(errorDocument));
    channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Document(org.w3c.dom.Document)

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