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.server.mdsal.streams.dtcl;
10 import com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import java.io.StringWriter;
13 import java.time.Instant;
14 import java.util.List;
15 import javax.xml.xpath.XPathExpressionException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.restconf.server.spi.TextParameters;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.$YangModuleInfoImpl;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.DataChangedNotification;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.data.changed.notification.DataChangeEvent;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 final class JSONDataTreeCandidateFormatter extends DataTreeCandidateFormatter {
25 private static final @NonNull String DATA_CHANGED_EVENT_NAME = DataChangeEvent.QNAME.getLocalName();
26 private static final @NonNull String DATA_CHANGED_NOTIFICATION_NAME =
27 $YangModuleInfoImpl.getInstance().getName().getLocalName() + ":" + DataChangedNotification.QNAME.getLocalName();
28 private static final JSONDataTreeCandidateFormatter EMPTY =
29 new JSONDataTreeCandidateFormatter(TextParameters.EMPTY);
31 static final DataTreeCandidateFormatterFactory FACTORY = new DataTreeCandidateFormatterFactory(EMPTY) {
33 public DataTreeCandidateFormatter newFormatter(final TextParameters textParams) {
34 return new JSONDataTreeCandidateFormatter(textParams);
38 public DataTreeCandidateFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
39 throws XPathExpressionException {
40 return new JSONDataTreeCandidateFormatter(textParams, xpathFilter);
44 private JSONDataTreeCandidateFormatter(final TextParameters textParams) {
48 private JSONDataTreeCandidateFormatter(final TextParameters textParams, final String xpathFilter)
49 throws XPathExpressionException {
50 super(textParams, xpathFilter);
54 protected String createText(final TextParameters params, final EffectiveModelContext schemaContext,
55 final List<DataTreeCandidate> input, final Instant now) throws IOException {
56 try (var writer = new StringWriter()) {
57 boolean nonEmpty = false;
58 try (var jsonWriter = new JsonWriter(writer)) {
59 jsonWriter.beginObject()
60 .name("ietf-restconf:notification").beginObject()
61 .name("event-time").value(toRFC3339(now))
62 .name(DATA_CHANGED_NOTIFICATION_NAME).beginObject()
63 .name(DATA_CHANGED_EVENT_NAME).beginArray();
65 final var serializer = new JSONDataTreeCandidateSerializer(schemaContext, jsonWriter);
66 for (var candidate : input) {
67 nonEmpty |= serializer.serialize(candidate, params);
70 jsonWriter.endArray().endObject().endObject().endObject();
73 return nonEmpty ? writer.toString() : null;