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.
26 * @see NetconfHelloMessageAdditionalHeader
28 public final class NetconfHelloMessage extends NetconfMessage {
30 public static final String HELLO_TAG = "hello";
32 private final NetconfHelloMessageAdditionalHeader additionalHeader;
34 public NetconfHelloMessage(final Document doc, final NetconfHelloMessageAdditionalHeader additionalHeader)
35 throws NetconfDocumentedException {
37 checkHelloMessage(doc);
38 this.additionalHeader = additionalHeader;
41 public NetconfHelloMessage(final Document doc) throws NetconfDocumentedException {
45 public Optional<NetconfHelloMessageAdditionalHeader> getAdditionalHeader() {
46 return Optional.fromNullable(additionalHeader);
49 private static void checkHelloMessage(final Document doc) {
50 if (!isHelloMessage(doc)) {
51 throw new IllegalArgumentException(String.format(
52 "Hello message invalid format, should contain %s tag from namespace %s, but is: %s", HELLO_TAG,
53 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlUtil.toString(doc)));
57 public static NetconfHelloMessage createClientHello(final Iterable<String> capabilities,
58 final Optional<NetconfHelloMessageAdditionalHeader> additionalHeaderOptional)
59 throws NetconfDocumentedException {
60 return new NetconfHelloMessage(createHelloMessageDoc(capabilities), additionalHeaderOptional.orNull());
63 private static Document createHelloMessageDoc(final Iterable<String> capabilities) {
64 Document doc = UntrustedXML.newDocumentBuilder().newDocument();
65 Element helloElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
67 Element capabilitiesElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
68 XmlNetconfConstants.CAPABILITIES);
70 for (String capability : Sets.newHashSet(capabilities)) {
71 Element capElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
72 XmlNetconfConstants.CAPABILITY);
73 capElement.setTextContent(capability);
74 capabilitiesElement.appendChild(capElement);
77 helloElement.appendChild(capabilitiesElement);
79 doc.appendChild(helloElement);
83 public static NetconfHelloMessage createServerHello(final Set<String> capabilities, final long sessionId)
84 throws NetconfDocumentedException {
85 Document doc = createHelloMessageDoc(capabilities);
86 Element sessionIdElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
87 XmlNetconfConstants.SESSION_ID);
88 sessionIdElement.setTextContent(Long.toString(sessionId));
89 doc.getDocumentElement().appendChild(sessionIdElement);
90 return new NetconfHelloMessage(doc);
93 public static boolean isHelloMessage(final NetconfMessage msg) {
94 return isHelloMessage(msg.getDocument());
97 private static boolean isHelloMessage(final Document document) {
98 final XmlElement element = XmlElement.fromDomElement(document.getDocumentElement());
99 if (!HELLO_TAG.equals(element.getName())) {
103 final Optional<String> optNamespace = element.getNamespaceOptionally();
104 // accept even if hello has no namespace
105 return !optNamespace.isPresent()
106 || XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(optNamespace.get());