2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.streams.listeners;
10 import static java.util.Objects.requireNonNull;
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;
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;
28 private JSONDataTreeCandidateFormatter(final JSONCodecFactorySupplier codecSupplier) {
29 this.codecSupplier = requireNonNull(codecSupplier);
32 private JSONDataTreeCandidateFormatter(final String xpathFilter, final JSONCodecFactorySupplier codecSupplier)
33 throws XPathExpressionException {
35 this.codecSupplier = requireNonNull(codecSupplier);
38 public static DataTreeCandidateFormatterFactory createFactory(
39 final JSONCodecFactorySupplier codecSupplier) {
40 requireNonNull(codecSupplier);
41 return new DataTreeCandidateFormatterFactory() {
43 public DataTreeCandidateFormatter getFormatter(final String xpathFilter)
44 throws XPathExpressionException {
45 return new JSONDataTreeCandidateFormatter(xpathFilter, codecSupplier);
49 public DataTreeCandidateFormatter getFormatter() {
50 return new JSONDataTreeCandidateFormatter(codecSupplier);
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();
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();
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);
73 jsonWriter.endArray();
74 // data-changed-notification
75 jsonWriter.endObject();
77 jsonWriter.name("event-time").value(toRFC3339(now));
78 jsonWriter.endObject();
81 jsonWriter.endObject();
83 return nonEmpty ? writer.toString() : null;