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" + "<bad-rpc/>\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());
}
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));
}
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);
}
}
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()));
}
}
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));
}
Aggregations