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