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.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;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
26 public final class NetconfUtil {
28 private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
30 private NetconfUtil() {}
32 public static NetconfMessage createMessage(final File f) {
33 Preconditions.checkNotNull(f, "File parameter was null");
35 return createMessage(new FileInputStream(f));
36 } catch (final FileNotFoundException e) {
37 logger.warn("File {} not found.", f, e);
42 public static NetconfMessage createMessage(final InputStream is) {
43 Preconditions.checkNotNull(is, "InputStream parameter was null");
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);
52 return (doc == null) ? null : new NetconfMessage(doc);
55 public static Document checkIsMessageOk(NetconfMessage responseMessage) {
56 return checkIsMessageOk(responseMessage.getDocument());
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)) {
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));