|
| 1 | +package net.juniper.netconf.element; |
| 2 | + |
| 3 | +import lombok.Builder; |
| 4 | +import lombok.Data; |
| 5 | +import lombok.EqualsAndHashCode; |
| 6 | +import lombok.Singular; |
| 7 | +import lombok.ToString; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import net.juniper.netconf.NetconfConstants; |
| 10 | +import org.w3c.dom.Document; |
| 11 | +import org.w3c.dom.Element; |
| 12 | +import org.w3c.dom.Node; |
| 13 | +import org.w3c.dom.NodeList; |
| 14 | +import org.xml.sax.InputSource; |
| 15 | +import org.xml.sax.SAXException; |
| 16 | + |
| 17 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 18 | +import javax.xml.parsers.ParserConfigurationException; |
| 19 | +import javax.xml.transform.OutputKeys; |
| 20 | +import javax.xml.transform.Transformer; |
| 21 | +import javax.xml.transform.TransformerException; |
| 22 | +import javax.xml.transform.TransformerFactory; |
| 23 | +import javax.xml.transform.dom.DOMSource; |
| 24 | +import javax.xml.transform.stream.StreamResult; |
| 25 | +import javax.xml.xpath.XPath; |
| 26 | +import javax.xml.xpath.XPathConstants; |
| 27 | +import javax.xml.xpath.XPathExpressionException; |
| 28 | +import javax.xml.xpath.XPathFactory; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.StringReader; |
| 31 | +import java.io.StringWriter; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +/** |
| 35 | + * Class to represent a NETCONF hello element - https://datatracker.ietf.org/doc/html/rfc6241#section-8.1 |
| 36 | + */ |
| 37 | +@Data |
| 38 | +@Slf4j |
| 39 | +public class Hello { |
| 40 | + |
| 41 | + @ToString.Exclude |
| 42 | + @EqualsAndHashCode.Exclude |
| 43 | + private final Document document; |
| 44 | + |
| 45 | + @ToString.Exclude |
| 46 | + private final String xml; |
| 47 | + |
| 48 | + private final String sessionId; |
| 49 | + |
| 50 | + @Singular("capability") |
| 51 | + private final List<String> capabilities; |
| 52 | + |
| 53 | + public boolean hasCapability(final String capability) { |
| 54 | + return capabilities.contains(capability); |
| 55 | + } |
| 56 | + |
| 57 | + public String toXML() { |
| 58 | + return xml; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a Hello object based on the supplied XML. |
| 63 | + * |
| 64 | + * @param xml The XML of the NETCONF <hello> |
| 65 | + * @return an new, immutable, Hello object. |
| 66 | + * @throws ParserConfigurationException If the XML parser cannot be created |
| 67 | + * @throws IOException If the XML cannot be read |
| 68 | + * @throws SAXException If the XML cannot be parsed |
| 69 | + * @throws XPathExpressionException If there is a problem in the parsing expressions |
| 70 | + */ |
| 71 | + public static Hello from(final String xml) |
| 72 | + throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { |
| 73 | + |
| 74 | + final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 75 | + documentBuilderFactory.setNamespaceAware(true); |
| 76 | + final Document document = documentBuilderFactory.newDocumentBuilder() |
| 77 | + .parse(new InputSource(new StringReader(xml))); |
| 78 | + final XPath xPath = XPathFactory.newInstance().newXPath(); |
| 79 | + final String sessionId = xPath.evaluate("/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='hello']/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='session-id']", document); |
| 80 | + final HelloBuilder builder = Hello.builder() |
| 81 | + .originalDocument(document) |
| 82 | + .sessionId(sessionId); |
| 83 | + final NodeList capabilities = (NodeList) xPath.evaluate("/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='hello']/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='capabilities']/*[namespace-uri()='urn:ietf:params:xml:ns:netconf:base:1.0' and local-name()='capability']", document, XPathConstants.NODESET); |
| 84 | + for (int i = 0; i < capabilities.getLength(); i++) { |
| 85 | + final Node node = capabilities.item(i); |
| 86 | + builder.capability(node.getTextContent()); |
| 87 | + } |
| 88 | + final Hello hello = builder.build(); |
| 89 | + if (log.isInfoEnabled()) { |
| 90 | + log.info("hello is: {}", hello.toXML()); |
| 91 | + } |
| 92 | + return hello; |
| 93 | + } |
| 94 | + |
| 95 | + @Builder |
| 96 | + private Hello( |
| 97 | + final Document originalDocument, |
| 98 | + final String namespacePrefix, |
| 99 | + final String sessionId, |
| 100 | + @Singular("capability") final List<String> capabilities) { |
| 101 | + this.sessionId = sessionId; |
| 102 | + this.capabilities = capabilities; |
| 103 | + if (originalDocument != null) { |
| 104 | + this.document = originalDocument; |
| 105 | + } else { |
| 106 | + this.document = createDocument(namespacePrefix, sessionId, capabilities); |
| 107 | + } |
| 108 | + this.xml = createXml(document); |
| 109 | + } |
| 110 | + |
| 111 | + private static Document createDocument( |
| 112 | + final String namespacePrefix, |
| 113 | + final String sessionId, |
| 114 | + final List<String> capabilities) { |
| 115 | + |
| 116 | + final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 117 | + documentBuilderFactory.setNamespaceAware(true); |
| 118 | + final Document createdDocument; |
| 119 | + try { |
| 120 | + createdDocument = documentBuilderFactory.newDocumentBuilder().newDocument(); |
| 121 | + } catch (final ParserConfigurationException e) { |
| 122 | + throw new IllegalStateException("Unable to create document builder", e); |
| 123 | + } |
| 124 | + |
| 125 | + final Element helloElement |
| 126 | + = createdDocument.createElementNS(NetconfConstants.URN_XML_NS_NETCONF_BASE_1_0, "hello"); |
| 127 | + helloElement.setPrefix(namespacePrefix); |
| 128 | + createdDocument.appendChild(helloElement); |
| 129 | + |
| 130 | + final Element capabilitiesElement |
| 131 | + = createdDocument.createElementNS(NetconfConstants.URN_XML_NS_NETCONF_BASE_1_0, "capabilities"); |
| 132 | + capabilitiesElement.setPrefix(namespacePrefix); |
| 133 | + capabilities.forEach(capability -> { |
| 134 | + final Element capabilityElement = |
| 135 | + createdDocument.createElementNS(NetconfConstants.URN_XML_NS_NETCONF_BASE_1_0, "capability"); |
| 136 | + capabilityElement.setTextContent(capability); |
| 137 | + capabilityElement.setPrefix(namespacePrefix); |
| 138 | + capabilitiesElement.appendChild(capabilityElement); |
| 139 | + }); |
| 140 | + helloElement.appendChild(capabilitiesElement); |
| 141 | + |
| 142 | + if (sessionId != null) { |
| 143 | + final Element sessionIdElement |
| 144 | + = createdDocument.createElementNS(NetconfConstants.URN_XML_NS_NETCONF_BASE_1_0, "session-id"); |
| 145 | + sessionIdElement.setPrefix(namespacePrefix); |
| 146 | + sessionIdElement.setTextContent(sessionId); |
| 147 | + helloElement.appendChild(sessionIdElement); |
| 148 | + } |
| 149 | + return createdDocument; |
| 150 | + } |
| 151 | + |
| 152 | + private static String createXml(final Document document) { |
| 153 | + try { |
| 154 | + final TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 155 | + final Transformer transformer = transformerFactory.newTransformer(); |
| 156 | + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 157 | + final StringWriter stringWriter = new StringWriter(); |
| 158 | + transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); |
| 159 | + return stringWriter.toString(); |
| 160 | + } catch (final TransformerException e) { |
| 161 | + throw new IllegalStateException("Unable to transform document to XML", e); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments