Merge "Bug 564 - add missing sal-remote dependency."
[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.config.api.ConflictingVersionException;
12 import org.opendaylight.controller.netconf.api.NetconfMessage;
13 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
14 import org.opendaylight.controller.netconf.util.xml.XmlElement;
15 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
16 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.w3c.dom.Document;
20 import org.xml.sax.SAXException;
21
22 import javax.xml.xpath.XPathConstants;
23 import javax.xml.xpath.XPathExpression;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29
30 public final class NetconfUtil {
31
32     private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
33
34     private NetconfUtil() {}
35
36     public static NetconfMessage createMessage(final File f) {
37         Preconditions.checkNotNull(f, "File parameter was null");
38         try {
39             return createMessage(new FileInputStream(f));
40         } catch (final FileNotFoundException e) {
41             logger.warn("File {} not found.", f, e);
42         }
43         return null;
44     }
45
46     public static NetconfMessage createMessage(final InputStream is) {
47         Preconditions.checkNotNull(is, "InputStream parameter was null");
48         Document doc = null;
49         try {
50             doc = XmlUtil.readXmlToDocument(is);
51         } catch (final IOException e) {
52             logger.warn("Error ocurred while parsing stream.", e);
53         } catch (final SAXException e) {
54             logger.warn("Error ocurred while final parsing stream.", e);
55         }
56         return (doc == null) ? null : new NetconfMessage(doc);
57     }
58
59     public static Document checkIsMessageOk(NetconfMessage responseMessage) throws ConflictingVersionException {
60         return checkIsMessageOk(responseMessage.getDocument());
61     }
62
63     public static Document checkIsMessageOk(Document response) throws ConflictingVersionException {
64         XmlElement element = XmlElement.fromDomDocument(response);
65         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
66         element = element.getOnlyChildElement();
67
68         if (element.getName().equals(XmlNetconfConstants.OK)) {
69             return response;
70         }
71
72         if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
73             logger.warn("Can not load last configuration, operation failed");
74             // is it ConflictingVersionException ?
75             XPathExpression xPathExpression = XMLNetconfUtil.compileXPath("/netconf:rpc-reply/netconf:rpc-error/netconf:error-info/netconf:error");
76             String error = (String) XmlUtil.evaluateXPath(xPathExpression, element.getDomElement(), XPathConstants.STRING);
77             if (error!=null && error.contains(ConflictingVersionException.class.getCanonicalName())) {
78                 throw new ConflictingVersionException(error);
79             }
80             throw new IllegalStateException("Can not load last configuration, operation failed: "
81                     + XmlUtil.toString(response));
82         }
83
84         logger.warn("Can not load last configuration. Operation failed.");
85         throw new IllegalStateException("Can not load last configuration. Operation failed: "
86                 + XmlUtil.toString(response));
87     }
88 }