Merge "Bug 8153: Enforce check-style on netconf-impl"
[netconf.git] / netconf / netconf-api / src / main / java / org / opendaylight / netconf / api / 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.netconf.api.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.config.util.xml.DocumentedException;
16 import org.opendaylight.controller.config.util.xml.XmlElement;
17 import org.opendaylight.controller.config.util.xml.XmlUtil;
18 import org.opendaylight.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.netconf.api.NetconfMessage;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 /**
25  * NetconfMessage that can carry additional header with session metadata.
26  * See {@link NetconfHelloMessageAdditionalHeader}
27  */
28 public final class NetconfHelloMessage extends NetconfMessage {
29
30     public static final String HELLO_TAG = "hello";
31
32     private final NetconfHelloMessageAdditionalHeader additionalHeader;
33
34     public NetconfHelloMessage(Document doc, NetconfHelloMessageAdditionalHeader additionalHeader)
35             throws NetconfDocumentedException {
36         super(doc);
37         checkHelloMessage(doc);
38         this.additionalHeader = additionalHeader;
39     }
40
41     public NetconfHelloMessage(Document doc) throws NetconfDocumentedException {
42         this(doc, null);
43     }
44
45     public Optional<NetconfHelloMessageAdditionalHeader> getAdditionalHeader() {
46         return additionalHeader == null ? Optional.<NetconfHelloMessageAdditionalHeader>absent()
47                 : Optional.of(additionalHeader);
48     }
49
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));
54     }
55
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);
61     }
62
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,
66                 HELLO_TAG);
67         Element capabilitiesElement = doc.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
68                 XmlNetconfConstants.CAPABILITIES);
69
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);
75         }
76
77         helloElement.appendChild(capabilitiesElement);
78
79         doc.appendChild(helloElement);
80         return doc;
81     }
82
83     public static NetconfHelloMessage createServerHello(Set<String> capabilities, 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);
91     }
92
93     public static boolean isHelloMessage(final NetconfMessage msg) {
94         Document document = msg.getDocument();
95         return isHelloMessage(document);
96     }
97
98     private static boolean isHelloMessage(final Document document) {
99         XmlElement element = XmlElement.fromDomElement(document.getDocumentElement());
100         try {
101             // accept even if hello has no namespace
102             return element.getName().equals(HELLO_TAG)
103                     && (!element.hasNamespace()
104                     || element.getNamespace().equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
105         } catch (DocumentedException e) {
106             // Cannot happen, since we check for hasNamespace
107             throw new IllegalStateException(e);
108         }
109     }
110 }