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;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.w3c.dom.Document;
22 import javax.annotation.Nullable;
24 import java.util.Collection;
25 import java.util.List;
27 public final class NetconfMessageUtil {
29 private static final Logger logger = LoggerFactory.getLogger(NetconfMessageUtil.class);
31 private NetconfMessageUtil() {}
33 public static boolean isOKMessage(NetconfMessage message) throws NetconfDocumentedException {
34 return isOKMessage(message.getDocument());
37 public static boolean isOKMessage(Document document) throws NetconfDocumentedException {
38 return isOKMessage(XmlElement.fromDomDocument(document));
41 public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException {
42 if(xmlElement.getChildElements().size() != 1) {
45 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
48 public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException {
49 return isErrorMessage(message.getDocument());
52 public static boolean isErrorMessage(Document document) throws NetconfDocumentedException {
53 return isErrorMessage(XmlElement.fromDomDocument(document));
56 public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException {
57 if(xmlElement.getChildElements().size() != 1) {
60 return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.RPC_ERROR);
63 public static Collection<String> extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException {
64 XmlElement responseElement = XmlElement.fromDomDocument(doc);
65 XmlElement capabilitiesElement = responseElement
66 .getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CAPABILITIES);
67 List<XmlElement> caps = capabilitiesElement.getChildElements(XmlNetconfConstants.CAPABILITY);
68 return Collections2.transform(caps, new Function<XmlElement, String>() {
72 public String apply(@Nullable XmlElement input) {
73 // Trim possible leading/tailing whitespace
75 return input.getTextContent().trim();
76 } catch (NetconfDocumentedException e) {
77 logger.trace("Error fetching inpit text content becauese {}",e);