Merge "Add test for generated code checking list of dependencies."
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / util / NetconfUtil.java
1 package org.opendaylight.controller.netconf.impl.util;
2
3 import com.google.common.base.Preconditions;
4 import org.opendaylight.controller.netconf.api.NetconfMessage;
5 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.w3c.dom.Document;
9 import org.xml.sax.SAXException;
10
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.InputStream;
16
17 public class NetconfUtil {
18
19     private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
20
21     public static NetconfMessage createMessage(final File f) {
22         Preconditions.checkNotNull(f, "File parameter was null");
23         try {
24             return createMessage(new FileInputStream(f));
25         } catch (final FileNotFoundException e) {
26             logger.warn("File {} not found.", f, e);
27         }
28         return null;
29     }
30
31     public static NetconfMessage createMessage(final InputStream is) {
32         Preconditions.checkNotNull(is, "InputStream parameter was null");
33         Document doc = null;
34         try {
35             doc = XmlUtil.readXmlToDocument(is);
36         } catch (final IOException e) {
37             logger.warn("Error ocurred while parsing stream.", e);
38         } catch (final SAXException e) {
39             logger.warn("Error ocurred while final parsing stream.", e);
40         }
41         return (doc == null) ? null : new NetconfMessage(doc);
42     }
43 }