2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.util.messages;
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Lists;
14 import org.opendaylight.controller.netconf.api.NetconfDeserializerException;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
17 import org.opendaylight.protocol.framework.DeserializerException;
18 import org.opendaylight.protocol.framework.DocumentedException;
19 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Comment;
24 import org.w3c.dom.Document;
25 import org.xml.sax.SAXException;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.nio.ByteBuffer;
30 import java.util.Arrays;
31 import java.util.List;
34 * NetconfMessageFactory for (de)serializing DOM documents.
36 public final class NetconfMessageFactory implements ProtocolMessageFactory<NetconfMessage> {
38 private static final Logger logger = LoggerFactory.getLogger(NetconfMessageFactory.class);
40 private final Optional<String> clientId;
42 public NetconfMessageFactory() {
43 clientId = Optional.absent();
46 public NetconfMessageFactory(Optional<String> clientId) {
47 this.clientId = clientId;
51 public NetconfMessage parse(byte[] bytes) throws DeserializerException, DocumentedException {
54 String additionalHeader = null;
56 if (startsWithAdditionalHeader(bytes)) {
57 // Auth information containing username, ip address... extracted for monitoring
58 int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
59 if (endOfAuthHeader > -1) {
60 byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
61 additionalHeader = additionalHeaderToString(additionalHeaderBytes);
62 bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
65 NetconfMessage message;
67 Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
68 message = new NetconfMessage(doc, additionalHeader);
69 } catch (final SAXException | IOException | IllegalStateException e) {
70 throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e);
75 private int getAdditionalHeaderEndIndex(byte[] bytes) {
76 for (String possibleEnd : Lists.newArrayList("]\n", "]\r\n")) {
77 int idx = ByteArray.findByteSequence(bytes, possibleEnd.getBytes(Charsets.UTF_8));
87 private boolean startsWithAdditionalHeader(byte[] bytes) {
88 List<String> possibleStarts = Lists.newArrayList("[", "\r\n[", "\n[");
89 for (String possibleStart : possibleStarts) {
91 for (byte b : possibleStart.getBytes(Charsets.UTF_8)) {
102 private void logMessage(byte[] bytes) {
103 String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
104 logger.debug("Parsing message \n{}", s);
107 private String additionalHeaderToString(byte[] bytes) {
108 return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
112 public byte[] put(NetconfMessage netconfMessage) {
113 if (clientId.isPresent()) {
114 Comment comment = netconfMessage.getDocument().createComment("clientId:" + clientId.get());
115 netconfMessage.getDocument().appendChild(comment);
117 final ByteBuffer msgBytes = Charsets.UTF_8.encode(xmlToString(netconfMessage.getDocument()));
118 String content = xmlToString(netconfMessage.getDocument());
120 logger.trace("Putting message \n{}", content);
121 byte[] b = new byte[msgBytes.limit()];
126 private String xmlToString(Document doc) {
127 return XmlUtil.toString(doc, false);