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.Charsets;
11 import com.google.common.base.Preconditions;
12 import com.google.common.eventbus.AsyncEventBus;
13 import com.google.common.eventbus.EventBus;
14 import com.google.common.eventbus.Subscribe;
15 import io.netty.channel.Channel;
16 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
17 import io.netty.util.internal.ConcurrentSet;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.OutputStreamWriter;
21 import java.io.UnsupportedEncodingException;
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.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
46 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
47 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
58 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
59 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
60 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
61 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
62 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
63 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66 import org.w3c.dom.Document;
67 import org.w3c.dom.Element;
68 import org.w3c.dom.Node;
71 * {@link ListenerAdapter} is responsible to track events, which occurred by changing data in data source.
73 public class ListenerAdapter implements DOMDataChangeListener {
75 private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapter.class);
76 private static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
77 private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
78 private static final Pattern RFC3339_PATTERN = Pattern.compile("(\\d\\d)(\\d\\d)$");
80 private final SimpleDateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
82 private final YangInstanceIdentifier path;
83 private ListenerRegistration<DOMDataChangeListener> registration;
84 private final String streamName;
85 private Set<Channel> subscribers = new ConcurrentSet<>();
86 private final EventBus eventBus;
87 private final EventBusChangeRecorder eventBusChangeRecorder;
90 * Creates new {@link ListenerAdapter} listener specified by path and stream name.
93 * Path to data in data store.
95 * The name of the stream.
97 ListenerAdapter(final YangInstanceIdentifier path, final String streamName) {
98 Preconditions.checkNotNull(path);
99 Preconditions.checkArgument(streamName != null && !streamName.isEmpty());
101 this.streamName = streamName;
102 eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
103 eventBusChangeRecorder = new EventBusChangeRecorder();
104 eventBus.register(eventBusChangeRecorder);
108 public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
109 if (!change.getCreatedData().isEmpty() || !change.getUpdatedData().isEmpty()
110 || !change.getRemovedPaths().isEmpty()) {
111 final String xml = prepareXmlFrom(change);
112 final Event event = new Event(EventType.NOTIFY);
114 eventBus.post(event);
119 * Tracks events of data change by customer.
121 private final class EventBusChangeRecorder {
123 public void recordCustomerChange(final Event event) {
124 if (event.getType() == EventType.REGISTER) {
125 final Channel subscriber = event.getSubscriber();
126 if (!subscribers.contains(subscriber)) {
127 subscribers.add(subscriber);
129 } else if (event.getType() == EventType.DEREGISTER) {
130 subscribers.remove(event.getSubscriber());
131 Notificator.removeListenerIfNoSubscriberExists(ListenerAdapter.this);
132 } else if (event.getType() == EventType.NOTIFY) {
133 for (final Channel subscriber : subscribers) {
134 if (subscriber.isActive()) {
135 LOG.debug("Data are sent to subscriber {}:", subscriber.remoteAddress());
136 subscriber.writeAndFlush(new TextWebSocketFrame(event.getData()));
138 LOG.debug("Subscriber {} is removed - channel is not active yet.", subscriber.remoteAddress());
139 subscribers.remove(subscriber);
147 * Represents event of specific {@link EventType} type, holds data and {@link Channel} subscriber.
149 private final class Event {
150 private final EventType type;
151 private Channel subscriber;
155 * Creates new event specified by {@link EventType} type.
160 public Event(final EventType type) {
165 * Gets the {@link Channel} subscriber.
169 public Channel getSubscriber() {
174 * Sets subscriber for event.
179 public void setSubscriber(final Channel subscriber) {
180 this.subscriber = subscriber;
186 * @return String representation of event data.
188 public String getData() {
195 * @param data String.
197 public void setData(final String data) {
204 * @return The type of the event.
206 public EventType getType() {
214 private enum EventType {
221 * Prepare data in printable form and transform it to String.
225 * @return Data in printable form.
227 private String prepareXmlFrom(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
228 final SchemaContext schemaContext = ControllerContext.getInstance().getGlobalSchema();
229 final DataSchemaContextTree dataContextTree = DataSchemaContextTree.from(schemaContext);
230 final Document doc = createDocument();
231 final Element notificationElement = doc.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0",
233 doc.appendChild(notificationElement);
235 final Element eventTimeElement = doc.createElement("eventTime");
236 eventTimeElement.setTextContent(toRFC3339(new Date()));
237 notificationElement.appendChild(eventTimeElement);
239 final Element dataChangedNotificationEventElement = doc.createElementNS(
240 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "data-changed-notification");
241 addValuesToDataChangedNotificationEventElement(doc, dataChangedNotificationEventElement, change,
242 schemaContext, dataContextTree);
243 notificationElement.appendChild(dataChangedNotificationEventElement);
246 final ByteArrayOutputStream out = new ByteArrayOutputStream();
247 final Transformer transformer = FACTORY.newTransformer();
248 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
249 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
250 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
251 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
252 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
253 transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, Charsets.UTF_8)));
254 final byte[] charData = out.toByteArray();
255 return new String(charData, "UTF-8");
256 } catch (TransformerException | UnsupportedEncodingException e) {
257 final String msg = "Error during transformation of Document into String";
264 * Formats data specified by RFC3339.
268 * @return Data specified by RFC3339.
270 private String toRFC3339(final Date d) {
271 return RFC3339_PATTERN.matcher(rfc3339.format(d)).replaceAll("$1:$2");
275 * Creates {@link Document} document.
276 * @return {@link Document} document.
278 private Document createDocument() {
279 final DocumentBuilder bob;
281 bob = DBF.newDocumentBuilder();
282 } catch (final ParserConfigurationException e) {
285 return bob.newDocument();
289 * Adds values to data changed notification event element.
293 * @param dataChangedNotificationEventElement
296 * {@link AsyncDataChangeEvent}
298 private void addValuesToDataChangedNotificationEventElement(final Document doc,
299 final Element dataChangedNotificationEventElement,
300 final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change,
301 final SchemaContext schemaContext, final DataSchemaContextTree dataSchemaContextTree) {
303 addCreatedChangedValuesFromDataToElement(doc, change.getCreatedData().entrySet(),
304 dataChangedNotificationEventElement,
305 Operation.CREATED, schemaContext, dataSchemaContextTree);
307 addCreatedChangedValuesFromDataToElement(doc, change.getUpdatedData().entrySet(),
308 dataChangedNotificationEventElement,
309 Operation.UPDATED, schemaContext, dataSchemaContextTree);
311 addValuesFromDataToElement(doc, change.getRemovedPaths(), dataChangedNotificationEventElement,
316 * Adds values from data to element.
321 * Set of {@link YangInstanceIdentifier}.
327 private void addValuesFromDataToElement(final Document doc, final Set<YangInstanceIdentifier> data, final Element element,
328 final Operation operation) {
329 if (data == null || data.isEmpty()) {
332 for (final YangInstanceIdentifier path : data) {
333 if (!ControllerContext.getInstance().isNodeMixin(path)) {
334 final Node node = createDataChangeEventElement(doc, path, operation);
335 element.appendChild(node);
340 private void addCreatedChangedValuesFromDataToElement(final Document doc, final Set<Entry<YangInstanceIdentifier,
341 NormalizedNode<?,?>>> data, final Element element, final Operation operation, final SchemaContext
342 schemaContext, final DataSchemaContextTree dataSchemaContextTree) {
343 if (data == null || data.isEmpty()) {
346 for (Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry : data) {
347 if (!ControllerContext.getInstance().isNodeMixin(entry.getKey())) {
348 final Node node = createCreatedChangedDataChangeEventElement(doc, entry, operation, schemaContext,
349 dataSchemaContextTree);
350 element.appendChild(node);
356 * Creates changed event element from data.
361 * Path to data in data store.
364 * @return {@link Node} node represented by changed event element.
366 private Node createDataChangeEventElement(final Document doc, final YangInstanceIdentifier path, final Operation operation) {
367 final Element dataChangeEventElement = doc.createElement("data-change-event");
368 final Element pathElement = doc.createElement("path");
369 addPathAsValueToElement(path, pathElement);
370 dataChangeEventElement.appendChild(pathElement);
372 final Element operationElement = doc.createElement("operation");
373 operationElement.setTextContent(operation.value);
374 dataChangeEventElement.appendChild(operationElement);
376 return dataChangeEventElement;
379 private Node createCreatedChangedDataChangeEventElement(final Document doc, final Entry<YangInstanceIdentifier,
380 NormalizedNode<?, ?>> entry, final Operation operation, final SchemaContext
381 schemaContext, final DataSchemaContextTree dataSchemaContextTree) {
382 final Element dataChangeEventElement = doc.createElement("data-change-event");
383 final Element pathElement = doc.createElement("path");
384 final YangInstanceIdentifier path = entry.getKey();
385 addPathAsValueToElement(path, pathElement);
386 dataChangeEventElement.appendChild(pathElement);
388 final Element operationElement = doc.createElement("operation");
389 operationElement.setTextContent(operation.value);
390 dataChangeEventElement.appendChild(operationElement);
393 final DOMResult domResult = writeNormalizedNode(entry.getValue(), path,
394 schemaContext, dataSchemaContextTree);
395 final Node result = doc.importNode(domResult.getNode().getFirstChild(), true);
396 final Element dataElement = doc.createElement("data");
397 dataElement.appendChild(result);
398 dataChangeEventElement.appendChild(dataElement);
399 } catch (IOException e) {
400 LOG.error("Error in writer ", e);
401 } catch (XMLStreamException e) {
402 LOG.error("Error processing stream", e);
405 return dataChangeEventElement;
408 private static DOMResult writeNormalizedNode(final NormalizedNode<?,?> normalized, final
409 YangInstanceIdentifier path, final SchemaContext context, final DataSchemaContextTree dataSchemaContextTree) throws
410 IOException, XMLStreamException {
411 final XMLOutputFactory XML_FACTORY = XMLOutputFactory.newFactory();
412 final Document doc = XmlDocumentUtils.getDocument();
413 final DOMResult result = new DOMResult(doc);
414 NormalizedNodeWriter normalizedNodeWriter = null;
415 NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
416 XMLStreamWriter writer = null;
417 final SchemaPath nodePath;
419 if (normalized instanceof MapEntryNode || normalized instanceof UnkeyedListEntryNode) {
420 nodePath = dataSchemaContextTree.getChild(path).getDataSchemaNode().getPath();
422 nodePath = dataSchemaContextTree.getChild(path).getDataSchemaNode().getPath().getParent();
426 writer = XML_FACTORY.createXMLStreamWriter(result);
427 normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context, nodePath);
428 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);
430 normalizedNodeWriter.write(normalized);
432 normalizedNodeWriter.flush();
434 if (normalizedNodeWriter != null) {
435 normalizedNodeWriter.close();
437 if (normalizedNodeStreamWriter != null) {
438 normalizedNodeStreamWriter.close();
440 if (writer != null) {
449 * Adds path as value to element.
452 * Path to data in data store.
456 private void addPathAsValueToElement(final YangInstanceIdentifier path, final Element element) {
457 // Map< key = namespace, value = prefix>
458 final Map<String, String> prefixes = new HashMap<>();
459 final YangInstanceIdentifier normalizedPath = ControllerContext.getInstance().toXpathRepresentation(path);
460 final StringBuilder textContent = new StringBuilder();
462 // FIXME: BUG-1281: this is duplicated code from yangtools (BUG-1275)
463 for (final PathArgument pathArgument : normalizedPath.getPathArguments()) {
464 if (pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier) {
467 textContent.append("/");
468 writeIdentifierWithNamespacePrefix(element, textContent, pathArgument.getNodeType(), prefixes);
469 if (pathArgument instanceof NodeIdentifierWithPredicates) {
470 final Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
471 for (final QName keyValue : predicates.keySet()) {
472 final String predicateValue = String.valueOf(predicates.get(keyValue));
473 textContent.append("[");
474 writeIdentifierWithNamespacePrefix(element, textContent, keyValue, prefixes);
475 textContent.append("='");
476 textContent.append(predicateValue);
477 textContent.append("'");
478 textContent.append("]");
480 } else if (pathArgument instanceof NodeWithValue) {
481 textContent.append("[.='");
482 textContent.append(((NodeWithValue) pathArgument).getValue());
483 textContent.append("'");
484 textContent.append("]");
487 element.setTextContent(textContent.toString());
491 * Writes identifier that consists of prefix and QName.
500 * Map of namespaces and prefixes.
502 private static void writeIdentifierWithNamespacePrefix(final Element element, final StringBuilder textContent,
503 final QName qName, final Map<String, String> prefixes) {
504 final String namespace = qName.getNamespace().toString();
505 String prefix = prefixes.get(namespace);
506 if (prefix == null) {
507 prefix = generateNewPrefix(prefixes.values());
510 element.setAttribute("xmlns:" + prefix, namespace);
511 textContent.append(prefix);
512 prefixes.put(namespace, prefix);
514 textContent.append(":");
515 textContent.append(qName.getLocalName());
519 * Generates new prefix which consists of four random characters <a-z>.
522 * Collection of prefixes.
523 * @return New prefix which consists of four random characters <a-z>.
525 private static String generateNewPrefix(final Collection<String> prefixes) {
526 StringBuilder result = null;
527 final Random random = new Random();
529 result = new StringBuilder();
530 for (int i = 0; i < 4; i++) {
531 final int randomNumber = 0x61 + (Math.abs(random.nextInt()) % 26);
532 result.append(Character.toChars(randomNumber));
534 } while (prefixes.contains(result.toString()));
536 return result.toString();
540 * Gets path pointed to data in data store.
542 * @return Path pointed to data in data store.
544 public YangInstanceIdentifier getPath() {
549 * Sets {@link ListenerRegistration} registration.
551 * @param registration DOMDataChangeListener registration
553 public void setRegistration(final ListenerRegistration<DOMDataChangeListener> registration) {
554 this.registration = registration;
558 * Gets the name of the stream.
560 * @return The name of the stream.
562 public String getStreamName() {
567 * Removes all subscribers and unregisters event bus change recorder form event bus.
569 public void close() throws Exception {
570 subscribers = new ConcurrentSet<>();
571 registration.close();
573 eventBus.unregister(eventBusChangeRecorder);
577 * Checks if {@link ListenerRegistration} registration exist.
579 * @return True if exist, false otherwise.
581 public boolean isListening() {
582 return registration == null ? false : true;
586 * Creates event of type {@link EventType#REGISTER}, set {@link Channel} subscriber to the event and post event into
592 public void addSubscriber(final Channel subscriber) {
593 if (!subscriber.isActive()) {
594 LOG.debug("Channel is not active between websocket server and subscriber {}" + subscriber.remoteAddress());
596 final Event event = new Event(EventType.REGISTER);
597 event.setSubscriber(subscriber);
598 eventBus.post(event);
602 * Creates event of type {@link EventType#DEREGISTER}, sets {@link Channel} subscriber to the event and posts event
607 public void removeSubscriber(final Channel subscriber) {
608 LOG.debug("Subscriber {} is removed.", subscriber.remoteAddress());
609 final Event event = new Event(EventType.DEREGISTER);
610 event.setSubscriber(subscriber);
611 eventBus.post(event);
615 * Checks if exists at least one {@link Channel} subscriber.
617 * @return True if exist at least one {@link Channel} subscriber, false otherwise.
619 public boolean hasSubscribers() {
620 return !subscribers.isEmpty();
624 * Consists of two types {@link Store#CONFIG} and {@link Store#OPERATION}.
626 private static enum Store {
628 OPERATION("operation");
630 private final String value;
632 private Store(final String value) {
638 * Consists of three types {@link Operation#CREATED}, {@link Operation#UPDATED} and {@link Operation#DELETED}.
640 private static enum Operation {
645 private final String value;
647 private Operation(final String value) {