Decouple config and netconf subsystems.
[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.base.Optional;
13 import com.google.common.collect.Collections2;
14 import java.util.Collection;
15 import java.util.List;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.config.util.xml.DocumentedException;
18 import org.opendaylight.controller.config.util.xml.XmlElement;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.api.NetconfMessage;
21 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25
26 public final class NetconfMessageUtil {
27
28     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
29
30     private NetconfMessageUtil() {}
31
32     public static boolean isOKMessage(NetconfMessage message) throws NetconfDocumentedException {
33         return isOKMessage(message.getDocument());
34     }
35
36     public static boolean isOKMessage(Document document) throws NetconfDocumentedException {
37         return isOKMessage(XmlElement.fromDomDocument(document));
38     }
39
40     public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException {
41         if(xmlElement.getChildElements().size() != 1) {
42             return false;
43         }
44         try {
45             return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
46         } catch (DocumentedException e) {
47             throw new NetconfDocumentedException(e);
48         }
49     }
50
51     public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException {
52         return isErrorMessage(message.getDocument());
53     }
54
55     public static boolean isErrorMessage(Document document) throws NetconfDocumentedException {
56         return isErrorMessage(XmlElement.fromDomDocument(document));
57     }
58
59     public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException {
60         if(xmlElement.getChildElements().size() != 1) {
61             return false;
62         }
63         try {
64             return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR);
65         } catch (DocumentedException e) {
66             throw new NetconfDocumentedException(e);
67         }
68     }
69
70     public static Collection<String> extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException {
71         XmlElement responseElement = XmlElement.fromDomDocument(doc);
72         // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
73         Optional<XmlElement> capabilitiesElement = responseElement
74                 .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES)
75                 .or(responseElement
76                         .getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES));
77
78         List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
79         return Collections2.transform(caps, new Function<XmlElement, String>() {
80
81             @Override
82             public String apply(@Nonnull XmlElement input) {
83                 // Trim possible leading/tailing whitespace
84                 try {
85                     return input.getTextContent().trim();
86                 } catch (DocumentedException e) {
87                     LOG.trace("Error fetching input text content",e);
88                     return null;
89                 }
90             }
91         });
92
93     }
94 }