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.util.handler;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufUtil;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.nio.ByteBuffer;
18 import java.util.Arrays;
19 import java.util.List;
21 import org.opendaylight.controller.netconf.api.NetconfDeserializerException;
22 import org.opendaylight.controller.netconf.api.NetconfMessage;
23 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.xml.sax.SAXException;
29 import com.google.common.annotations.VisibleForTesting;
30 import com.google.common.base.Charsets;
31 import com.google.common.collect.ImmutableList;
33 public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder {
34 private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class);
36 private static final List<byte[]> POSSIBLE_ENDS = ImmutableList.of(
37 new byte[] { ']', '\n' },
38 new byte[] { ']', '\r', '\n' });
39 private static final List<byte[]> POSSIBLE_STARTS = ImmutableList.of(
41 new byte[] { '\r', '\n', '[' },
42 new byte[] { '\n', '[' });
46 public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
47 if (in.readableBytes() == 0) {
48 LOG.debug("No more content in incoming buffer.");
54 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
55 byte[] bytes = new byte[in.readableBytes()];
60 String additionalHeader = null;
62 // FIXME: this has to be moved into the negotiator and explained as to what the heck
63 // is going on. This is definitely not specified in NETCONF and has no place here. It
64 // requires reading all data and incurs inefficiency by being unable to pipe the ByteBuf
65 // directly into the XML decoder.
66 if (startsWithAdditionalHeader(bytes)) {
67 // Auth information containing username, ip address... extracted for monitoring
68 int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
69 if (endOfAuthHeader > -1) {
70 byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
71 additionalHeader = additionalHeaderToString(additionalHeaderBytes);
72 bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
75 NetconfMessage message;
77 Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
78 message = new NetconfMessage(doc, additionalHeader);
79 } catch (final SAXException | IOException | IllegalStateException e) {
80 throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e);
85 in.discardReadBytes();
89 private int getAdditionalHeaderEndIndex(byte[] bytes) {
90 for (byte[] possibleEnd : POSSIBLE_ENDS) {
91 int idx = findByteSequence(bytes, possibleEnd);
101 private static int findByteSequence(final byte[] bytes, final byte[] sequence) {
102 if (bytes.length < sequence.length) {
103 throw new IllegalArgumentException("Sequence to be found is longer than the given byte array.");
105 if (bytes.length == sequence.length) {
106 if (Arrays.equals(bytes, sequence)) {
113 for (int i = 0; i < bytes.length; i++) {
114 if (bytes[i] == sequence[j]) {
116 if (j == sequence.length) {
126 private boolean startsWithAdditionalHeader(byte[] bytes) {
127 for (byte[] possibleStart : POSSIBLE_STARTS) {
129 for (byte b : possibleStart) {
140 private void logMessage(byte[] bytes) {
141 String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
142 LOG.debug("Parsing message \n{}", s);
145 private String additionalHeaderToString(byte[] bytes) {
146 return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();