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.time.Instant;
16 import java.util.Collection;
17 import javax.xml.xpath.XPathExpressionException;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.$YangModuleInfoImpl;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.DataChangedNotification;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.data.changed.notification.DataChangeEvent;
22 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 public final class JSONDataTreeCandidateFormatter extends DataTreeCandidateFormatter {
27 private static final @NonNull String NOTIFICATION_NAME = "ietf-restconf:notification";
29 private static final @NonNull String DATA_CHANGED_NOTIFICATION_NAME;
30 private static final @NonNull String DATA_CHANGED_EVENT_NAME = DataChangeEvent.QNAME.getLocalName();
33 final var salRemoteName = $YangModuleInfoImpl.getInstance().getName().getLocalName();
34 DATA_CHANGED_NOTIFICATION_NAME = salRemoteName + ":" + DataChangedNotification.QNAME.getLocalName();
37 private final JSONCodecFactorySupplier codecSupplier;
39 private JSONDataTreeCandidateFormatter(final TextParameters textParams,
40 final JSONCodecFactorySupplier codecSupplier) {
42 this.codecSupplier = requireNonNull(codecSupplier);
45 private JSONDataTreeCandidateFormatter(final TextParameters textParams, final String xpathFilter,
46 final JSONCodecFactorySupplier codecSupplier) throws XPathExpressionException {
47 super(textParams, xpathFilter);
48 this.codecSupplier = requireNonNull(codecSupplier);
51 public static DataTreeCandidateFormatterFactory createFactory(
52 final JSONCodecFactorySupplier codecSupplier) {
53 final var empty = new JSONDataTreeCandidateFormatter(TextParameters.EMPTY, codecSupplier);
54 return new DataTreeCandidateFormatterFactory(empty) {
56 DataTreeCandidateFormatter newFormatter(final TextParameters textParams) {
57 return new JSONDataTreeCandidateFormatter(textParams, codecSupplier);
61 DataTreeCandidateFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
62 throws XPathExpressionException {
63 return new JSONDataTreeCandidateFormatter(textParams, xpathFilter, codecSupplier);
69 String createText(final TextParameters params, final EffectiveModelContext schemaContext,
70 final Collection<DataTreeCandidate> input, final Instant now) throws IOException {
71 try (var writer = new StringWriter()) {
72 boolean nonEmpty = false;
73 try (var jsonWriter = new JsonWriter(writer)) {
74 jsonWriter.beginObject()
75 .name(NOTIFICATION_NAME).beginObject()
76 .name("event-time").value(toRFC3339(now))
77 .name(DATA_CHANGED_NOTIFICATION_NAME).beginObject()
78 .name(DATA_CHANGED_EVENT_NAME).beginArray();
80 final var serializer = new JsonDataTreeCandidateSerializer(schemaContext, codecSupplier, jsonWriter);
81 for (var candidate : input) {
82 nonEmpty |= serializer.serialize(candidate, params);
85 jsonWriter.endArray().endObject().endObject().endObject();
88 return nonEmpty ? writer.toString() : null;