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.netconf.util.messages;
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.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.netconf.api.NetconfMessage;
21 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
26 public final class NetconfMessageUtil {
28 private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
30 private NetconfMessageUtil() {}
32 public static boolean isOKMessage(NetconfMessage message) throws NetconfDocumentedException {
33 return isOKMessage(message.getDocument());
36 public static boolean isOKMessage(Document document) throws NetconfDocumentedException {
37 return isOKMessage(XmlElement.fromDomDocument(document));
40 public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException {
41 if(xmlElement.getChildElements().size() != 1) {
45 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
46 } catch (DocumentedException e) {
47 throw new NetconfDocumentedException(e);
51 public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException {
52 return isErrorMessage(message.getDocument());
55 public static boolean isErrorMessage(Document document) throws NetconfDocumentedException {
56 return isErrorMessage(XmlElement.fromDomDocument(document));
59 public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException {
60 if(xmlElement.getChildElements().size() != 1) {
64 return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR);
65 } catch (DocumentedException e) {
66 throw new NetconfDocumentedException(e);
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)
76 .getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES));
78 List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
79 return Collections2.transform(caps, new Function<XmlElement, String>() {
82 public String apply(@Nonnull XmlElement input) {
83 // Trim possible leading/tailing whitespace
85 return input.getTextContent().trim();
86 } catch (DocumentedException e) {
87 LOG.trace("Error fetching input text content",e);