Bug 1029: Remove dead code: sal-schema-repository-api
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / 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.controller.netconf.nettyutil.handler;
9
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;
16
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;
22
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;
31
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;
36
37 /**
38  * Customized NetconfXMLToMessageDecoder that reads additional header with
39  * session metadata from
40  * {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage}
41  *
42  *
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.
46  *
47  */
48 public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder {
49     private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToHelloMessageDecoder.class);
50
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(
55             new byte[] { '[' },
56             new byte[] { '\r', '\n', '[' },
57             new byte[] { '\n', '[' });
58
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;
64
65     @Override
66     @VisibleForTesting
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.");
70             return;
71         }
72
73         in.markReaderIndex();
74         try {
75             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
76             byte[] bytes = new byte[in.readableBytes()];
77             in.readBytes(bytes);
78
79             logMessage(bytes);
80
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);
90                 }
91             }
92
93             Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
94
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()));
100                 out.add(message);
101                 helloReceived = true;
102             // Non hello message, suspend the message and insert into cache
103             } else {
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);
109             }
110         } finally {
111             in.discardReadBytes();
112         }
113     }
114
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));
120             } else {
121                 return new NetconfHelloMessage(doc);
122             }
123         }
124
125         return msg;
126     }
127
128     private int getAdditionalHeaderEndIndex(byte[] bytes) {
129         for (byte[] possibleEnd : POSSIBLE_ENDS) {
130             int idx = findByteSequence(bytes, possibleEnd);
131
132             if (idx != -1) {
133                 return idx;
134             }
135         }
136
137         return -1;
138     }
139
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.");
143         }
144         if (bytes.length == sequence.length) {
145             if (Arrays.equals(bytes, sequence)) {
146                 return 0;
147             } else {
148                 return -1;
149             }
150         }
151         int j = 0;
152         for (int i = 0; i < bytes.length; i++) {
153             if (bytes[i] == sequence[j]) {
154                 j++;
155                 if (j == sequence.length) {
156                     return i - j + 1;
157                 }
158             } else {
159                 j = 0;
160             }
161         }
162         return -1;
163     }
164
165
166     private void logMessage(byte[] bytes) {
167         String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
168         LOG.debug("Parsing message \n{}", s);
169     }
170
171     private boolean startsWithAdditionalHeader(byte[] bytes) {
172         for (byte[] possibleStart : POSSIBLE_STARTS) {
173             int i = 0;
174             for (byte b : possibleStart) {
175                 if(bytes[i++] != b) {
176                     break;
177                 }
178
179                 if(i == possibleStart.length) {
180                     return true;
181                 }
182             }
183         }
184
185         return false;
186     }
187
188     private String additionalHeaderToString(byte[] bytes) {
189         return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
190     }
191
192     /**
193      * @return Collection of NetconfMessages that were not hello, but were received during negotiation
194      */
195     public Iterable<NetconfMessage> getPostHelloNetconfMessages() {
196         return nonHelloMessages;
197     }
198 }