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();
}
}
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());
}
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);
}
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);
}
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);
}
Aggregations