2eefb917245e152ee0cb59e7eb84f8867a5edfa3
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / handler / NetconfXMLToMessageDecoder.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.util.handler;
9
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;
14
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;
20
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;
28
29 import com.google.common.annotations.VisibleForTesting;
30 import com.google.common.base.Charsets;
31 import com.google.common.collect.ImmutableList;
32
33 public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder {
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class);
35
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(
40             new byte[] { '[' },
41             new byte[] { '\r', '\n', '[' },
42             new byte[] { '\n', '[' });
43
44     @Override
45     @VisibleForTesting
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.");
49             return;
50         }
51
52         in.markReaderIndex();
53         try {
54             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
55             byte[] bytes = new byte[in.readableBytes()];
56             in.readBytes(bytes);
57
58             logMessage(bytes);
59
60             String additionalHeader = null;
61
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);
73                 }
74             }
75             NetconfMessage message;
76             try {
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);
81             }
82
83             out.add(message);
84         } finally {
85             in.discardReadBytes();
86         }
87     }
88
89     private int getAdditionalHeaderEndIndex(byte[] bytes) {
90         for (byte[] possibleEnd : POSSIBLE_ENDS) {
91             int idx = findByteSequence(bytes, possibleEnd);
92
93             if (idx != -1) {
94                 return idx;
95             }
96         }
97
98         return -1;
99     }
100
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.");
104         }
105         if (bytes.length == sequence.length) {
106             if (Arrays.equals(bytes, sequence)) {
107                 return 0;
108             } else {
109                 return -1;
110             }
111         }
112         int j = 0;
113         for (int i = 0; i < bytes.length; i++) {
114             if (bytes[i] == sequence[j]) {
115                 j++;
116                 if (j == sequence.length) {
117                     return i - j + 1;
118                 }
119             } else {
120                 j = 0;
121             }
122         }
123         return -1;
124     }
125
126     private boolean startsWithAdditionalHeader(byte[] bytes) {
127         for (byte[] possibleStart : POSSIBLE_STARTS) {
128             int i = 0;
129             for (byte b : possibleStart) {
130                 if(bytes[i] != b)
131                     break;
132
133                 return true;
134             }
135         }
136
137         return false;
138     };
139
140     private void logMessage(byte[] bytes) {
141         String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
142         LOG.debug("Parsing message \n{}", s);
143     }
144
145     private String additionalHeaderToString(byte[] bytes) {
146         return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
147     }
148
149 }