Move NetconfUtil to netconf-util.
[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 class NetconfUtil {
31
32     private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
33
34     public static NetconfMessage createMessage(final File f) {
35         Preconditions.checkNotNull(f, "File parameter was null");
36         try {
37             return createMessage(new FileInputStream(f));
38         } catch (final FileNotFoundException e) {
39             logger.warn("File {} not found.", f, e);
40         }
41         return null;
42     }
43
44     public static NetconfMessage createMessage(final InputStream is) {
45         Preconditions.checkNotNull(is, "InputStream parameter was null");
46         Document doc = null;
47         try {
48             doc = XmlUtil.readXmlToDocument(is);
49         } catch (final IOException e) {
50             logger.warn("Error ocurred while parsing stream.", e);
51         } catch (final SAXException e) {
52             logger.warn("Error ocurred while final parsing stream.", e);
53         }
54         return (doc == null) ? null : new NetconfMessage(doc);
55     }
56
57     public static void checkIsMessageOk(NetconfMessage responseMessage) throws ConflictingVersionException {
58         XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
59         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
60         element = element.getOnlyChildElement();
61
62         if (element.getName().equals(XmlNetconfConstants.OK)) {
63             return;
64         }
65
66         if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
67             logger.warn("Can not load last configuration, operation failed");
68             // is it ConflictingVersionException ?
69             XPathExpression xPathExpression = XMLNetconfUtil.compileXPath("/netconf:rpc-reply/netconf:rpc-error/netconf:error-info/netconf:error");
70             String error = (String) XmlUtil.evaluateXPath(xPathExpression, element.getDomElement(), XPathConstants.STRING);
71             if (error!=null && error.contains(ConflictingVersionException.class.getCanonicalName())) {
72                 throw new ConflictingVersionException(error);
73             }
74             throw new IllegalStateException("Can not load last configuration, operation failed: "
75                     + XmlUtil.toString(responseMessage.getDocument()));
76         }
77
78         logger.warn("Can not load last configuration. Operation failed.");
79         throw new IllegalStateException("Can not load last configuration. Operation failed: "
80                 + XmlUtil.toString(responseMessage.getDocument()));
81     }
82 }