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.collect.Collections2;
13 import java.util.Collection;
14 import java.util.List;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
24 public final class NetconfMessageUtil {
26 private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
28 private NetconfMessageUtil() {}
30 public static boolean isOKMessage(NetconfMessage message) throws NetconfDocumentedException {
31 return isOKMessage(message.getDocument());
34 public static boolean isOKMessage(Document document) throws NetconfDocumentedException {
35 return isOKMessage(XmlElement.fromDomDocument(document));
38 public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException {
39 if(xmlElement.getChildElements().size() != 1) {
42 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
45 public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException {
46 return isErrorMessage(message.getDocument());
49 public static boolean isErrorMessage(Document document) throws NetconfDocumentedException {
50 return isErrorMessage(XmlElement.fromDomDocument(document));
53 public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException {
54 if(xmlElement.getChildElements().size() != 1) {
57 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.RPC_ERROR);
60 public static Collection<String> extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException {
61 XmlElement responseElement = XmlElement.fromDomDocument(doc);
62 XmlElement capabilitiesElement = responseElement
63 .getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CAPABILITIES);
64 List<XmlElement> caps = capabilitiesElement.getChildElements(XmlNetconfConstants.CAPABILITY);
65 return Collections2.transform(caps, new Function<XmlElement, String>() {
68 public String apply(@Nonnull XmlElement input) {
69 // Trim possible leading/tailing whitespace
71 return input.getTextContent().trim();
72 } catch (NetconfDocumentedException e) {
73 LOG.trace("Error fetching input text content",e);