2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.nettyutil.handler;
10 import com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufUtil;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.nio.ByteBuffer;
18 import java.nio.charset.StandardCharsets;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import org.opendaylight.netconf.api.messages.HelloMessage;
23 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
24 import org.opendaylight.netconf.api.messages.NetconfMessage;
25 import org.opendaylight.netconf.api.xml.XmlUtil;
26 import org.opendaylight.odlparent.logging.markers.Markers;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.xml.sax.SAXException;
33 * Customized NetconfXMLToMessageDecoder that reads additional header with
34 * session metadata from
35 * {@link HelloMessage}*
36 * This handler should be replaced in pipeline by regular message handler as last step of negotiation.
37 * It serves as a message barrier and halts all non-hello netconf messages.
38 * Netconf messages after hello should be processed once the negotiation succeeded.
41 public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder {
42 private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToHelloMessageDecoder.class);
44 private static final List<byte[]> POSSIBLE_ENDS = List.of(
45 new byte[] { ']', '\n' },
46 new byte[] { ']', '\r', '\n' });
47 private static final List<byte[]> POSSIBLE_STARTS = List.of(
49 new byte[] { '\r', '\n', '[' },
50 new byte[] { '\n', '[' });
52 // State variables do not have to by synchronized
53 // Netty uses always the same (1) thread per pipeline
54 // We use instance of this per pipeline
55 private final List<NetconfMessage> nonHelloMessages = new ArrayList<>();
56 private boolean helloReceived = false;
60 public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
61 throws IOException, SAXException {
62 if (in.readableBytes() == 0) {
63 LOG.debug("No more content in incoming buffer.");
69 if (LOG.isTraceEnabled()) {
70 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
73 byte[] bytes = new byte[in.readableBytes()];
78 // Extract bytes containing header with additional metadata
79 String additionalHeader = null;
80 if (startsWithAdditionalHeader(bytes)) {
81 // Auth information containing username, ip address... extracted for monitoring
82 int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
83 if (endOfAuthHeader > -1) {
84 byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader);
85 additionalHeader = additionalHeaderToString(additionalHeaderBytes);
86 bytes = Arrays.copyOfRange(bytes, endOfAuthHeader, bytes.length);
90 Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
92 final NetconfMessage message = getNetconfMessage(additionalHeader, doc);
93 if (message instanceof HelloMessage) {
95 throw new IllegalStateException("Multiple hello messages received, unexpected hello: " + message);
99 } else if (helloReceived) {
100 // Non hello message, suspend the message and insert into cache
101 LOG.debug("Netconf message received during negotiation, caching {}", message);
102 nonHelloMessages.add(message);
104 throw new IllegalStateException("Hello message not received, instead received: " + message);
107 in.discardReadBytes();
111 private static NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) {
112 NetconfMessage msg = new NetconfMessage(doc);
113 if (HelloMessage.isHelloMessage(msg)) {
114 if (additionalHeader != null) {
115 return new HelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader));
117 return new HelloMessage(doc);
124 private static int getAdditionalHeaderEndIndex(final byte[] bytes) {
125 for (byte[] possibleEnd : POSSIBLE_ENDS) {
126 int idx = findByteSequence(bytes, possibleEnd);
129 return idx + possibleEnd.length;
136 private static int findByteSequence(final byte[] bytes, final byte[] sequence) {
137 if (bytes.length < sequence.length) {
138 throw new IllegalArgumentException("Sequence to be found is longer than the given byte array.");
140 if (bytes.length == sequence.length) {
141 if (Arrays.equals(bytes, sequence)) {
148 for (int i = 0; i < bytes.length; i++) {
149 if (bytes[i] == sequence[index]) {
151 if (index == sequence.length) {
152 return i - index + 1;
161 private static void logMessage(final byte[] bytes) {
162 if (LOG.isDebugEnabled()) {
163 LOG.debug(Markers.confidential(), "Parsing message \n{}",
164 StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString());
168 private static boolean startsWithAdditionalHeader(final byte[] bytes) {
169 for (byte[] possibleStart : POSSIBLE_STARTS) {
171 for (byte b : possibleStart) {
172 if (bytes[index++] != b) {
176 if (index == possibleStart.length) {
185 private static String additionalHeaderToString(final byte[] bytes) {
186 return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
190 * Get netconf messages received during negotiation.
192 * @return Collection of NetconfMessages that were not hello, but were received during negotiation.
194 public Iterable<NetconfMessage> getPostHelloNetconfMessages() {
195 return nonHelloMessages;