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