Merge "Add message to update the schema context of the InMemoryDOMDataStore"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / NetconfMessageUtil.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.util.messages;
10
11 import com.google.common.base.Function;
12 import com.google.common.collect.Collections2;
13
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.w3c.dom.Document;
21
22 import javax.annotation.Nullable;
23
24 import java.util.Collection;
25 import java.util.List;
26
27 public final class NetconfMessageUtil {
28
29     private static final Logger logger = LoggerFactory.getLogger(NetconfMessageUtil.class);
30
31     private NetconfMessageUtil() {}
32
33     public static boolean isOKMessage(NetconfMessage message) throws NetconfDocumentedException {
34         return isOKMessage(message.getDocument());
35     }
36
37     public static boolean isOKMessage(Document document) throws NetconfDocumentedException {
38         return isOKMessage(XmlElement.fromDomDocument(document));
39     }
40
41     public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException {
42         if(xmlElement.getChildElements().size() != 1) {
43             return false;
44         }
45         return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
46     }
47
48     public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException {
49         return isErrorMessage(message.getDocument());
50     }
51
52     public static boolean isErrorMessage(Document document) throws NetconfDocumentedException {
53         return isErrorMessage(XmlElement.fromDomDocument(document));
54     }
55
56     public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException {
57         if(xmlElement.getChildElements().size() != 1) {
58             return false;
59         }
60         return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.RPC_ERROR);
61     }
62
63     public static Collection<String> extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException {
64         XmlElement responseElement = XmlElement.fromDomDocument(doc);
65         XmlElement capabilitiesElement = responseElement
66                 .getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CAPABILITIES);
67         List<XmlElement> caps = capabilitiesElement.getChildElements(XmlNetconfConstants.CAPABILITY);
68         return Collections2.transform(caps, new Function<XmlElement, String>() {
69
70             @Nullable
71             @Override
72             public String apply(@Nullable XmlElement input) {
73                 // Trim possible leading/tailing whitespace
74                 try {
75                     return input.getTextContent().trim();
76                 } catch (NetconfDocumentedException e) {
77                     logger.trace("Error fetching inpit text content becauese {}",e);
78                     return null;
79                 }
80             }
81         });
82
83     }
84 }