Merge "Do not force toString() in NetconfClientSession"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / NetconfHelloMessage.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.util.messages;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Sets;
14 import java.util.Set;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 /**
25  * NetconfMessage that can carry additional header with session metadata. See {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader}
26  */
27 public final class NetconfHelloMessage extends NetconfMessage {
28
29     public static final String HELLO_TAG = "hello";
30
31     private final NetconfHelloMessageAdditionalHeader additionalHeader;
32
33     public NetconfHelloMessage(Document doc, NetconfHelloMessageAdditionalHeader additionalHeader) throws NetconfDocumentedException {
34         super(doc);
35         checkHelloMessage(doc);
36         this.additionalHeader = additionalHeader;
37     }
38
39     public NetconfHelloMessage(Document doc) throws NetconfDocumentedException {
40         this(doc, null);
41     }
42
43     public Optional<NetconfHelloMessageAdditionalHeader> getAdditionalHeader() {
44         return additionalHeader== null ? Optional.<NetconfHelloMessageAdditionalHeader>absent() : Optional.of(additionalHeader);
45     }
46
47     private static void checkHelloMessage(Document doc) {
48         Preconditions.checkArgument(isHelloMessage(doc),
49                 "Hello message invalid format, should contain %s tag from namespace %s, but is: %s", HELLO_TAG,
50                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlUtil.toString(doc));
51     }
52
53     public static NetconfHelloMessage createClientHello(Iterable<String> capabilities,
54                                                         Optional<NetconfHelloMessageAdditionalHeader> additionalHeaderOptional) throws NetconfDocumentedException {
55         Document doc = createHelloMessageDoc(capabilities);
56         return additionalHeaderOptional.isPresent() ? new NetconfHelloMessage(doc, additionalHeaderOptional.get())
57                 : new NetconfHelloMessage(doc);
58     }
59
60     private static Document createHelloMessageDoc(Iterable<String> capabilities) {
61         Document doc = XmlUtil.newDocument();
62         Element helloElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
63                 HELLO_TAG);
64         Element capabilitiesElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
65                 XmlNetconfConstants.CAPABILITIES);
66
67         for (String capability : Sets.newHashSet(capabilities)) {
68             Element capElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
69                     XmlNetconfConstants.CAPABILITY);
70             capElement.setTextContent(capability);
71             capabilitiesElement.appendChild(capElement);
72         }
73
74         helloElement.appendChild(capabilitiesElement);
75
76         doc.appendChild(helloElement);
77         return doc;
78     }
79
80     public static NetconfHelloMessage createServerHello(Set<String> capabilities, long sessionId) throws NetconfDocumentedException {
81         Document doc = createHelloMessageDoc(capabilities);
82         Element sessionIdElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
83                 XmlNetconfConstants.SESSION_ID);
84         sessionIdElement.setTextContent(Long.toString(sessionId));
85         doc.getDocumentElement().appendChild(sessionIdElement);
86         return new NetconfHelloMessage(doc);
87     }
88
89     public static boolean isHelloMessage(final NetconfMessage msg) {
90         Document document = msg.getDocument();
91         return isHelloMessage(document);
92     }
93
94     private static boolean isHelloMessage(final Document document) {
95         XmlElement element = XmlElement.fromDomElement(document.getDocumentElement());
96         try {
97             return element.getName().equals(HELLO_TAG) &&
98                    element.hasNamespace() &&
99                    element.getNamespace().equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
100         } catch (MissingNameSpaceException e) {
101             // Cannot happen, since we check for hasNamespace
102             throw new IllegalStateException(e);
103         }
104     }
105 }