X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnettyutil%2Fhandler%2FNetconfXMLToHelloMessageDecoder.java;h=69af52d61e0e8eb3b8617b07b8006a6cf0fd9ff3;hb=refs%2Fchanges%2F13%2F23413%2F26;hp=1fbac0b1ba04a4926bea0853d87fb1febc8599ba;hpb=0418d143fbc882b24becaa52ab115a4a42d3328c;p=controller.git diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToHelloMessageDecoder.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToHelloMessageDecoder.java index 1fbac0b1ba..69af52d61e 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToHelloMessageDecoder.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToHelloMessageDecoder.java @@ -21,11 +21,11 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.List; +import org.opendaylight.controller.config.util.xml.XmlUtil; import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage; import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader; -import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; @@ -56,12 +56,12 @@ 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 List nonHelloMessages = Lists.newArrayList(); + private final List nonHelloMessages = Lists.newArrayList(); private boolean helloReceived = false; @Override @VisibleForTesting - public void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws IOException, SAXException, NetconfDocumentedException { + public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List out) throws IOException, SAXException, NetconfDocumentedException { if (in.readableBytes() == 0) { LOG.debug("No more content in incoming buffer."); return; @@ -84,9 +84,9 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder // Auth information containing username, ip address... extracted for monitoring int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes); if (endOfAuthHeader > -1) { - byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2); + byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader); additionalHeader = additionalHeaderToString(additionalHeaderBytes); - bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length); + bytes = Arrays.copyOfRange(bytes, endOfAuthHeader, bytes.length); } } @@ -95,16 +95,13 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder final NetconfMessage message = getNetconfMessage(additionalHeader, doc); if (message instanceof NetconfHelloMessage) { Preconditions.checkState(helloReceived == false, - "Multiple hello messages received, unexpected hello: %s", - XmlUtil.toString(message.getDocument())); + "Multiple hello messages received, unexpected hello: %s", message); out.add(message); helloReceived = true; // Non hello message, suspend the message and insert into cache } else { - Preconditions.checkState(helloReceived, "Hello message not received, instead received: %s", - XmlUtil.toString(message.getDocument())); - LOG.debug("Netconf message received during negotiation, caching {}", - XmlUtil.toString(message.getDocument())); + Preconditions.checkState(helloReceived, "Hello message not received, instead received: %s", message); + LOG.debug("Netconf message received during negotiation, caching {}", message); nonHelloMessages.add(message); } } finally { @@ -112,7 +109,7 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder } } - private NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) throws NetconfDocumentedException { + private static NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) throws NetconfDocumentedException { NetconfMessage msg = new NetconfMessage(doc); if(NetconfHelloMessage.isHelloMessage(msg)) { if (additionalHeader != null) { @@ -125,12 +122,12 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder return msg; } - private int getAdditionalHeaderEndIndex(byte[] bytes) { + private static int getAdditionalHeaderEndIndex(final byte[] bytes) { for (byte[] possibleEnd : POSSIBLE_ENDS) { int idx = findByteSequence(bytes, possibleEnd); if (idx != -1) { - return idx; + return idx + possibleEnd.length; } } @@ -162,13 +159,14 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder return -1; } - - private void logMessage(byte[] bytes) { - String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); - LOG.debug("Parsing message \n{}", s); + private static void logMessage(final byte[] bytes) { + if (LOG.isDebugEnabled()) { + String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); + LOG.debug("Parsing message \n{}", s); + } } - private boolean startsWithAdditionalHeader(byte[] bytes) { + private static boolean startsWithAdditionalHeader(final byte[] bytes) { for (byte[] possibleStart : POSSIBLE_STARTS) { int i = 0; for (byte b : possibleStart) { @@ -185,7 +183,7 @@ public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder return false; } - private String additionalHeaderToString(byte[] bytes) { + private static String additionalHeaderToString(final byte[] bytes) { return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); }