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 {
59 // In the case of multiple rpc-error messages, size will not be 1 but we still want to report as Error
60 if (xmlElement.getChildElements().size() != 1) {
61 List<XmlElement> allResults = xmlElement.getChildElements();
62 for (XmlElement result : allResults) {
63 if (result.getName().equals(DocumentedException.RPC_ERROR)) {
70 return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR);
71 } catch (DocumentedException e) {
72 throw new NetconfDocumentedException(e);
76 public static Collection<String> extractCapabilitiesFromHello(final Document doc) {
77 XmlElement responseElement = XmlElement.fromDomDocument(doc);
78 // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
79 Optional<XmlElement> capabilitiesElement = responseElement
80 .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES);
81 if (capabilitiesElement.isEmpty()) {
82 capabilitiesElement = responseElement.getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES);
85 List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
86 return Collections2.transform(caps, input -> {
87 // Trim possible leading/tailing whitespace
89 return input.getTextContent().trim();
90 } catch (DocumentedException e) {
91 LOG.trace("Error fetching input text content",e);