Merge "Fix preparing result of action"
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / NetconfXMLToHelloMessageDecoder.java
index 5353381748e610f2cd3e90076b6fd3a082833a79..31a3284debfe46222773f08a3facda07dbd9c7c2 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.netconf.nettyutil.handler;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
 import io.netty.channel.ChannelHandlerContext;
@@ -19,13 +18,13 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -34,9 +33,7 @@ import org.xml.sax.SAXException;
 /**
  * Customized NetconfXMLToMessageDecoder that reads additional header with
  * session metadata from
- * {@link NetconfHelloMessage}
- *
- *
+ * {@link NetconfHelloMessage}*
  * This handler should be replaced in pipeline by regular message handler as last step of negotiation.
  * It serves as a message barrier and halts all non-hello netconf messages.
  * Netconf messages after hello should be processed once the negotiation succeeded.
@@ -56,12 +53,13 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder
     // State variables do not have to by synchronized
     // Netty uses always the same (1) thread per pipeline
     // We use instance of this per pipeline
-    private final List<NetconfMessage> nonHelloMessages = Lists.newArrayList();
+    private final List<NetconfMessage> nonHelloMessages = new ArrayList<>();
     private boolean helloReceived = false;
 
     @Override
     @VisibleForTesting
-    public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws IOException, SAXException, NetconfDocumentedException {
+    public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
+            throws IOException, SAXException {
         if (in.readableBytes() == 0) {
             LOG.debug("No more content in incoming buffer.");
             return;
@@ -94,7 +92,7 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder
 
             final NetconfMessage message = getNetconfMessage(additionalHeader, doc);
             if (message instanceof NetconfHelloMessage) {
-                Preconditions.checkState(helloReceived == false,
+                Preconditions.checkState(!helloReceived,
                         "Multiple hello messages received, unexpected hello: %s", message);
                 out.add(message);
                 helloReceived = true;
@@ -109,9 +107,9 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder
         }
     }
 
-    private static NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) throws NetconfDocumentedException {
+    private static NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) {
         NetconfMessage msg = new NetconfMessage(doc);
-        if(NetconfHelloMessage.isHelloMessage(msg)) {
+        if (NetconfHelloMessage.isHelloMessage(msg)) {
             if (additionalHeader != null) {
                 return new NetconfHelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader));
             } else {
@@ -145,15 +143,15 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder
                 return -1;
             }
         }
-        int j = 0;
+        int index = 0;
         for (int i = 0; i < bytes.length; i++) {
-            if (bytes[i] == sequence[j]) {
-                j++;
-                if (j == sequence.length) {
-                    return i - j + 1;
+            if (bytes[i] == sequence[index]) {
+                index++;
+                if (index == sequence.length) {
+                    return i - index + 1;
                 }
             } else {
-                j = 0;
+                index = 0;
             }
         }
         return -1;
@@ -161,20 +159,20 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder
 
     private static void logMessage(final byte[] bytes) {
         if (LOG.isDebugEnabled()) {
-            String s = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
-            LOG.debug("Parsing message \n{}", s);
+            String string = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
+            LOG.debug("Parsing message \n{}", string);
         }
     }
 
     private static boolean startsWithAdditionalHeader(final byte[] bytes) {
         for (byte[] possibleStart : POSSIBLE_STARTS) {
-            int i = 0;
+            int index = 0;
             for (byte b : possibleStart) {
-                if(bytes[i++] != b) {
+                if (bytes[index++] != b) {
                     break;
                 }
 
-                if(i == possibleStart.length) {
+                if (index == possibleStart.length) {
                     return true;
                 }
             }
@@ -188,7 +186,9 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder
     }
 
     /**
-     * @return Collection of NetconfMessages that were not hello, but were received during negotiation
+     * Get netconf messages received during negotiation.
+     *
+     * @return Collection of NetconfMessages that were not hello, but were received during negotiation.
      */
     public Iterable<NetconfMessage> getPostHelloNetconfMessages() {
         return nonHelloMessages;