X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fmessages%2FNetconfMessageFactory.java;h=526708ab580238e34ba26a1c70a1d89289571ca6;hb=c541f7868e6e2d654b8080b5426bb12a39bddf11;hp=0b6914ec2ae44f12f5c4ade5a9c9574c7b2a7de6;hpb=3a186f6f164122ab47f816b5761ef526f939f946;p=controller.git diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageFactory.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageFactory.java index 0b6914ec2a..526708ab58 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageFactory.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageFactory.java @@ -8,8 +8,12 @@ package org.opendaylight.controller.netconf.util.messages; -import com.google.common.base.Charsets; -import com.google.common.base.Optional; +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; import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.util.xml.XmlUtil; @@ -23,10 +27,9 @@ import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.xml.sax.SAXException; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.Arrays; +import com.google.common.base.Charsets; +import com.google.common.base.Optional; +import com.google.common.collect.Lists; /** * NetconfMessageFactory for (de)serializing DOM documents. @@ -47,33 +50,79 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory -1) { + byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2); + additionalHeader = additionalHeaderToString(additionalHeaderBytes); bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length); } } - NetconfMessage message = null; + NetconfMessage message; try { Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes)); - message = new NetconfMessage(doc); + message = new NetconfMessage(doc, additionalHeader); } catch (final SAXException | IOException | IllegalStateException e) { throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e); } return message; } + private int getAdditionalHeaderEndIndex(byte[] bytes) { + for (String possibleEnd : Lists.newArrayList("]\n", "]\r\n")) { + int idx = ByteArray.findByteSequence(bytes, possibleEnd.getBytes(Charsets.UTF_8)); + + if (idx != -1) { + return idx; + } + } + + return -1; + } + + private boolean startsWithAdditionalHeader(byte[] bytes) { + List possibleStarts = Lists.newArrayList("[", "\r\n[", "\n["); + for (String possibleStart : possibleStarts) { + int i = 0; + for (byte b : possibleStart.getBytes(Charsets.UTF_8)) { + if(bytes[i]!=b) + break; + + return true; + } + } + + return false; + }; + + private void logMessage(byte[] bytes) { + String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); + logger.debug("Parsing message \n{}", s); + } + + private String additionalHeaderToString(byte[] bytes) { + return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); + } + @Override public byte[] put(NetconfMessage netconfMessage) { if (clientId.isPresent()) { Comment comment = netconfMessage.getDocument().createComment("clientId:" + clientId.get()); netconfMessage.getDocument().appendChild(comment); } - final ByteBuffer msgBytes = Charsets.UTF_8.encode(xmlToString(netconfMessage.getDocument())); + ByteBuffer msgBytes; + if(netconfMessage.getAdditionalHeader().isPresent()) { + String header = netconfMessage.getAdditionalHeader().get(); + logger.trace("Header of netconf message parsed \n{}", header); + msgBytes = Charsets.UTF_8.encode(header + xmlToString(netconfMessage.getDocument())); + } else { + msgBytes = Charsets.UTF_8.encode(xmlToString(netconfMessage.getDocument())); + } String content = xmlToString(netconfMessage.getDocument()); logger.trace("Putting message \n{}", content);