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 public static final byte[] endOfMessage = "]]>]]>".getBytes(Charsets.UTF_8);
42 public static final byte[] endOfChunk = "\n##\n".getBytes(Charsets.UTF_8);
44 public static final int MAX_CHUNK_SIZE = 1024; // Bytes
46 private final Optional<String> clientId;
48 public NetconfMessageFactory() {
49 clientId = Optional.absent();
52 public NetconfMessageFactory(Optional<String> clientId) {
53 this.clientId = clientId;
57 public List<NetconfMessage> parse(byte[] bytes) throws DeserializerException, DocumentedException {
58 String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
59 logger.debug("Parsing message \n{}", s);
60 if (bytes[0] == '[') {
61 // yuma sends auth information in the first line. Ignore until ]\n
63 int endOfAuthHeader = ByteArray.findByteSequence(bytes, new byte[] { ']', '\n' });
64 if (endOfAuthHeader > -1) {
65 bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
68 List<NetconfMessage> messages = Lists.newArrayList();
70 Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
71 messages.add(new NetconfMessage(doc));
72 } catch (final SAXException | IOException | IllegalStateException e) {
73 throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e);
79 public byte[] put(NetconfMessage netconfMessage) {
80 if (clientId.isPresent()) {
81 Comment comment = netconfMessage.getDocument().createComment("clientId:" + clientId.get());
82 netconfMessage.getDocument().appendChild(comment);
84 final ByteBuffer msgBytes = Charsets.UTF_8.encode(xmlToString(netconfMessage.getDocument()));
85 String content = xmlToString(netconfMessage.getDocument());
87 logger.trace("Putting message \n{}", content);
88 byte[] b = new byte[msgBytes.remaining()];
93 private String xmlToString(Document doc) {
94 return XmlUtil.toString(doc, false);