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.api.schema.tree.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public final class JSONDataTreeCandidateFormatter extends DataTreeCandidateFormatter {
27 private static final Logger LOG = LoggerFactory.getLogger(JSONDataTreeCandidateFormatter.class);
28 public static final String SAL_REMOTE_NAMESPACE = "urn-opendaylight-params-xml-ns-yang-controller-md-sal-remote";
29 public static final String NETCONF_NOTIFICATION_NAMESPACE = "urn-ietf-params-xml-ns-netconf-notification-1.0";
30 private final JSONCodecFactorySupplier codecSupplier;
32 private JSONDataTreeCandidateFormatter(final JSONCodecFactorySupplier codecSupplier) {
33 this.codecSupplier = requireNonNull(codecSupplier);
36 private JSONDataTreeCandidateFormatter(final String xpathFilter, final JSONCodecFactorySupplier codecSupplier)
37 throws XPathExpressionException {
39 this.codecSupplier = requireNonNull(codecSupplier);
42 public static DataTreeCandidateFormatterFactory createFactory(
43 final JSONCodecFactorySupplier codecSupplier) {
44 requireNonNull(codecSupplier);
45 return new DataTreeCandidateFormatterFactory() {
47 public DataTreeCandidateFormatter getFormatter(final String xpathFilter)
48 throws XPathExpressionException {
49 return new JSONDataTreeCandidateFormatter(xpathFilter, codecSupplier);
53 public DataTreeCandidateFormatter getFormatter() {
54 return new JSONDataTreeCandidateFormatter(codecSupplier);
60 String createText(final EffectiveModelContext schemaContext, final Collection<DataTreeCandidate> input,
61 final Instant now, boolean leafNodesOnly, boolean skipData)
63 final Writer writer = new StringWriter();
64 final JsonWriter jsonWriter = new JsonWriter(writer).beginObject();
66 jsonWriter.name(NETCONF_NOTIFICATION_NAMESPACE + ":notification").beginObject();
67 jsonWriter.name(SAL_REMOTE_NAMESPACE + ":data-changed-notification").beginObject();
68 jsonWriter.name("data-change-event").beginArray();
70 final JsonDataTreeCandidateSerializer serializer =
71 new JsonDataTreeCandidateSerializer(codecSupplier, schemaContext, jsonWriter);
72 for (final DataTreeCandidate candidate : input) {
73 serializer.serialize(candidate, leafNodesOnly, skipData);
77 jsonWriter.endArray();
78 // data-changed-notification
79 jsonWriter.endObject();
81 jsonWriter.name("event-time").value(toRFC3339(now));
82 jsonWriter.endObject();
85 jsonWriter.endObject();
87 return writer.toString();