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