2 * Copyright (c) 2016 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
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.nio.charset.StandardCharsets;
13 import java.time.Instant;
14 import java.time.OffsetDateTime;
15 import java.time.ZoneId;
16 import java.time.format.DateTimeFormatter;
17 import javax.xml.stream.XMLOutputFactory;
18 import javax.xml.stream.XMLStreamException;
19 import javax.xml.stream.XMLStreamWriter;
20 import javax.xml.transform.OutputKeys;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.TransformerException;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.dom.DOMResult;
25 import javax.xml.transform.dom.DOMSource;
26 import javax.xml.transform.stream.StreamResult;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule;
30 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
31 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
32 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
33 import org.opendaylight.yangtools.util.xml.UntrustedXML;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
38 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
47 * Abstract class for processing and preparing data.
50 abstract class AbstractNotificationsData {
51 private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationsData.class);
52 private static final TransformerFactory TF = TransformerFactory.newInstance();
53 private static final XMLOutputFactory OF = XMLOutputFactory.newInstance();
55 private TransactionChainHandler transactionChainHandler;
56 protected SchemaContextHandler schemaHandler;
57 private String localName;
60 * Transaction chain for delete data in DS on close().
62 * @param transactionChainHandler
63 * creating new write transaction for delete data on close
64 * @param schemaHandler
65 * for getting schema to deserialize
66 * {@link MonitoringModule#PATH_TO_STREAM_WITHOUT_KEY} to
67 * {@link YangInstanceIdentifier}
69 public void setCloseVars(final TransactionChainHandler transactionChainHandler,
70 final SchemaContextHandler schemaHandler) {
71 this.transactionChainHandler = transactionChainHandler;
72 this.schemaHandler = schemaHandler;
78 protected void deleteDataInDS() throws Exception {
79 final DOMDataWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
80 wTx.delete(LogicalDatastoreType.OPERATIONAL, IdentifierCodec
81 .deserialize(MonitoringModule.PATH_TO_STREAM_WITHOUT_KEY + this.localName, this.schemaHandler.get()));
82 wTx.submit().checkedGet();
86 * Set localName of last path element of specific listener.
91 protected void setLocalNameOfPath(final String localName) {
92 this.localName = localName;
96 * Formats data specified by RFC3339.
98 * @param now time stamp
99 * @return Data specified by RFC3339.
101 protected static String toRFC3339(final Instant now) {
102 return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OffsetDateTime.ofInstant(now, ZoneId.systemDefault()));
106 * Creates {@link Document} document.
108 * @return {@link Document} document.
110 protected static Document createDocument() {
111 return UntrustedXML.newDocumentBuilder().newDocument();
115 * Write normalized node to {@link DOMResult}.
120 * actual schema context
122 * schema path of data
123 * @return {@link DOMResult}
125 protected DOMResult writeNormalizedNode(final NormalizedNode<?, ?> normalized, final SchemaContext context,
126 final SchemaPath schemaPath) throws IOException, XMLStreamException {
127 final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
128 final DOMResult result = new DOMResult(doc);
129 NormalizedNodeWriter normalizedNodeWriter = null;
130 NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
131 XMLStreamWriter writer = null;
134 writer = OF.createXMLStreamWriter(result);
135 normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
136 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);
138 normalizedNodeWriter.write(normalized);
140 normalizedNodeWriter.flush();
142 if (normalizedNodeWriter != null) {
143 normalizedNodeWriter.close();
145 if (normalizedNodeStreamWriter != null) {
146 normalizedNodeStreamWriter.close();
148 if (writer != null) {
157 * Generating base element of every notification.
160 * base {@link Document}
161 * @return element of {@link Document}
163 protected Element basePartDoc(final Document doc) {
164 final Element notificationElement =
165 doc.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0", "notification");
167 doc.appendChild(notificationElement);
169 final Element eventTimeElement = doc.createElement("eventTime");
170 eventTimeElement.setTextContent(toRFC3339(Instant.now()));
171 notificationElement.appendChild(eventTimeElement);
173 return notificationElement;
177 * Generating of {@link Document} transforming to string.
180 * {@link Document} with data
181 * @return - string from {@link Document}
183 protected String transformDoc(final Document doc) {
184 final ByteArrayOutputStream out = new ByteArrayOutputStream();
187 final Transformer transformer = TF.newTransformer();
188 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
189 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
190 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
191 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
192 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
193 transformer.transform(new DOMSource(doc), new StreamResult(out));
194 } catch (final TransformerException e) {
195 // FIXME: this should raise an exception
196 final String msg = "Error during transformation of Document into String";
201 return new String(out.toByteArray(), StandardCharsets.UTF_8);