Search in sources :

Example 1 with NetconfHelloMessage

use of org.opendaylight.netconf.api.messages.NetconfHelloMessage 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 2 with NetconfHelloMessage

use of org.opendaylight.netconf.api.messages.NetconfHelloMessage in project netconf by opendaylight.

the class NetconfXMLToHelloMessageDecoderTest method testDecodeNoHeader.

@Test
public void testDecodeNoHeader() throws Exception {
    final ByteBuf src = Unpooled.wrappedBuffer("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
    final List<Object> out = new ArrayList<>();
    new NetconfXMLToHelloMessageDecoder().decode(null, src, out);
    assertEquals(1, out.size());
    assertThat(out.get(0), CoreMatchers.instanceOf(NetconfHelloMessage.class));
    final NetconfHelloMessage hello = (NetconfHelloMessage) out.get(0);
    assertFalse(hello.getAdditionalHeader().isPresent());
}
Also used : ArrayList(java.util.ArrayList) NetconfHelloMessage(org.opendaylight.netconf.api.messages.NetconfHelloMessage) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 3 with NetconfHelloMessage

use of org.opendaylight.netconf.api.messages.NetconfHelloMessage in project netconf by opendaylight.

the class AbstractNetconfSessionTest method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, 1L);
    final NetconfHelloMessage hello = NetconfHelloMessage.createClientHello(Collections.emptySet(), Optional.empty());
    testingNetconfSession.sendMessage(hello);
    verify(channel).writeAndFlush(hello, writeFuture);
}
Also used : NetconfHelloMessage(org.opendaylight.netconf.api.messages.NetconfHelloMessage) Test(org.junit.Test)

Example 4 with NetconfHelloMessage

use of org.opendaylight.netconf.api.messages.NetconfHelloMessage in project netconf by opendaylight.

the class Netconf539Test method setUp.

@Before
public void setUp() throws Exception {
    channel = new EmbeddedChannel();
    channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, new ChannelInboundHandlerAdapter());
    channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, new NetconfXMLToHelloMessageDecoder());
    channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
    channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
    final NetconfHelloMessage serverHello = NetconfHelloMessage.createClientHello(Collections.singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.empty());
    negotiator = new TestSessionNegotiator(new NetconfSessionPreferences(serverHello), promise, channel, new HashedWheelTimer(), listener, 100L);
}
Also used : NetconfXMLToHelloMessageDecoder(org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetconfHelloMessage(org.opendaylight.netconf.api.messages.NetconfHelloMessage) HashedWheelTimer(io.netty.util.HashedWheelTimer) NetconfSessionPreferences(org.opendaylight.netconf.api.NetconfSessionPreferences) NetconfEOMAggregator(org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Before(org.junit.Before)

Example 5 with NetconfHelloMessage

use of org.opendaylight.netconf.api.messages.NetconfHelloMessage in project netconf by opendaylight.

the class NetconfClientSessionNegotiatorTest method createHelloMsg.

private static NetconfHelloMessage createHelloMsg(final String name) throws Exception {
    final InputStream stream = NetconfClientSessionNegotiatorTest.class.getResourceAsStream(name);
    final Document doc = XmlUtil.readXmlToDocument(stream);
    return new NetconfHelloMessage(doc);
}
Also used : InputStream(java.io.InputStream) NetconfHelloMessage(org.opendaylight.netconf.api.messages.NetconfHelloMessage) Document(org.w3c.dom.Document)

Aggregations

NetconfHelloMessage (org.opendaylight.netconf.api.messages.NetconfHelloMessage)14 Test (org.junit.Test)8 NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)5 ByteBuf (io.netty.buffer.ByteBuf)4 Document (org.w3c.dom.Document)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 ArrayList (java.util.ArrayList)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelProgressivePromise (io.netty.channel.ChannelProgressivePromise)1 ChannelPromise (io.netty.channel.ChannelPromise)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 HashedWheelTimer (io.netty.util.HashedWheelTimer)1 Promise (io.netty.util.concurrent.Promise)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Before (org.junit.Before)1