Search in sources :

Example 16 with NetconfMessage

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));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Document(org.w3c.dom.Document)

Example 17 with NetconfMessage

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());
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) NetconfXMLToMessageDecoder(org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) NetconfMessageToXMLEncoder(org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder) NetconfEOMAggregator(org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator) Test(org.junit.Test)

Example 18 with NetconfMessage

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());
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) NetconfChunkAggregator(org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator) NetconfXMLToMessageDecoder(org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) NetconfMessageToXMLEncoder(org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder) Test(org.junit.Test)

Example 19 with NetconfMessage

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();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) NetconfHelloMessage(org.opendaylight.netconf.api.messages.NetconfHelloMessage) Document(org.w3c.dom.Document) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with NetconfMessage

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.");
    }
}
Also used : FailedNetconfMessage(org.opendaylight.netconf.api.FailedNetconfMessage) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) FailedNetconfMessage(org.opendaylight.netconf.api.FailedNetconfMessage) SAXParseException(org.xml.sax.SAXParseException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

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