5b13003c2ff2d2c1f425e052eece84508d39f5d4
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / NetconfXMLToHelloMessageDecoder.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.netconf.nettyutil.handler;
9
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.messages.HelloMessage;
25 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
26 import org.opendaylight.netconf.api.messages.NetconfMessage;
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;
32
33 /**
34  * Customized NetconfXMLToMessageDecoder that reads additional header with
35  * session metadata from
36  * {@link HelloMessage}*
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.
40  *
41  */
42 public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder {
43     private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToHelloMessageDecoder.class);
44
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(
49             new byte[] { '[' },
50             new byte[] { '\r', '\n', '[' },
51             new byte[] { '\n', '[' });
52
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;
58
59     @Override
60     @VisibleForTesting
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.");
65             return;
66         }
67
68         in.markReaderIndex();
69         try {
70             if (LOG.isTraceEnabled()) {
71                 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
72             }
73
74             byte[] bytes = new byte[in.readableBytes()];
75             in.readBytes(bytes);
76
77             logMessage(bytes);
78
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);
88                 }
89             }
90
91             Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
92
93             final NetconfMessage message = getNetconfMessage(additionalHeader, doc);
94             if (message instanceof HelloMessage) {
95                 Preconditions.checkState(!helloReceived,
96                         "Multiple hello messages received, unexpected hello: %s", message);
97                 out.add(message);
98                 helloReceived = true;
99             // Non hello message, suspend the message and insert into cache
100             } else {
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);
104             }
105         } finally {
106             in.discardReadBytes();
107         }
108     }
109
110     private static NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) {
111         NetconfMessage msg = new NetconfMessage(doc);
112         if (HelloMessage.isHelloMessage(msg)) {
113             if (additionalHeader != null) {
114                 return new HelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader));
115             } else {
116                 return new HelloMessage(doc);
117             }
118         }
119
120         return msg;
121     }
122
123     private static int getAdditionalHeaderEndIndex(final byte[] bytes) {
124         for (byte[] possibleEnd : POSSIBLE_ENDS) {
125             int idx = findByteSequence(bytes, possibleEnd);
126
127             if (idx != -1) {
128                 return idx + possibleEnd.length;
129             }
130         }
131
132         return -1;
133     }
134
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.");
138         }
139         if (bytes.length == sequence.length) {
140             if (Arrays.equals(bytes, sequence)) {
141                 return 0;
142             } else {
143                 return -1;
144             }
145         }
146         int index = 0;
147         for (int i = 0; i < bytes.length; i++) {
148             if (bytes[i] == sequence[index]) {
149                 index++;
150                 if (index == sequence.length) {
151                     return i - index + 1;
152                 }
153             } else {
154                 index = 0;
155             }
156         }
157         return -1;
158     }
159
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);
164         }
165     }
166
167     private static boolean startsWithAdditionalHeader(final byte[] bytes) {
168         for (byte[] possibleStart : POSSIBLE_STARTS) {
169             int index = 0;
170             for (byte b : possibleStart) {
171                 if (bytes[index++] != b) {
172                     break;
173                 }
174
175                 if (index == possibleStart.length) {
176                     return true;
177                 }
178             }
179         }
180
181         return false;
182     }
183
184     private static String additionalHeaderToString(final byte[] bytes) {
185         return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
186     }
187
188     /**
189      * Get netconf messages received during negotiation.
190      *
191      * @return Collection of NetconfMessages that were not hello, but were received during negotiation.
192      */
193     public Iterable<NetconfMessage> getPostHelloNetconfMessages() {
194         return nonHelloMessages;
195     }
196 }