2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.util;
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;
22 import javax.xml.xpath.XPathConstants;
23 import javax.xml.xpath.XPathExpression;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
30 public final class NetconfUtil {
32 private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
34 private NetconfUtil() {}
36 public static NetconfMessage createMessage(final File f) {
37 Preconditions.checkNotNull(f, "File parameter was null");
39 return createMessage(new FileInputStream(f));
40 } catch (final FileNotFoundException e) {
41 logger.warn("File {} not found.", f, e);
46 public static NetconfMessage createMessage(final InputStream is) {
47 Preconditions.checkNotNull(is, "InputStream parameter was null");
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);
56 return (doc == null) ? null : new NetconfMessage(doc);
59 public static Document checkIsMessageOk(NetconfMessage responseMessage) throws ConflictingVersionException {
60 return checkIsMessageOk(responseMessage.getDocument());
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();
68 if (element.getName().equals(XmlNetconfConstants.OK)) {
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);
80 throw new IllegalStateException("Can not load last configuration, operation failed: "
81 + XmlUtil.toString(response));
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));