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.Optional;
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.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.netconf.api.NetconfMessage;
19 import org.opendaylight.netconf.api.xml.XmlElement;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
25 public final class NetconfMessageUtil {
27 private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
29 private NetconfMessageUtil() {}
31 public static boolean isOKMessage(final NetconfMessage message) throws NetconfDocumentedException {
32 return isOKMessage(message.getDocument());
35 public static boolean isOKMessage(final Document document) throws NetconfDocumentedException {
36 return isOKMessage(XmlElement.fromDomDocument(document));
39 public static boolean isOKMessage(final XmlElement xmlElement) throws NetconfDocumentedException {
40 if (xmlElement.getChildElements().size() != 1) {
44 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
45 } catch (DocumentedException e) {
46 throw new NetconfDocumentedException(e);
50 public static boolean isErrorMessage(final NetconfMessage message) throws NetconfDocumentedException {
51 return isErrorMessage(message.getDocument());
54 public static boolean isErrorMessage(final Document document) throws NetconfDocumentedException {
55 return isErrorMessage(XmlElement.fromDomDocument(document));
58 public static boolean isErrorMessage(final XmlElement xmlElement) throws NetconfDocumentedException {
59 if (xmlElement.getChildElements().size() != 1) {
63 return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR);
64 } catch (DocumentedException e) {
65 throw new NetconfDocumentedException(e);
69 public static Collection<String> extractCapabilitiesFromHello(final Document doc)
70 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, (@Nonnull final XmlElement input) -> {
80 // Trim possible leading/tailing whitespace
82 return input.getTextContent().trim();
83 } catch (DocumentedException e) {
84 LOG.trace("Error fetching input text content",e);