Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / 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.util;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
12 import org.opendaylight.controller.netconf.api.NetconfMessage;
13 import org.opendaylight.controller.netconf.util.xml.XmlElement;
14 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
15 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.w3c.dom.Document;
19 import org.xml.sax.SAXException;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 public final class NetconfUtil {
28
29     private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
30
31     private NetconfUtil() {}
32
33     public static NetconfMessage createMessage(final File f) {
34         Preconditions.checkNotNull(f, "File parameter was null");
35         try {
36             return createMessage(new FileInputStream(f));
37         } catch (final FileNotFoundException e) {
38             logger.warn("File {} not found.", f, e);
39         }
40         return null;
41     }
42
43     public static NetconfMessage createMessage(final InputStream is) {
44         Preconditions.checkNotNull(is, "InputStream parameter was null");
45         Document doc = null;
46         try {
47             doc = XmlUtil.readXmlToDocument(is);
48         } catch (final IOException e) {
49             logger.warn("Error ocurred while parsing stream.", e);
50         } catch (final SAXException e) {
51             logger.warn("Error ocurred while final parsing stream.", e);
52         }
53         return (doc == null) ? null : new NetconfMessage(doc);
54     }
55
56     public static Document checkIsMessageOk(Document response) throws NetconfDocumentedException {
57         XmlElement element = XmlElement.fromDomDocument(response);
58         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
59         element = element.getOnlyChildElement();
60         if (element.getName().equals(XmlNetconfConstants.OK)) {
61             return response;
62         }
63         logger.warn("Can not load last configuration. Operation failed.");
64         throw new IllegalStateException("Can not load last configuration. Operation failed: "
65                 + XmlUtil.toString(response));
66     }
67 }