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%2Fmessages%2FNetconfMessageFactory.java;h=891d40cf74fa520be681aa73109765424f5ab8e8;hp=029d2ba7595c2a7fabb0f2ec74511fcf8b166184;hb=6da86863382c6c850fb2f7c7d2e026d0aa96ae1a;hpb=e6bcd06e610be274e8f2df901b61789bb17c442a 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 029d2ba759..891d40cf74 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 @@ -37,12 +37,6 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory clientId; public NetconfMessageFactory() { @@ -54,25 +48,64 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory parse(byte[] bytes) throws DeserializerException, DocumentedException { - String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString(); - logger.debug("Parsing message \n{}", s); - if (bytes[0] == '[') { - // yuma sends auth information in the first line. Ignore until ]\n - // is found. - int endOfAuthHeader = ByteArray.findByteSequence(bytes, new byte[] { ']', '\n' }); + public NetconfMessage parse(byte[] bytes) throws DeserializerException, DocumentedException { + logMessage(bytes); + + String additionalHeader = null; + + 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); } } - List messages = Lists.newArrayList(); + NetconfMessage message; try { Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes)); - messages.add(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 messages; + 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 @@ -85,7 +118,7 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory