X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fhandler%2FNetconfXMLToMessageDecoder.java;h=b697edfb053229fd75f602798c10150f0152b86c;hp=2eefb917245e152ee0cb59e7eb84f8867a5edfa3;hb=082d7ba433b85d5810c50f624d2691088336e66a;hpb=c31af714994cbaed40299758460916b2c7101158 diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java index 2eefb91724..b697edfb05 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java @@ -7,15 +7,8 @@ */ package org.opendaylight.controller.netconf.util.handler; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; - import java.io.ByteArrayInputStream; -import java.io.IOException; import java.nio.ByteBuffer; -import java.util.Arrays; import java.util.List; import org.opendaylight.controller.netconf.api.NetconfDeserializerException; @@ -24,22 +17,17 @@ import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; -import org.xml.sax.SAXException; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Charsets; -import com.google.common.collect.ImmutableList; -public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder { - private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class); +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; - private static final List POSSIBLE_ENDS = ImmutableList.of( - new byte[] { ']', '\n' }, - new byte[] { ']', '\r', '\n' }); - private static final List POSSIBLE_STARTS = ImmutableList.of( - new byte[] { '[' }, - new byte[] { '\r', '\n', '[' }, - new byte[] { '\n', '[' }); +public class NetconfXMLToMessageDecoder extends ByteToMessageDecoder { + private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class); @Override @VisibleForTesting @@ -57,93 +45,35 @@ public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder { logMessage(bytes); - String additionalHeader = null; - - // FIXME: this has to be moved into the negotiator and explained as to what the heck - // is going on. This is definitely not specified in NETCONF and has no place here. It - // requires reading all data and incurs inefficiency by being unable to pipe the ByteBuf - // directly into the XML decoder. - if (startsWithAdditionalHeader(bytes)) { - // Auth information containing username, ip address... extracted for monitoring - int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes); - if (endOfAuthHeader > -1) { - byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2); - additionalHeader = additionalHeaderToString(additionalHeaderBytes); - bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length); - } - } + bytes = preprocessMessageBytes(bytes); NetconfMessage message; try { Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes)); - message = new NetconfMessage(doc, additionalHeader); - } catch (final SAXException | IOException | IllegalStateException e) { + message = buildNetconfMessage(doc); + } catch (Exception e) { throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e); } out.add(message); } finally { in.discardReadBytes(); + cleanUpAfterDecode(); } } - private int getAdditionalHeaderEndIndex(byte[] bytes) { - for (byte[] possibleEnd : POSSIBLE_ENDS) { - int idx = findByteSequence(bytes, possibleEnd); - - if (idx != -1) { - return idx; - } - } + protected void cleanUpAfterDecode() {} - return -1; + protected NetconfMessage buildNetconfMessage(Document doc) { + return new NetconfMessage(doc); } - private static int findByteSequence(final byte[] bytes, final byte[] sequence) { - if (bytes.length < sequence.length) { - throw new IllegalArgumentException("Sequence to be found is longer than the given byte array."); - } - if (bytes.length == sequence.length) { - if (Arrays.equals(bytes, sequence)) { - return 0; - } else { - return -1; - } - } - int j = 0; - for (int i = 0; i < bytes.length; i++) { - if (bytes[i] == sequence[j]) { - j++; - if (j == sequence.length) { - return i - j + 1; - } - } else { - j = 0; - } - } - return -1; + protected byte[] preprocessMessageBytes(byte[] bytes) { + return bytes; } - private boolean startsWithAdditionalHeader(byte[] bytes) { - for (byte[] possibleStart : POSSIBLE_STARTS) { - int i = 0; - for (byte b : possibleStart) { - if(bytes[i] != b) - break; - - return true; - } - } - - return false; - }; - private void logMessage(byte[] bytes) { String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); LOG.debug("Parsing message \n{}", s); } - private String additionalHeaderToString(byte[] bytes) { - return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); - } - }