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 com.google.gson.JsonObject;
13 import com.google.gson.JsonParser;
14 import java.io.IOException;
15 import java.io.StringWriter;
16 import java.io.Writer;
17 import java.time.Instant;
18 import java.util.Collection;
19 import javax.xml.stream.XMLStreamException;
20 import javax.xml.transform.dom.DOMResult;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
22 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
23 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
31 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
32 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.Node;
43 * {@link NotificationListenerAdapter} is responsible to track events on
47 public class NotificationListenerAdapter extends AbstractCommonSubscriber implements DOMNotificationListener {
49 private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class);
51 private final String streamName;
52 private final SchemaPath path;
53 private final String outputType;
55 private SchemaContext schemaContext;
56 private DOMNotification notification;
59 * Set path of listener and stream name, register event bus.
62 * path of notification
64 * stream name of listener
66 * type of output on notification (JSON, XML)
68 NotificationListenerAdapter(final SchemaPath path, final String streamName, final String outputType) {
70 this.outputType = Preconditions.checkNotNull(outputType);
71 this.path = Preconditions.checkNotNull(path);
72 Preconditions.checkArgument(streamName != null && !streamName.isEmpty());
73 this.streamName = streamName;
77 * Get outputType of listener.
79 * @return the outputType
82 public String getOutputType() {
83 return this.outputType;
87 public void onNotification(final DOMNotification notification) {
88 this.schemaContext = ControllerContext.getInstance().getGlobalSchema();
89 this.notification = notification;
91 final String xml = prepareXml();
92 if (checkQueryParams(xml, this)) {
93 prepareAndPostData(xml);
98 * Get stream name of this listener.
100 * @return {@link String}
103 public String getStreamName() {
104 return this.streamName;
108 * Get schema path of notification.
110 * @return {@link SchemaPath}
112 public SchemaPath getSchemaPath() {
117 * Prepare data of notification and data to client.
121 private void prepareAndPostData(final String xml) {
122 final Event event = new Event(EventType.NOTIFY);
123 if (this.outputType.equals("JSON")) {
124 event.setData(prepareJson());
132 * Prepare json from notification data.
134 * @return json as {@link String}
137 String prepareJson() {
138 final JsonParser jsonParser = new JsonParser();
139 final JsonObject json = new JsonObject();
140 json.add("ietf-restconf:notification", jsonParser.parse(writeBodyToString()));
141 json.addProperty("event-time", ListenerAdapter.toRFC3339(Instant.now()));
142 return json.toString();
146 void setNotification(final DOMNotification notification) {
147 this.notification = Preconditions.checkNotNull(notification);
151 void setSchemaContext(final SchemaContext schemaContext) {
152 this.schemaContext = Preconditions.checkNotNull(schemaContext);
155 private String writeBodyToString() {
156 final Writer writer = new StringWriter();
157 final NormalizedNodeStreamWriter jsonStream =
158 JSONNormalizedNodeStreamWriter.createExclusiveWriter(JSONCodecFactory.getShared(this.schemaContext),
159 this.notification.getType(), null, JsonWriterFactory.createJsonWriter(writer));
160 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
162 nodeWriter.write(this.notification.getBody());
164 } catch (final IOException e) {
165 throw new RestconfDocumentedException("Problem while writing body of notification to JSON. ", e);
167 return writer.toString();
170 private String prepareXml() {
171 final Document doc = createDocument();
172 final Element notificationElement = basePartDoc(doc);
174 final Element notificationEventElement = doc.createElementNS(
175 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "create-notification-stream");
176 addValuesToNotificationEventElement(doc, notificationEventElement, this.notification, this.schemaContext);
177 notificationElement.appendChild(notificationEventElement);
179 return transformDoc(doc);
182 private void addValuesToNotificationEventElement(final Document doc, final Element element,
183 final DOMNotification notification, final SchemaContext schemaContext) {
184 if (notification == null) {
188 final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> body =
189 notification.getBody();
192 final DOMResult domResult = writeNormalizedNode(body, schemaContext, this.path);
193 final Node result = doc.importNode(domResult.getNode().getFirstChild(), true);
194 final Element dataElement = doc.createElement("notification");
195 dataElement.appendChild(result);
196 element.appendChild(dataElement);
197 } catch (final IOException e) {
198 LOG.error("Error in writer ", e);
199 } catch (final XMLStreamException e) {
200 LOG.error("Error processing stream", e);