Fixed some TODOs in netconf.
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / Util.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
9 package org.opendaylight.controller.netconf.persist.impl;
10
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;
21
22 import javax.xml.xpath.XPathConstants;
23 import javax.xml.xpath.XPathExpression;
24 import java.util.Set;
25
26 public final class Util {
27     private static final Logger logger = LoggerFactory.getLogger(Util.class);
28
29
30     public static boolean isSubset(NetconfClient netconfClient, Set<String> expectedCaps) {
31         return isSubset(netconfClient.getCapabilities(), expectedCaps);
32
33     }
34
35     private static boolean isSubset(Set<String> currentCapabilities, Set<String> expectedCaps) {
36         for (String exCap : expectedCaps) {
37             if (currentCapabilities.contains(exCap) == false)
38                 return false;
39         }
40         return true;
41     }
42
43     public static void closeClientAndDispatcher(NetconfClient client) {
44         NetconfClientDispatcher dispatcher = client.getNetconfClientDispatcher();
45         Exception fromClient = null;
46         try {
47             client.close();
48         } catch (Exception e) {
49             fromClient = e;
50         } finally {
51             try {
52                 dispatcher.close();
53             } catch (Exception e) {
54                 if (fromClient != null) {
55                     e.addSuppressed(fromClient);
56                 }
57                 throw new RuntimeException("Error closing temporary client ", e);
58             }
59         }
60     }
61
62
63     public static void checkIsOk(XmlElement element, NetconfMessage responseMessage) throws ConflictingVersionException {
64         if (element.getName().equals(XmlNetconfConstants.OK)) {
65             return;
66         }
67
68         if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
69             logger.warn("Can not load last configuration, operation failed");
70             // is it ConflictingVersionException ?
71             XPathExpression xPathExpression = XMLNetconfUtil.compileXPath("/netconf:rpc-reply/netconf:rpc-error/netconf:error-info/netconf:error");
72             String error = (String) XmlUtil.evaluateXPath(xPathExpression, element.getDomElement(), XPathConstants.STRING);
73             if (error!=null && error.contains(ConflictingVersionException.class.getCanonicalName())) {
74                 throw new ConflictingVersionException(error);
75             }
76             throw new IllegalStateException("Can not load last configuration, operation failed: "
77                     + XmlUtil.toString(responseMessage.getDocument()));
78         }
79
80         logger.warn("Can not load last configuration. Operation failed.");
81         throw new IllegalStateException("Can not load last configuration. Operation failed: "
82                 + XmlUtil.toString(responseMessage.getDocument()));
83     }
84 }