Refactor DOMDataBrokerImpl
[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
12 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
14 import org.opendaylight.controller.netconf.util.xml.XmlElement;
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
20 public final class NetconfUtil {
21
22     private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
23
24     private NetconfUtil() {}
25
26     public static Document checkIsMessageOk(Document response) throws NetconfDocumentedException {
27         XmlElement element = XmlElement.fromDomDocument(response);
28         Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
29         element = element.getOnlyChildElement();
30         if (element.getName().equals(XmlNetconfConstants.OK)) {
31             return response;
32         }
33         logger.warn("Can not load last configuration. Operation failed.");
34         throw new IllegalStateException("Can not load last configuration. Operation failed: "
35                 + XmlUtil.toString(response));
36     }
37 }