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.controller.netconf.nettyutil.handler;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufUtil;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.handler.codec.ByteToMessageDecoder;
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.nio.ByteBuffer;
20 import java.util.Arrays;
21 import java.util.List;
23 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.controller.netconf.api.NetconfMessage;
25 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
26 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
27 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
32 import com.google.common.annotations.VisibleForTesting;
33 import com.google.common.base.Charsets;
34 import com.google.common.collect.ImmutableList;
35 import org.xml.sax.SAXException;
38 * Customized NetconfXMLToMessageDecoder that reads additional header with
39 * session metadata from
40 * {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage}
43 * This handler should be replaced in pipeline by regular message handler as last step of negotiation.
44 * It serves as a message barrier and halts all non-hello netconf messages.
45 * Netconf messages after hello should be processed once the negotiation succeeded.
48 public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder {
49 private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToHelloMessageDecoder.class);
51 private static final List<byte[]> POSSIBLE_ENDS = ImmutableList.of(
52 new byte[] { ']', '\n' },
53 new byte[] { ']', '\r', '\n' });
54 private static final List<byte[]> POSSIBLE_STARTS = ImmutableList.of(
56 new byte[] { '\r', '\n', '[' },
57 new byte[] { '\n', '[' });
59 // State variables do not have to by synchronized
60 // Netty uses always the same (1) thread per pipeline
61 // We use instance of this per pipeline
62 private List<NetconfMessage> nonHelloMessages = Lists.newArrayList();
63 private boolean helloReceived = false;
67 public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IOException, SAXException, NetconfDocumentedException {
68 if (in.readableBytes() == 0) {
69 LOG.debug("No more content in incoming buffer.");
75 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
76 byte[] bytes = new byte[in.readableBytes()];
81 // Extract bytes containing header with additional metadata
82 String additionalHeader = null;
83 if (startsWithAdditionalHeader(bytes)) {
84 // Auth information containing username, ip address... extracted for monitoring
85 int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
86 if (endOfAuthHeader > -1) {
87 byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
88 additionalHeader = additionalHeaderToString(additionalHeaderBytes);
89 bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
93 Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
95 final NetconfMessage message = getNetconfMessage(additionalHeader, doc);
96 if (message instanceof NetconfHelloMessage) {
97 Preconditions.checkState(helloReceived == false,
98 "Multiple hello messages received, unexpected hello: %s",
99 XmlUtil.toString(message.getDocument()));
101 helloReceived = true;
102 // Non hello message, suspend the message and insert into cache
104 Preconditions.checkState(helloReceived, "Hello message not received, instead received: %s",
105 XmlUtil.toString(message.getDocument()));
106 LOG.debug("Netconf message received during negotiation, caching {}",
107 XmlUtil.toString(message.getDocument()));
108 nonHelloMessages.add(message);
111 in.discardReadBytes();
115 private NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) throws NetconfDocumentedException {
116 NetconfMessage msg = new NetconfMessage(doc);
117 if(NetconfHelloMessage.isHelloMessage(msg)) {
118 if (additionalHeader != null) {
119 return new NetconfHelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader));
121 return new NetconfHelloMessage(doc);
128 private int getAdditionalHeaderEndIndex(byte[] bytes) {
129 for (byte[] possibleEnd : POSSIBLE_ENDS) {
130 int idx = findByteSequence(bytes, possibleEnd);
140 private static int findByteSequence(final byte[] bytes, final byte[] sequence) {
141 if (bytes.length < sequence.length) {
142 throw new IllegalArgumentException("Sequence to be found is longer than the given byte array.");
144 if (bytes.length == sequence.length) {
145 if (Arrays.equals(bytes, sequence)) {
152 for (int i = 0; i < bytes.length; i++) {
153 if (bytes[i] == sequence[j]) {
155 if (j == sequence.length) {
166 private void logMessage(byte[] bytes) {
167 String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
168 LOG.debug("Parsing message \n{}", s);
171 private boolean startsWithAdditionalHeader(byte[] bytes) {
172 for (byte[] possibleStart : POSSIBLE_STARTS) {
174 for (byte b : possibleStart) {
175 if(bytes[i++] != b) {
179 if(i == possibleStart.length) {
188 private String additionalHeaderToString(byte[] bytes) {
189 return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
193 * @return Collection of NetconfMessages that were not hello, but were received during negotiation
195 public Iterable<NetconfMessage> getPostHelloNetconfMessages() {
196 return nonHelloMessages;