X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Fmessages%2FNetconfHelloMessage.java;h=15223cb60ba994472cdedc939a6586f569471593;hp=3fd25e814d697d37e607e1e0ade520db254e83a8;hb=87c5228dad99a51479e38d959042b719da3f038a;hpb=9c9f6e506395f806978a955a8cf51ba736b978ad diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfHelloMessage.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfHelloMessage.java index 3fd25e814d..15223cb60b 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfHelloMessage.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfHelloMessage.java @@ -8,17 +8,21 @@ package org.opendaylight.controller.netconf.util.messages; -import com.google.common.base.Optional; -import com.google.common.collect.Sets; -import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.api.NetconfMessage; + +import java.util.Set; + +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; +import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants; +import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException; import org.opendaylight.controller.netconf.util.xml.XmlElement; -import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants; import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.w3c.dom.Document; import org.w3c.dom.Element; -import java.util.Set; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.collect.Sets; /** * NetconfMessage that can carry additional header with session metadata. See {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader} @@ -43,10 +47,10 @@ public final class NetconfHelloMessage extends NetconfMessage { return additionalHeader== null ? Optional.absent() : Optional.of(additionalHeader); } - private static void checkHelloMessage(Document doc) throws NetconfDocumentedException { - XmlElement.fromDomElementWithExpected(doc.getDocumentElement(), HELLO_TAG, - XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); - + private static void checkHelloMessage(Document doc) { + Preconditions.checkArgument(isHelloMessage(doc), + "Hello message invalid format, should contain %s tag from namespace %s, but is: %s", HELLO_TAG, + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlUtil.toString(doc)); } public static NetconfHelloMessage createClientHello(Iterable capabilities, @@ -60,10 +64,12 @@ public final class NetconfHelloMessage extends NetconfMessage { Document doc = XmlUtil.newDocument(); Element helloElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, HELLO_TAG); - Element capabilitiesElement = doc.createElement(XmlNetconfConstants.CAPABILITIES); + Element capabilitiesElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, + XmlNetconfConstants.CAPABILITIES); for (String capability : Sets.newHashSet(capabilities)) { - Element capElement = doc.createElement(XmlNetconfConstants.CAPABILITY); + Element capElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, + XmlNetconfConstants.CAPABILITY); capElement.setTextContent(capability); capabilitiesElement.appendChild(capElement); } @@ -76,9 +82,27 @@ public final class NetconfHelloMessage extends NetconfMessage { public static NetconfHelloMessage createServerHello(Set capabilities, long sessionId) throws NetconfDocumentedException { Document doc = createHelloMessageDoc(capabilities); - Element sessionIdElement = doc.createElement(XmlNetconfConstants.SESSION_ID); + Element sessionIdElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, + XmlNetconfConstants.SESSION_ID); sessionIdElement.setTextContent(Long.toString(sessionId)); doc.getDocumentElement().appendChild(sessionIdElement); return new NetconfHelloMessage(doc); } + + public static boolean isHelloMessage(final NetconfMessage msg) { + Document document = msg.getDocument(); + return isHelloMessage(document); + } + + private static boolean isHelloMessage(final Document document) { + XmlElement element = XmlElement.fromDomElement(document.getDocumentElement()); + try { + return element.getName().equals(HELLO_TAG) && + element.hasNamespace() && + element.getNamespace().equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); + } catch (MissingNameSpaceException e) { + // Cannot happen, since we check for hasNamespace + throw new IllegalStateException(e); + } + } }