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
8 package org.opendaylight.netconf.util.messages;
10 import com.google.common.collect.Collections2;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.netconf.api.DocumentedException;
15 import org.opendaylight.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.netconf.api.NetconfMessage;
17 import org.opendaylight.netconf.api.xml.XmlElement;
18 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
23 public final class NetconfMessageUtil {
24 private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
26 private NetconfMessageUtil() {
30 public static boolean isOKMessage(final NetconfMessage message) throws NetconfDocumentedException {
31 return isOKMessage(message.getDocument());
34 public static boolean isOKMessage(final Document document) throws NetconfDocumentedException {
35 return isOKMessage(XmlElement.fromDomDocument(document));
38 public static boolean isOKMessage(final XmlElement xmlElement) throws NetconfDocumentedException {
39 if (xmlElement.getChildElements().size() != 1) {
43 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
44 } catch (DocumentedException e) {
45 throw new NetconfDocumentedException(e);
49 public static boolean isErrorMessage(final NetconfMessage message) throws NetconfDocumentedException {
50 return isErrorMessage(message.getDocument());
53 public static boolean isErrorMessage(final Document document) throws NetconfDocumentedException {
54 return isErrorMessage(XmlElement.fromDomDocument(document));
57 public static boolean isErrorMessage(final XmlElement xmlElement) throws NetconfDocumentedException {
58 if (xmlElement.getChildElements().size() != 1) {
62 return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR);
63 } catch (DocumentedException e) {
64 throw new NetconfDocumentedException(e);
68 public static Collection<String> extractCapabilitiesFromHello(final Document doc) {
69 XmlElement responseElement = XmlElement.fromDomDocument(doc);
70 // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
71 Optional<XmlElement> capabilitiesElement = responseElement
72 .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES);
73 if (!capabilitiesElement.isPresent()) {
74 capabilitiesElement = responseElement.getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES);
77 List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
78 return Collections2.transform(caps, input -> {
79 // Trim possible leading/tailing whitespace
81 return input.getTextContent().trim();
82 } catch (DocumentedException e) {
83 LOG.trace("Error fetching input text content",e);