029d2ba7595c2a7fabb0f2ec74511fcf8b166184
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / NetconfMessageFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.util.messages;
10
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;
26
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;
32
33 /**
34  * NetconfMessageFactory for (de)serializing DOM documents.
35  */
36 public final class NetconfMessageFactory implements ProtocolMessageFactory<NetconfMessage> {
37
38     private static final Logger logger = LoggerFactory.getLogger(NetconfMessageFactory.class);
39
40     public static final byte[] endOfMessage = "]]>]]>".getBytes(Charsets.UTF_8);
41
42     public static final byte[] endOfChunk = "\n##\n".getBytes(Charsets.UTF_8);
43
44     public static final int MAX_CHUNK_SIZE = 1024; // Bytes
45
46     private final Optional<String> clientId;
47
48     public NetconfMessageFactory() {
49         clientId = Optional.absent();
50     }
51
52     public NetconfMessageFactory(Optional<String> clientId) {
53         this.clientId = clientId;
54     }
55
56     @Override
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
62             // is found.
63             int endOfAuthHeader = ByteArray.findByteSequence(bytes, new byte[] { ']', '\n' });
64             if (endOfAuthHeader > -1) {
65                 bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
66             }
67         }
68         List<NetconfMessage> messages = Lists.newArrayList();
69         try {
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);
74         }
75         return messages;
76     }
77
78     @Override
79     public byte[] put(NetconfMessage netconfMessage) {
80         if (clientId.isPresent()) {
81             Comment comment = netconfMessage.getDocument().createComment("clientId:" + clientId.get());
82             netconfMessage.getDocument().appendChild(comment);
83         }
84         final ByteBuffer msgBytes = Charsets.UTF_8.encode(xmlToString(netconfMessage.getDocument()));
85         String content = xmlToString(netconfMessage.getDocument());
86
87         logger.trace("Putting message \n{}", content);
88         byte[] b = new byte[msgBytes.remaining()];
89         msgBytes.get(b);
90         return b;
91     }
92
93     private String xmlToString(Document doc) {
94         return XmlUtil.toString(doc, false);
95     }
96 }