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.netconf.sal.streams.listeners;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.io.StringWriter;
14 import java.io.Writer;
15 import java.time.Instant;
16 import java.util.Collection;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.transform.dom.DOMResult;
19 import org.json.JSONObject;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
22 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
30 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
31 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Node;
42 * {@link NotificationListenerAdapter} is responsible to track events on
46 public class NotificationListenerAdapter extends AbstractCommonSubscriber implements DOMNotificationListener {
48 private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class);
50 private final String streamName;
51 private final SchemaPath path;
52 private final String outputType;
54 private SchemaContext schemaContext;
55 private DOMNotification notification;
58 * Set path of listener and stream name, register event bus.
61 * path of notification
63 * stream name of listener
65 * type of output on notification (JSON, XML)
67 NotificationListenerAdapter(final SchemaPath path, final String streamName, final String outputType) {
70 setLocalNameOfPath(path.getLastComponent().getLocalName());
72 this.outputType = Preconditions.checkNotNull(outputType);
73 this.path = Preconditions.checkNotNull(path);
74 Preconditions.checkArgument(streamName != null && !streamName.isEmpty());
75 this.streamName = streamName;
79 * Get outputType of listener.
81 * @return the outputType
84 public String getOutputType() {
85 return this.outputType;
89 public void onNotification(final DOMNotification notification) {
90 this.schemaContext = ControllerContext.getInstance().getGlobalSchema();
91 this.notification = notification;
93 final String xml = prepareXml();
94 if (checkQueryParams(xml, this)) {
95 prepareAndPostData(xml);
100 * Get stream name of this listener.
102 * @return {@link String}
105 public String getStreamName() {
106 return this.streamName;
110 * Get schema path of notification.
112 * @return {@link SchemaPath}
114 public SchemaPath getSchemaPath() {
119 * Prepare data of notification and data to client.
123 private void prepareAndPostData(final String xml) {
124 final Event event = new Event(EventType.NOTIFY);
125 if (this.outputType.equals("JSON")) {
126 event.setData(prepareJson());
134 * Prepare json from notification data.
136 * @return json as {@link String}
139 String prepareJson() {
140 final JSONObject json = new JSONObject();
141 json.put("ietf-restconf:notification",
142 new JSONObject(writeBodyToString()).put("event-time", ListenerAdapter.toRFC3339(Instant.now())));
143 return json.toString();
147 void setNotification(final DOMNotification notification) {
148 this.notification = Preconditions.checkNotNull(notification);
152 void setSchemaContext(final SchemaContext schemaContext) {
153 this.schemaContext = Preconditions.checkNotNull(schemaContext);
156 private String writeBodyToString() {
157 final Writer writer = new StringWriter();
158 final NormalizedNodeStreamWriter jsonStream =
159 JSONNormalizedNodeStreamWriter.createExclusiveWriter(JSONCodecFactory.getShared(this.schemaContext),
160 this.notification.getType(), null, JsonWriterFactory.createJsonWriter(writer));
161 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
163 nodeWriter.write(this.notification.getBody());
165 } catch (final IOException e) {
166 throw new RestconfDocumentedException("Problem while writing body of notification to JSON. ", e);
168 return writer.toString();
171 private String prepareXml() {
172 final Document doc = createDocument();
173 final Element notificationElement = basePartDoc(doc);
175 final Element notificationEventElement = doc.createElementNS(
176 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "create-notification-stream");
177 addValuesToNotificationEventElement(doc, notificationEventElement, this.notification, this.schemaContext);
178 notificationElement.appendChild(notificationEventElement);
180 return transformDoc(doc);
183 private void addValuesToNotificationEventElement(final Document doc, final Element element,
184 final DOMNotification notification, final SchemaContext schemaContext) {
185 if (notification == null) {
189 final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> body =
190 notification.getBody();
193 final DOMResult domResult = writeNormalizedNode(body, schemaContext, this.path);
194 final Node result = doc.importNode(domResult.getNode().getFirstChild(), true);
195 final Element dataElement = doc.createElement("notification");
196 dataElement.appendChild(result);
197 element.appendChild(dataElement);
198 } catch (final IOException e) {
199 LOG.error("Error in writer ", e);
200 } catch (final XMLStreamException e) {
201 LOG.error("Error processing stream", e);