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.controller.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.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.util.xml.XmlElement;
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(NetconfMessage message) throws NetconfDocumentedException {
32 return isOKMessage(message.getDocument());
35 public static boolean isOKMessage(Document document) throws NetconfDocumentedException {
36 return isOKMessage(XmlElement.fromDomDocument(document));
39 public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException {
40 if(xmlElement.getChildElements().size() != 1) {
43 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
46 public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException {
47 return isErrorMessage(message.getDocument());
50 public static boolean isErrorMessage(Document document) throws NetconfDocumentedException {
51 return isErrorMessage(XmlElement.fromDomDocument(document));
54 public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException {
55 if(xmlElement.getChildElements().size() != 1) {
58 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.RPC_ERROR);
61 public static Collection<String> extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException {
62 XmlElement responseElement = XmlElement.fromDomDocument(doc);
63 // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
64 Optional<XmlElement> capabilitiesElement = responseElement
65 .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES)
67 .getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES));
69 List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
70 return Collections2.transform(caps, new Function<XmlElement, String>() {
73 public String apply(@Nonnull XmlElement input) {
74 // Trim possible leading/tailing whitespace
76 return input.getTextContent().trim();
77 } catch (NetconfDocumentedException e) {
78 LOG.trace("Error fetching input text content",e);