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 org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
18 import org.opendaylight.controller.netconf.util.xml.XmlElement;
19 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
25 import com.google.common.collect.Sets;
28 * NetconfMessage that can carry additional header with session metadata. See {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader}
30 public final class NetconfHelloMessage extends NetconfMessage {
32 public static final String HELLO_TAG = "hello";
34 private final NetconfHelloMessageAdditionalHeader additionalHeader;
36 public NetconfHelloMessage(Document doc, NetconfHelloMessageAdditionalHeader additionalHeader) throws NetconfDocumentedException {
38 checkHelloMessage(doc);
39 this.additionalHeader = additionalHeader;
42 public NetconfHelloMessage(Document doc) throws NetconfDocumentedException {
46 public Optional<NetconfHelloMessageAdditionalHeader> getAdditionalHeader() {
47 return additionalHeader== null ? Optional.<NetconfHelloMessageAdditionalHeader>absent() : Optional.of(additionalHeader);
50 private static void checkHelloMessage(Document doc) {
51 Preconditions.checkArgument(isHelloMessage(doc),
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));
56 public static NetconfHelloMessage createClientHello(Iterable<String> capabilities,
57 Optional<NetconfHelloMessageAdditionalHeader> additionalHeaderOptional) throws NetconfDocumentedException {
58 Document doc = createHelloMessageDoc(capabilities);
59 return additionalHeaderOptional.isPresent() ? new NetconfHelloMessage(doc, additionalHeaderOptional.get())
60 : new NetconfHelloMessage(doc);
63 private static Document createHelloMessageDoc(Iterable<String> capabilities) {
64 Document doc = XmlUtil.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(Set<String> capabilities, long sessionId) 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 Document document = msg.getDocument();
94 return isHelloMessage(document);
97 private static boolean isHelloMessage(final Document document) {
98 XmlElement element = XmlElement.fromDomElement(document.getDocumentElement());
100 return element.getName().equals(HELLO_TAG) &&
101 element.hasNamespace() &&
102 element.getNamespace().equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
103 } catch (MissingNameSpaceException e) {
104 // Cannot happen, since we check for hasNamespace
105 throw new IllegalStateException(e);