Add Child Nodes Only query parameter to SSE events
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / JSONDataTreeCandidateFormatter.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.gson.stream.JsonWriter;
13 import java.io.IOException;
14 import java.io.StringWriter;
15 import java.io.Writer;
16 import java.time.Instant;
17 import java.util.Collection;
18 import javax.xml.xpath.XPathExpressionException;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22
23 public final class JSONDataTreeCandidateFormatter extends DataTreeCandidateFormatter {
24     public static final String SAL_REMOTE_NAMESPACE = "urn-opendaylight-params-xml-ns-yang-controller-md-sal-remote";
25     public static final String NETCONF_NOTIFICATION_NAMESPACE = "urn-ietf-params-xml-ns-netconf-notification-1.0";
26     private final JSONCodecFactorySupplier codecSupplier;
27
28     private JSONDataTreeCandidateFormatter(final JSONCodecFactorySupplier codecSupplier) {
29         this.codecSupplier = requireNonNull(codecSupplier);
30     }
31
32     private JSONDataTreeCandidateFormatter(final String xpathFilter, final JSONCodecFactorySupplier codecSupplier)
33             throws XPathExpressionException {
34         super(xpathFilter);
35         this.codecSupplier = requireNonNull(codecSupplier);
36     }
37
38     public static DataTreeCandidateFormatterFactory createFactory(
39             final JSONCodecFactorySupplier codecSupplier) {
40         requireNonNull(codecSupplier);
41         return new DataTreeCandidateFormatterFactory() {
42             @Override
43             public DataTreeCandidateFormatter getFormatter(final String xpathFilter)
44                     throws XPathExpressionException {
45                 return new JSONDataTreeCandidateFormatter(xpathFilter, codecSupplier);
46             }
47
48             @Override
49             public DataTreeCandidateFormatter getFormatter() {
50                 return new JSONDataTreeCandidateFormatter(codecSupplier);
51             }
52         };
53     }
54
55     @Override
56     String createText(final EffectiveModelContext schemaContext, final Collection<DataTreeCandidate> input,
57             final Instant now, final boolean leafNodesOnly, final boolean skipData, final boolean changedLeafNodesOnly,
58             final boolean childNodesOnly) throws IOException {
59         final Writer writer = new StringWriter();
60         final JsonWriter jsonWriter = new JsonWriter(writer).beginObject();
61
62         jsonWriter.name(NETCONF_NOTIFICATION_NAMESPACE + ":notification").beginObject();
63         jsonWriter.name(SAL_REMOTE_NAMESPACE + ":data-changed-notification").beginObject();
64         jsonWriter.name("data-change-event").beginArray();
65
66         final var serializer = new JsonDataTreeCandidateSerializer(schemaContext, codecSupplier, jsonWriter);
67         boolean nonEmpty = false;
68         for (var candidate : input) {
69             nonEmpty |= serializer.serialize(candidate, leafNodesOnly, skipData, changedLeafNodesOnly, childNodesOnly);
70         }
71
72         // data-change-event
73         jsonWriter.endArray();
74         // data-changed-notification
75         jsonWriter.endObject();
76
77         jsonWriter.name("event-time").value(toRFC3339(now));
78         jsonWriter.endObject();
79
80         // notification
81         jsonWriter.endObject();
82
83         return nonEmpty ? writer.toString() : null;
84     }
85 }