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.netconf.api.messages;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Sets;
14 import org.opendaylight.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.netconf.api.NetconfMessage;
16 import org.opendaylight.netconf.api.xml.XmlElement;
17 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.netconf.api.xml.XmlUtil;
19 import org.opendaylight.yangtools.util.xml.UntrustedXML;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
24 * NetconfMessage that can carry additional header with session metadata.
25 * See {@link NetconfHelloMessageAdditionalHeader}
27 public final class NetconfHelloMessage extends NetconfMessage {
29 public static final String HELLO_TAG = "hello";
31 private final NetconfHelloMessageAdditionalHeader additionalHeader;
33 public NetconfHelloMessage(final Document doc, final NetconfHelloMessageAdditionalHeader additionalHeader)
34 throws NetconfDocumentedException {
36 checkHelloMessage(doc);
37 this.additionalHeader = additionalHeader;
40 public NetconfHelloMessage(final Document doc) throws NetconfDocumentedException {
44 public Optional<NetconfHelloMessageAdditionalHeader> getAdditionalHeader() {
45 return Optional.fromNullable(additionalHeader);
48 private static void checkHelloMessage(final Document doc) {
49 if (!isHelloMessage(doc)) {
50 throw new IllegalArgumentException(String.format(
51 "Hello message invalid format, should contain %s tag from namespace %s, but is: %s", HELLO_TAG,
52 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlUtil.toString(doc)));
56 public static NetconfHelloMessage createClientHello(final Iterable<String> capabilities,
57 final Optional<NetconfHelloMessageAdditionalHeader> additionalHeaderOptional)
58 throws NetconfDocumentedException {
59 return new NetconfHelloMessage(createHelloMessageDoc(capabilities), additionalHeaderOptional.orNull());
62 private static Document createHelloMessageDoc(final Iterable<String> capabilities) {
63 Document doc = UntrustedXML.newDocumentBuilder().newDocument();
64 Element helloElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
66 Element capabilitiesElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
67 XmlNetconfConstants.CAPABILITIES);
69 for (String capability : Sets.newHashSet(capabilities)) {
70 Element capElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
71 XmlNetconfConstants.CAPABILITY);
72 capElement.setTextContent(capability);
73 capabilitiesElement.appendChild(capElement);
76 helloElement.appendChild(capabilitiesElement);
78 doc.appendChild(helloElement);
82 public static NetconfHelloMessage createServerHello(final Set<String> capabilities, final long sessionId)
83 throws NetconfDocumentedException {
84 Document doc = createHelloMessageDoc(capabilities);
85 Element sessionIdElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
86 XmlNetconfConstants.SESSION_ID);
87 sessionIdElement.setTextContent(Long.toString(sessionId));
88 doc.getDocumentElement().appendChild(sessionIdElement);
89 return new NetconfHelloMessage(doc);
92 public static boolean isHelloMessage(final NetconfMessage msg) {
93 return isHelloMessage(msg.getDocument());
96 private static boolean isHelloMessage(final Document document) {
97 final XmlElement element = XmlElement.fromDomElement(document.getDocumentElement());
98 if (!HELLO_TAG.equals(element.getName())) {
102 final Optional<String> optNamespace = element.getNamespaceOptionally();
103 // accept even if hello has no namespace
104 return !optNamespace.isPresent()
105 || XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(optNamespace.get());