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 com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.handler.codec.ByteToMessageDecoder;
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.nio.ByteBuffer;
20 import java.nio.charset.StandardCharsets;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import org.opendaylight.netconf.api.NetconfMessage;
25 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
26 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
27 import org.opendaylight.netconf.api.xml.XmlUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.xml.sax.SAXException;
34 * Customized NetconfXMLToMessageDecoder that reads additional header with
35 * session metadata from
36 * {@link NetconfHelloMessage}*
37 * This handler should be replaced in pipeline by regular message handler as last step of negotiation.
38 * It serves as a message barrier and halts all non-hello netconf messages.
39 * Netconf messages after hello should be processed once the negotiation succeeded.
42 public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder {
43 private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToHelloMessageDecoder.class);
45 private static final List<byte[]> POSSIBLE_ENDS = ImmutableList.of(
46 new byte[] { ']', '\n' },
47 new byte[] { ']', '\r', '\n' });
48 private static final List<byte[]> POSSIBLE_STARTS = ImmutableList.of(
50 new byte[] { '\r', '\n', '[' },
51 new byte[] { '\n', '[' });
53 // State variables do not have to by synchronized
54 // Netty uses always the same (1) thread per pipeline
55 // We use instance of this per pipeline
56 private final List<NetconfMessage> nonHelloMessages = new ArrayList<>();
57 private boolean helloReceived = false;
61 public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
62 throws IOException, SAXException {
63 if (in.readableBytes() == 0) {
64 LOG.debug("No more content in incoming buffer.");
70 if (LOG.isTraceEnabled()) {
71 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
74 byte[] bytes = new byte[in.readableBytes()];
79 // Extract bytes containing header with additional metadata
80 String additionalHeader = null;
81 if (startsWithAdditionalHeader(bytes)) {
82 // Auth information containing username, ip address... extracted for monitoring
83 int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
84 if (endOfAuthHeader > -1) {
85 byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader);
86 additionalHeader = additionalHeaderToString(additionalHeaderBytes);
87 bytes = Arrays.copyOfRange(bytes, endOfAuthHeader, bytes.length);
91 Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
93 final NetconfMessage message = getNetconfMessage(additionalHeader, doc);
94 if (message instanceof NetconfHelloMessage) {
95 Preconditions.checkState(!helloReceived,
96 "Multiple hello messages received, unexpected hello: %s", message);
99 // Non hello message, suspend the message and insert into cache
101 Preconditions.checkState(helloReceived, "Hello message not received, instead received: %s", message);
102 LOG.debug("Netconf message received during negotiation, caching {}", message);
103 nonHelloMessages.add(message);
106 in.discardReadBytes();
110 private static NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) {
111 NetconfMessage msg = new NetconfMessage(doc);
112 if (NetconfHelloMessage.isHelloMessage(msg)) {
113 if (additionalHeader != null) {
114 return new NetconfHelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader));
116 return new NetconfHelloMessage(doc);
123 private static int getAdditionalHeaderEndIndex(final byte[] bytes) {
124 for (byte[] possibleEnd : POSSIBLE_ENDS) {
125 int idx = findByteSequence(bytes, possibleEnd);
128 return idx + possibleEnd.length;
135 private static int findByteSequence(final byte[] bytes, final byte[] sequence) {
136 if (bytes.length < sequence.length) {
137 throw new IllegalArgumentException("Sequence to be found is longer than the given byte array.");
139 if (bytes.length == sequence.length) {
140 if (Arrays.equals(bytes, sequence)) {
147 for (int i = 0; i < bytes.length; i++) {
148 if (bytes[i] == sequence[index]) {
150 if (index == sequence.length) {
151 return i - index + 1;
160 private static void logMessage(final byte[] bytes) {
161 if (LOG.isDebugEnabled()) {
162 String string = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
163 LOG.debug("Parsing message \n{}", string);
167 private static boolean startsWithAdditionalHeader(final byte[] bytes) {
168 for (byte[] possibleStart : POSSIBLE_STARTS) {
170 for (byte b : possibleStart) {
171 if (bytes[index++] != b) {
175 if (index == possibleStart.length) {
184 private static String additionalHeaderToString(final byte[] bytes) {
185 return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
189 * Get netconf messages received during negotiation.
191 * @return Collection of NetconfMessages that were not hello, but were received during negotiation.
193 public Iterable<NetconfMessage> getPostHelloNetconfMessages() {
194 return nonHelloMessages;