Add missing headers to config, netconf subsystems
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / util / NetconfUtil.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 package org.opendaylight.controller.netconf.impl.util;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.netconf.api.NetconfMessage;
12 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.w3c.dom.Document;
16 import org.xml.sax.SAXException;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 public class NetconfUtil {
25
26     private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
27
28     public static NetconfMessage createMessage(final File f) {
29         Preconditions.checkNotNull(f, "File parameter was null");
30         try {
31             return createMessage(new FileInputStream(f));
32         } catch (final FileNotFoundException e) {
33             logger.warn("File {} not found.", f, e);
34         }
35         return null;
36     }
37
38     public static NetconfMessage createMessage(final InputStream is) {
39         Preconditions.checkNotNull(is, "InputStream parameter was null");
40         Document doc = null;
41         try {
42             doc = XmlUtil.readXmlToDocument(is);
43         } catch (final IOException e) {
44             logger.warn("Error ocurred while parsing stream.", e);
45         } catch (final SAXException e) {
46             logger.warn("Error ocurred while final parsing stream.", e);
47         }
48         return (doc == null) ? null : new NetconfMessage(doc);
49     }
50 }