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