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
9 package org.opendaylight.controller.netconf.persist.impl;
11 import org.opendaylight.controller.config.api.ConflictingVersionException;
12 import org.opendaylight.controller.netconf.api.NetconfMessage;
13 import org.opendaylight.controller.netconf.client.NetconfClient;
14 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
15 import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil;
16 import org.opendaylight.controller.netconf.util.xml.XmlElement;
17 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import javax.xml.xpath.XPathConstants;
23 import javax.xml.xpath.XPathExpression;
26 public final class Util {
27 private static final Logger logger = LoggerFactory.getLogger(Util.class);
30 public static boolean isSubset(NetconfClient netconfClient, Set<String> expectedCaps) {
31 return isSubset(netconfClient.getCapabilities(), expectedCaps);
35 private static boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
36 for (String exCap : expectedCaps) {
37 if (currentCapabilities.contains(exCap) == false)
44 // TODO: check if closing in correct order
45 public static void closeClientAndDispatcher(NetconfClient client) {
46 NetconfClientDispatcher dispatcher = client.getNetconfClientDispatcher();
47 Exception fromClient = null;
50 } catch (Exception e) {
55 } catch (Exception e) {
56 if (fromClient != null) {
57 e.addSuppressed(fromClient);
59 throw new RuntimeException("Error closing temporary client ", e);
65 public static void checkIsOk(XmlElement element, NetconfMessage responseMessage) throws ConflictingVersionException {
66 if (element.getName().equals(XmlNetconfConstants.OK)) {
70 if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
71 logger.warn("Can not load last configuration, operation failed");
72 // is it ConflictingVersionException ?
73 XPathExpression xPathExpression = XMLNetconfUtil.compileXPath("/netconf:rpc-reply/netconf:rpc-error/netconf:error-info/netconf:error");
74 String error = (String) XmlUtil.evaluateXPath(xPathExpression, element.getDomElement(), XPathConstants.STRING);
75 if (error!=null && error.contains(ConflictingVersionException.class.getCanonicalName())) {
76 throw new ConflictingVersionException(error);
78 throw new IllegalStateException("Can not load last configuration, operation failed: "
79 + XmlUtil.toString(responseMessage.getDocument()));
82 logger.warn("Can not load last configuration. Operation failed.");
83 throw new IllegalStateException("Can not load last configuration. Operation failed: "
84 + XmlUtil.toString(responseMessage.getDocument()));