use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class SendErrorExceptionUtil method sendErrorMessage.
public static void sendErrorMessage(final Channel channel, final DocumentedException sendErrorException) {
LOG.trace("Sending error", sendErrorException);
final Document errorDocument = createDocument(sendErrorException);
ChannelFuture channelFuture = channel.writeAndFlush(new NetconfMessage(errorDocument));
channelFuture.addListener(new SendErrorVerifyingListener(sendErrorException));
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class MessageParserTest method testEOMFramingMechanismOnPipeline.
@Test
public void testEOMFramingMechanismOnPipeline() throws Exception {
EmbeddedChannel testChunkChannel = new EmbeddedChannel(FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM), new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
testChunkChannel.writeOutbound(this.msg);
ByteBuf recievedOutbound = testChunkChannel.readOutbound();
byte[] endOfMsgBytes = NetconfMessageConstants.END_OF_MESSAGE.getBytes(StandardCharsets.UTF_8);
byte[] eom = new byte[endOfMsgBytes.length];
recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfMsgBytes.length, eom);
assertArrayEquals(endOfMsgBytes, eom);
testChunkChannel.writeInbound(recievedOutbound);
NetconfMessage receivedMessage = testChunkChannel.readInbound();
assertNotNull(receivedMessage);
XMLUnit.setIgnoreWhitespace(true);
assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class MessageParserTest method testChunkedFramingMechanismOnPipeline.
@Test
public void testChunkedFramingMechanismOnPipeline() throws Exception {
EmbeddedChannel testChunkChannel = new EmbeddedChannel(FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK), new NetconfMessageToXMLEncoder(), new NetconfChunkAggregator(), new NetconfXMLToMessageDecoder());
testChunkChannel.writeOutbound(this.msg);
Queue<Object> messages = testChunkChannel.outboundMessages();
assertFalse(messages.isEmpty());
final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
final ByteBuf out = Unpooled.buffer();
enc.encode(null, msg, out);
int msgLength = out.readableBytes();
int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
if (msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE != 0) {
chunkCount++;
}
byte[] endOfChunkBytes = NetconfMessageConstants.END_OF_CHUNK.getBytes(StandardCharsets.UTF_8);
for (int i = 1; i <= chunkCount; i++) {
ByteBuf recievedOutbound = (ByteBuf) messages.poll();
int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
if (i == chunkCount) {
exptHeaderLength = msgLength - ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1);
byte[] eom = new byte[endOfChunkBytes.length];
recievedOutbound.getBytes(recievedOutbound.readableBytes() - endOfChunkBytes.length, eom);
assertArrayEquals(endOfChunkBytes, eom);
}
byte[] header = new byte[String.valueOf(exptHeaderLength).length() + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
recievedOutbound.getBytes(0, header);
assertEquals(exptHeaderLength, getHeaderLength(header));
testChunkChannel.writeInbound(recievedOutbound);
}
assertTrue(messages.isEmpty());
NetconfMessage receivedMessage = testChunkChannel.readInbound();
assertNotNull(receivedMessage);
XMLUnit.setIgnoreWhitespace(true);
assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class NetconfXMLToHelloMessageDecoder method decode.
@Override
@VisibleForTesting
public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws IOException, SAXException {
if (in.readableBytes() == 0) {
LOG.debug("No more content in incoming buffer.");
return;
}
in.markReaderIndex();
try {
if (LOG.isTraceEnabled()) {
LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
}
byte[] bytes = new byte[in.readableBytes()];
in.readBytes(bytes);
logMessage(bytes);
// Extract bytes containing header with additional metadata
String additionalHeader = null;
if (startsWithAdditionalHeader(bytes)) {
// Auth information containing username, ip address... extracted for monitoring
int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
if (endOfAuthHeader > -1) {
byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader);
additionalHeader = additionalHeaderToString(additionalHeaderBytes);
bytes = Arrays.copyOfRange(bytes, endOfAuthHeader, bytes.length);
}
}
Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
final NetconfMessage message = getNetconfMessage(additionalHeader, doc);
if (message instanceof NetconfHelloMessage) {
Preconditions.checkState(!helloReceived, "Multiple hello messages received, unexpected hello: %s", message);
out.add(message);
helloReceived = true;
// Non hello message, suspend the message and insert into cache
} else {
Preconditions.checkState(helloReceived, "Hello message not received, instead received: %s", message);
LOG.debug("Netconf message received during negotiation, caching {}", message);
nonHelloMessages.add(message);
}
} finally {
in.discardReadBytes();
}
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class NetconfXMLToMessageDecoder method decode.
@Override
public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws IOException, SAXException {
if (in.isReadable()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
}
// Skip all leading whitespaces by moving the reader index to the first non whitespace character
while (in.isReadable()) {
if (!isWhitespace(in.readByte())) {
// return reader index to the first non whitespace character
in.readerIndex(in.readerIndex() - 1);
break;
}
}
// Warn about leading whitespaces
if (in.readerIndex() != 0 && LOG.isWarnEnabled()) {
final byte[] strippedBytes = new byte[in.readerIndex()];
in.getBytes(0, strippedBytes, 0, in.readerIndex());
LOG.warn("XML message with unwanted leading bytes detected. Discarded the {} leading byte(s): '{}'", in.readerIndex(), ByteBufUtil.hexDump(Unpooled.wrappedBuffer(strippedBytes)));
}
}
if (in.isReadable()) {
NetconfMessage msg;
try {
msg = new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in)));
} catch (SAXParseException exception) {
LOG.error("Failed to parse received message", exception);
msg = new FailedNetconfMessage(exception);
}
out.add(msg);
} else {
LOG.debug("No more content in incoming buffer.");
}
}
Aggregations