2 * Copyright (c) 2014 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.netconf.sal.streams.listeners;
10 import com.google.common.base.Preconditions;
11 import com.google.common.eventbus.AsyncEventBus;
12 import com.google.common.eventbus.EventBus;
13 import com.google.common.eventbus.Subscribe;
14 import io.netty.channel.Channel;
15 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
16 import io.netty.util.internal.ConcurrentSet;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.OutputStreamWriter;
20 import java.io.UnsupportedEncodingException;
21 import java.nio.charset.StandardCharsets;
22 import java.text.SimpleDateFormat;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.HashMap;
27 import java.util.Map.Entry;
28 import java.util.Random;
30 import java.util.concurrent.Executors;
31 import java.util.regex.Pattern;
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import javax.xml.parsers.ParserConfigurationException;
35 import javax.xml.stream.XMLOutputFactory;
36 import javax.xml.stream.XMLStreamException;
37 import javax.xml.stream.XMLStreamWriter;
38 import javax.xml.transform.OutputKeys;
39 import javax.xml.transform.Transformer;
40 import javax.xml.transform.TransformerException;
41 import javax.xml.transform.TransformerFactory;
42 import javax.xml.transform.dom.DOMResult;
43 import javax.xml.transform.dom.DOMSource;
44 import javax.xml.transform.stream.StreamResult;
45 import org.json.JSONObject;
47 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
48 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
49 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
50 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
51 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
58 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
62 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
63 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
64 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
65 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.w3c.dom.Document;
71 import org.w3c.dom.Element;
72 import org.w3c.dom.Node;
75 * {@link ListenerAdapter} is responsible to track events, which occurred by changing data in data source.
77 public class ListenerAdapter implements DOMDataChangeListener {
79 private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapter.class);
80 private static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
81 private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
82 private static final Pattern RFC3339_PATTERN = Pattern.compile("(\\d\\d)(\\d\\d)$");
84 private static final SimpleDateFormat RFC3339 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
86 private final YangInstanceIdentifier path;
87 private ListenerRegistration<DOMDataChangeListener> registration;
88 private final String streamName;
89 private Set<Channel> subscribers = new ConcurrentSet<>();
90 private final EventBus eventBus;
91 private final EventBusChangeRecorder eventBusChangeRecorder;
92 private final NotificationOutputType outputType;
93 private Date start = null;
94 private Date stop = null;
97 * Creates new {@link ListenerAdapter} listener specified by path and stream
101 * Path to data in data store.
103 * The name of the stream.
105 * - type of output on notification (JSON, XML)
107 ListenerAdapter(final YangInstanceIdentifier path, final String streamName,
108 final NotificationOutputType outputType) {
109 this.outputType = outputType;
110 Preconditions.checkNotNull(path);
111 Preconditions.checkArgument((streamName != null) && !streamName.isEmpty());
113 this.streamName = streamName;
114 this.eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
115 this.eventBusChangeRecorder = new EventBusChangeRecorder();
116 this.eventBus.register(this.eventBusChangeRecorder);
120 public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
121 final Date now = new Date();
122 if (this.stop != null) {
123 if ((this.start.compareTo(now) < 0) && (this.stop.compareTo(now) > 0)) {
124 prepareAndPostData(change);
126 if (this.stop.compareTo(now) < 0) {
129 } catch (final Exception e) {
130 throw new RestconfDocumentedException("Problem with unregister listener." + e);
133 } else if (this.start != null) {
134 if (this.start.compareTo(now) < 0) {
136 prepareAndPostData(change);
139 prepareAndPostData(change);
143 private void prepareAndPostData(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
144 if (!change.getCreatedData().isEmpty() || !change.getUpdatedData().isEmpty()
145 || !change.getRemovedPaths().isEmpty()) {
146 final String xml = prepareXmlFrom(change);
147 final Event event = new Event(EventType.NOTIFY);
148 if (this.outputType.equals(NotificationOutputType.JSON)) {
149 final JSONObject jsonObject = XML.toJSONObject(xml);
150 event.setData(jsonObject.toString());
154 this.eventBus.post(event);
159 * Tracks events of data change by customer.
161 private final class EventBusChangeRecorder {
163 public void recordCustomerChange(final Event event) {
164 if (event.getType() == EventType.REGISTER) {
165 final Channel subscriber = event.getSubscriber();
166 if (!ListenerAdapter.this.subscribers.contains(subscriber)) {
167 ListenerAdapter.this.subscribers.add(subscriber);
169 } else if (event.getType() == EventType.DEREGISTER) {
170 ListenerAdapter.this.subscribers.remove(event.getSubscriber());
171 Notificator.removeListenerIfNoSubscriberExists(ListenerAdapter.this);
172 } else if (event.getType() == EventType.NOTIFY) {
173 for (final Channel subscriber : ListenerAdapter.this.subscribers) {
174 if (subscriber.isActive()) {
175 LOG.debug("Data are sent to subscriber {}:", subscriber.remoteAddress());
176 subscriber.writeAndFlush(new TextWebSocketFrame(event.getData()));
178 LOG.debug("Subscriber {} is removed - channel is not active yet.", subscriber.remoteAddress());
179 ListenerAdapter.this.subscribers.remove(subscriber);
187 * Represents event of specific {@link EventType} type, holds data and {@link Channel} subscriber.
189 private final class Event {
190 private final EventType type;
191 private Channel subscriber;
195 * Creates new event specified by {@link EventType} type.
200 public Event(final EventType type) {
205 * Gets the {@link Channel} subscriber.
209 public Channel getSubscriber() {
210 return this.subscriber;
214 * Sets subscriber for event.
219 public void setSubscriber(final Channel subscriber) {
220 this.subscriber = subscriber;
226 * @return String representation of event data.
228 public String getData() {
235 * @param data String.
237 public void setData(final String data) {
244 * @return The type of the event.
246 public EventType getType() {
254 private enum EventType {
261 * Prepare data in printable form and transform it to String.
265 * @return Data in printable form.
267 private String prepareXmlFrom(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
268 final SchemaContext schemaContext = ControllerContext.getInstance().getGlobalSchema();
269 final DataSchemaContextTree dataContextTree = DataSchemaContextTree.from(schemaContext);
270 final Document doc = createDocument();
271 final Element notificationElement = doc.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0",
274 doc.appendChild(notificationElement);
276 final Element eventTimeElement = doc.createElement("eventTime");
277 eventTimeElement.setTextContent(toRFC3339(new Date()));
278 notificationElement.appendChild(eventTimeElement);
280 final Element dataChangedNotificationEventElement = doc.createElementNS(
281 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "data-changed-notification");
283 addValuesToDataChangedNotificationEventElement(doc, dataChangedNotificationEventElement, change,
284 schemaContext, dataContextTree);
285 notificationElement.appendChild(dataChangedNotificationEventElement);
288 final ByteArrayOutputStream out = new ByteArrayOutputStream();
289 final Transformer transformer = FACTORY.newTransformer();
290 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
291 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
292 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
293 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
294 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
295 transformer.transform(new DOMSource(doc),
296 new StreamResult(new OutputStreamWriter(out, StandardCharsets.UTF_8)));
297 final byte[] charData = out.toByteArray();
298 return new String(charData, "UTF-8");
299 } catch (TransformerException | UnsupportedEncodingException e) {
300 final String msg = "Error during transformation of Document into String";
307 * Formats data specified by RFC3339.
311 * @return Data specified by RFC3339.
313 public static String toRFC3339(final Date d) {
314 return RFC3339_PATTERN.matcher(RFC3339.format(d)).replaceAll("$1:$2");
318 * Creates {@link Document} document.
319 * @return {@link Document} document.
321 public static Document createDocument() {
322 final DocumentBuilder bob;
324 bob = DBF.newDocumentBuilder();
325 } catch (final ParserConfigurationException e) {
328 return bob.newDocument();
332 * Adds values to data changed notification event element.
336 * @param dataChangedNotificationEventElement
339 * {@link AsyncDataChangeEvent}
341 private void addValuesToDataChangedNotificationEventElement(final Document doc,
342 final Element dataChangedNotificationEventElement,
343 final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change,
344 final SchemaContext schemaContext, final DataSchemaContextTree dataSchemaContextTree) {
346 addCreatedChangedValuesFromDataToElement(doc, change.getCreatedData().entrySet(),
347 dataChangedNotificationEventElement,
348 Operation.CREATED, schemaContext, dataSchemaContextTree);
350 addCreatedChangedValuesFromDataToElement(doc, change.getUpdatedData().entrySet(),
351 dataChangedNotificationEventElement,
352 Operation.UPDATED, schemaContext, dataSchemaContextTree);
354 addValuesFromDataToElement(doc, change.getRemovedPaths(), dataChangedNotificationEventElement,
359 * Adds values from data to element.
364 * Set of {@link YangInstanceIdentifier}.
370 private void addValuesFromDataToElement(final Document doc, final Set<YangInstanceIdentifier> data,
371 final Element element, final Operation operation) {
372 if ((data == null) || data.isEmpty()) {
375 for (final YangInstanceIdentifier path : data) {
376 if (!ControllerContext.getInstance().isNodeMixin(path)) {
377 final Node node = createDataChangeEventElement(doc, path, operation);
378 element.appendChild(node);
383 private void addCreatedChangedValuesFromDataToElement(final Document doc, final Set<Entry<YangInstanceIdentifier,
384 NormalizedNode<?,?>>> data, final Element element, final Operation operation, final SchemaContext
385 schemaContext, final DataSchemaContextTree dataSchemaContextTree) {
386 if ((data == null) || data.isEmpty()) {
389 for (final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry : data) {
390 if (!ControllerContext.getInstance().isNodeMixin(entry.getKey())) {
391 final Node node = createCreatedChangedDataChangeEventElement(doc, entry, operation, schemaContext,
392 dataSchemaContextTree);
393 element.appendChild(node);
399 * Creates changed event element from data.
404 * Path to data in data store.
407 * @return {@link Node} node represented by changed event element.
409 private Node createDataChangeEventElement(final Document doc, final YangInstanceIdentifier path,
410 final Operation operation) {
411 final Element dataChangeEventElement = doc.createElement("data-change-event");
412 final Element pathElement = doc.createElement("path");
413 addPathAsValueToElement(path, pathElement);
414 dataChangeEventElement.appendChild(pathElement);
416 final Element operationElement = doc.createElement("operation");
417 operationElement.setTextContent(operation.value);
418 dataChangeEventElement.appendChild(operationElement);
420 return dataChangeEventElement;
423 private Node createCreatedChangedDataChangeEventElement(final Document doc, final Entry<YangInstanceIdentifier,
424 NormalizedNode<?, ?>> entry, final Operation operation, final SchemaContext
425 schemaContext, final DataSchemaContextTree dataSchemaContextTree) {
426 final Element dataChangeEventElement = doc.createElement("data-change-event");
427 final Element pathElement = doc.createElement("path");
428 final YangInstanceIdentifier path = entry.getKey();
429 addPathAsValueToElement(path, pathElement);
430 dataChangeEventElement.appendChild(pathElement);
432 final Element operationElement = doc.createElement("operation");
433 operationElement.setTextContent(operation.value);
434 dataChangeEventElement.appendChild(operationElement);
437 final DOMResult domResult = writeNormalizedNode(entry.getValue(), path,
438 schemaContext, dataSchemaContextTree);
439 final Node result = doc.importNode(domResult.getNode().getFirstChild(), true);
440 final Element dataElement = doc.createElement("data");
441 dataElement.appendChild(result);
442 dataChangeEventElement.appendChild(dataElement);
443 } catch (final IOException e) {
444 LOG.error("Error in writer ", e);
445 } catch (final XMLStreamException e) {
446 LOG.error("Error processing stream", e);
449 return dataChangeEventElement;
452 private static DOMResult writeNormalizedNode(final NormalizedNode<?, ?> normalized,
453 final YangInstanceIdentifier path, final SchemaContext context,
454 final DataSchemaContextTree dataSchemaContextTree)
455 throws IOException, XMLStreamException {
456 final XMLOutputFactory XML_FACTORY = XMLOutputFactory.newFactory();
457 final Document doc = XmlDocumentUtils.getDocument();
458 final DOMResult result = new DOMResult(doc);
459 NormalizedNodeWriter normalizedNodeWriter = null;
460 NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
461 XMLStreamWriter writer = null;
462 final SchemaPath nodePath;
464 if ((normalized instanceof MapEntryNode) || (normalized instanceof UnkeyedListEntryNode)) {
465 nodePath = dataSchemaContextTree.getChild(path).getDataSchemaNode().getPath();
467 nodePath = dataSchemaContextTree.getChild(path).getDataSchemaNode().getPath().getParent();
471 writer = XML_FACTORY.createXMLStreamWriter(result);
472 normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context, nodePath);
473 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);
475 normalizedNodeWriter.write(normalized);
477 normalizedNodeWriter.flush();
479 if (normalizedNodeWriter != null) {
480 normalizedNodeWriter.close();
482 if (normalizedNodeStreamWriter != null) {
483 normalizedNodeStreamWriter.close();
485 if (writer != null) {
494 * Adds path as value to element.
497 * Path to data in data store.
501 private void addPathAsValueToElement(final YangInstanceIdentifier path, final Element element) {
502 // Map< key = namespace, value = prefix>
503 final Map<String, String> prefixes = new HashMap<>();
504 final YangInstanceIdentifier normalizedPath = ControllerContext.getInstance().toXpathRepresentation(path);
505 final StringBuilder textContent = new StringBuilder();
507 // FIXME: BUG-1281: this is duplicated code from yangtools (BUG-1275)
508 for (final PathArgument pathArgument : normalizedPath.getPathArguments()) {
509 if (pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier) {
512 textContent.append("/");
513 writeIdentifierWithNamespacePrefix(element, textContent, pathArgument.getNodeType(), prefixes);
514 if (pathArgument instanceof NodeIdentifierWithPredicates) {
515 final Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
516 for (final QName keyValue : predicates.keySet()) {
517 final String predicateValue = String.valueOf(predicates.get(keyValue));
518 textContent.append("[");
519 writeIdentifierWithNamespacePrefix(element, textContent, keyValue, prefixes);
520 textContent.append("='");
521 textContent.append(predicateValue);
522 textContent.append("'");
523 textContent.append("]");
525 } else if (pathArgument instanceof NodeWithValue) {
526 textContent.append("[.='");
527 textContent.append(((NodeWithValue) pathArgument).getValue());
528 textContent.append("'");
529 textContent.append("]");
532 element.setTextContent(textContent.toString());
536 * Writes identifier that consists of prefix and QName.
545 * Map of namespaces and prefixes.
547 private static void writeIdentifierWithNamespacePrefix(final Element element, final StringBuilder textContent,
548 final QName qName, final Map<String, String> prefixes) {
549 final String namespace = qName.getNamespace().toString();
550 String prefix = prefixes.get(namespace);
551 if (prefix == null) {
552 prefix = generateNewPrefix(prefixes.values());
555 element.setAttribute("xmlns:" + prefix, namespace);
556 textContent.append(prefix);
557 prefixes.put(namespace, prefix);
559 textContent.append(":");
560 textContent.append(qName.getLocalName());
564 * Generates new prefix which consists of four random characters <a-z>.
567 * Collection of prefixes.
568 * @return New prefix which consists of four random characters <a-z>.
570 private static String generateNewPrefix(final Collection<String> prefixes) {
571 StringBuilder result = null;
572 final Random random = new Random();
574 result = new StringBuilder();
575 for (int i = 0; i < 4; i++) {
576 final int randomNumber = 0x61 + (Math.abs(random.nextInt()) % 26);
577 result.append(Character.toChars(randomNumber));
579 } while (prefixes.contains(result.toString()));
581 return result.toString();
585 * Gets path pointed to data in data store.
587 * @return Path pointed to data in data store.
589 public YangInstanceIdentifier getPath() {
594 * Sets {@link ListenerRegistration} registration.
596 * @param registration DOMDataChangeListener registration
598 public void setRegistration(final ListenerRegistration<DOMDataChangeListener> registration) {
599 this.registration = registration;
603 * Gets the name of the stream.
605 * @return The name of the stream.
607 public String getStreamName() {
608 return this.streamName;
612 * Removes all subscribers and unregisters event bus change recorder form event bus.
614 public void close() throws Exception {
615 this.subscribers = new ConcurrentSet<>();
616 this.registration.close();
617 this.registration = null;
618 this.eventBus.unregister(this.eventBusChangeRecorder);
622 * Checks if {@link ListenerRegistration} registration exist.
624 * @return True if exist, false otherwise.
626 public boolean isListening() {
627 return this.registration == null ? false : true;
631 * Creates event of type {@link EventType#REGISTER}, set {@link Channel} subscriber to the event and post event into
637 public void addSubscriber(final Channel subscriber) {
638 if (!subscriber.isActive()) {
639 LOG.debug("Channel is not active between websocket server and subscriber {}" + subscriber.remoteAddress());
641 final Event event = new Event(EventType.REGISTER);
642 event.setSubscriber(subscriber);
643 this.eventBus.post(event);
647 * Creates event of type {@link EventType#DEREGISTER}, sets {@link Channel} subscriber to the event and posts event
652 public void removeSubscriber(final Channel subscriber) {
653 LOG.debug("Subscriber {} is removed.", subscriber.remoteAddress());
654 final Event event = new Event(EventType.DEREGISTER);
655 event.setSubscriber(subscriber);
656 this.eventBus.post(event);
660 * Checks if exists at least one {@link Channel} subscriber.
662 * @return True if exist at least one {@link Channel} subscriber, false otherwise.
664 public boolean hasSubscribers() {
665 return !this.subscribers.isEmpty();
669 * Consists of two types {@link Store#CONFIG} and {@link Store#OPERATION}.
671 private static enum Store {
673 OPERATION("operation");
675 private final String value;
677 private Store(final String value) {
683 * Consists of three types {@link Operation#CREATED}, {@link Operation#UPDATED} and {@link Operation#DELETED}.
685 private static enum Operation {
690 private final String value;
692 private Operation(final String value) {
698 * Set query parameters for listener
701 * - start-time of getting notification
703 * - stop-time of getting notification
705 public void setTimer(final Date start, final Date stop) {