X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fmessages%2FNetconfMessageUtil.java;h=354d74016d48f170321bfca9e9d5886a5a03b851;hb=refs%2Fchanges%2F13%2F23413%2F26;hp=46053e734ebf006154e26d5c8b7564ff6a36eedd;hpb=a92d9d6a21a0f6ca8d2153795721f500eaf29ee9;p=controller.git diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageUtil.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageUtil.java index 46053e734e..354d74016d 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageUtil.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageUtil.java @@ -8,35 +8,87 @@ package org.opendaylight.controller.netconf.util.messages; +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.collect.Collections2; +import java.util.Collection; +import java.util.List; +import javax.annotation.Nonnull; +import org.opendaylight.controller.config.util.xml.DocumentedException; +import org.opendaylight.controller.config.util.xml.XmlElement; +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.api.NetconfMessage; -import org.opendaylight.controller.netconf.util.xml.XmlElement; -import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants; +import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; -public class NetconfMessageUtil { +public final class NetconfMessageUtil { - public static boolean isOKMessage(NetconfMessage message) { + private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class); + + private NetconfMessageUtil() {} + + public static boolean isOKMessage(NetconfMessage message) throws NetconfDocumentedException { return isOKMessage(message.getDocument()); } - public static boolean isOKMessage(Document document) { + public static boolean isOKMessage(Document document) throws NetconfDocumentedException { return isOKMessage(XmlElement.fromDomDocument(document)); } - public static boolean isOKMessage(XmlElement xmlElement) { - return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK); + public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException { + if(xmlElement.getChildElements().size() != 1) { + return false; + } + try { + return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK); + } catch (DocumentedException e) { + throw new NetconfDocumentedException(e); + } } - public static boolean isErrorMEssage(NetconfMessage message) { + public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException { return isErrorMessage(message.getDocument()); } - public static boolean isErrorMessage(Document document) { + public static boolean isErrorMessage(Document document) throws NetconfDocumentedException { return isErrorMessage(XmlElement.fromDomDocument(document)); } - public static boolean isErrorMessage(XmlElement xmlElement) { - return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.RPC_ERROR); + public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException { + if(xmlElement.getChildElements().size() != 1) { + return false; + } + try { + return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR); + } catch (DocumentedException e) { + throw new NetconfDocumentedException(e); + } + } + + public static Collection extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException { + XmlElement responseElement = XmlElement.fromDomDocument(doc); + // Extract child element from with or without(fallback) the same namespace + Optional capabilitiesElement = responseElement + .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES) + .or(responseElement + .getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES)); + + List caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY); + return Collections2.transform(caps, new Function() { + + @Override + public String apply(@Nonnull XmlElement input) { + // Trim possible leading/tailing whitespace + try { + return input.getTextContent().trim(); + } catch (DocumentedException e) { + LOG.trace("Error fetching input text content",e); + return null; + } + } + }); } }