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.common.formatters;
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.restconf.common.serializer.JsonDataTreeCandidateSerializer;
20 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 public final class JSONDataTreeCandidateFormatter extends DataTreeCandidateFormatter {
25 public static final String SAL_REMOTE_NAMESPACE = "urn-opendaylight-params-xml-ns-yang-controller-md-sal-remote";
26 public static final String NETCONF_NOTIFICATION_NAMESPACE = "urn-ietf-params-xml-ns-netconf-notification-1.0";
27 private final JSONCodecFactorySupplier codecSupplier;
29 private JSONDataTreeCandidateFormatter(final JSONCodecFactorySupplier codecSupplier) {
30 this.codecSupplier = requireNonNull(codecSupplier);
33 private JSONDataTreeCandidateFormatter(final String xpathFilter, final JSONCodecFactorySupplier codecSupplier)
34 throws XPathExpressionException {
36 this.codecSupplier = requireNonNull(codecSupplier);
39 public static DataTreeCandidateFormatterFactory createFactory(
40 final JSONCodecFactorySupplier codecSupplier) {
41 requireNonNull(codecSupplier);
42 return new DataTreeCandidateFormatterFactory() {
44 public DataTreeCandidateFormatter getFormatter(final String xpathFilter)
45 throws XPathExpressionException {
46 return new JSONDataTreeCandidateFormatter(xpathFilter, codecSupplier);
50 public DataTreeCandidateFormatter getFormatter() {
51 return new JSONDataTreeCandidateFormatter(codecSupplier);
57 String createText(final EffectiveModelContext schemaContext, final Collection<DataTreeCandidate> input,
58 final Instant now, final boolean leafNodesOnly, final boolean skipData)
60 final Writer writer = new StringWriter();
61 final JsonWriter jsonWriter = new JsonWriter(writer).beginObject();
63 jsonWriter.name(NETCONF_NOTIFICATION_NAMESPACE + ":notification").beginObject();
64 jsonWriter.name(SAL_REMOTE_NAMESPACE + ":data-changed-notification").beginObject();
65 jsonWriter.name("data-change-event").beginArray();
67 final JsonDataTreeCandidateSerializer serializer =
68 new JsonDataTreeCandidateSerializer(schemaContext, codecSupplier, jsonWriter);
69 for (final DataTreeCandidate candidate : input) {
70 serializer.serialize(candidate, leafNodesOnly, skipData);
74 jsonWriter.endArray();
75 // data-changed-notification
76 jsonWriter.endObject();
78 jsonWriter.name("event-time").value(toRFC3339(now));
79 jsonWriter.endObject();
82 jsonWriter.endObject();
84 return writer.toString();