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 com.google.common.base.Optional;
12 import com.google.common.collect.Sets;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.netconf.util.xml.XmlElement;
16 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Element;
24 * NetconfMessage that can carry additional header with session metadata. See {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader}
26 public final class NetconfHelloMessage extends NetconfMessage {
28 public static final String HELLO_TAG = "hello";
30 private final NetconfHelloMessageAdditionalHeader additionalHeader;
32 public NetconfHelloMessage(Document doc, NetconfHelloMessageAdditionalHeader additionalHeader) throws NetconfDocumentedException {
34 checkHelloMessage(doc);
35 this.additionalHeader = additionalHeader;
38 public NetconfHelloMessage(Document doc) throws NetconfDocumentedException {
42 public Optional<NetconfHelloMessageAdditionalHeader> getAdditionalHeader() {
43 return additionalHeader== null ? Optional.<NetconfHelloMessageAdditionalHeader>absent() : Optional.of(additionalHeader);
46 private static void checkHelloMessage(Document doc) throws NetconfDocumentedException {
47 XmlElement.fromDomElementWithExpected(doc.getDocumentElement(), HELLO_TAG,
48 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
52 public static NetconfHelloMessage createClientHello(Iterable<String> capabilities,
53 Optional<NetconfHelloMessageAdditionalHeader> additionalHeaderOptional) throws NetconfDocumentedException {
54 Document doc = createHelloMessageDoc(capabilities);
55 return additionalHeaderOptional.isPresent() ? new NetconfHelloMessage(doc, additionalHeaderOptional.get())
56 : new NetconfHelloMessage(doc);
59 private static Document createHelloMessageDoc(Iterable<String> capabilities) {
60 Document doc = XmlUtil.newDocument();
61 Element helloElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
63 Element capabilitiesElement = doc.createElement(XmlNetconfConstants.CAPABILITIES);
65 for (String capability : Sets.newHashSet(capabilities)) {
66 Element capElement = doc.createElement(XmlNetconfConstants.CAPABILITY);
67 capElement.setTextContent(capability);
68 capabilitiesElement.appendChild(capElement);
71 helloElement.appendChild(capabilitiesElement);
73 doc.appendChild(helloElement);
77 public static NetconfHelloMessage createServerHello(Set<String> capabilities, long sessionId) throws NetconfDocumentedException {
78 Document doc = createHelloMessageDoc(capabilities);
79 Element sessionIdElement = doc.createElement(XmlNetconfConstants.SESSION_ID);
80 sessionIdElement.setTextContent(Long.toString(sessionId));
81 doc.getDocumentElement().appendChild(sessionIdElement);
82 return new NetconfHelloMessage(doc);