Make ListenerAdapter serialize JSON directly
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / formatters / JSONDataTreeCandidateFormatter.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.common.formatters;
9
10 import static java.util.Objects.requireNonNull;
11
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;
25
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;
31
32     private JSONDataTreeCandidateFormatter(final JSONCodecFactorySupplier codecSupplier) {
33         this.codecSupplier = requireNonNull(codecSupplier);
34     }
35
36     private JSONDataTreeCandidateFormatter(final String xpathFilter, final JSONCodecFactorySupplier codecSupplier)
37             throws XPathExpressionException {
38         super(xpathFilter);
39         this.codecSupplier = requireNonNull(codecSupplier);
40     }
41
42     public static DataTreeCandidateFormatterFactory createFactory(
43             final JSONCodecFactorySupplier codecSupplier) {
44         requireNonNull(codecSupplier);
45         return new DataTreeCandidateFormatterFactory() {
46             @Override
47             public DataTreeCandidateFormatter getFormatter(final String xpathFilter)
48                     throws XPathExpressionException {
49                 return new JSONDataTreeCandidateFormatter(xpathFilter, codecSupplier);
50             }
51
52             @Override
53             public DataTreeCandidateFormatter getFormatter() {
54                 return new JSONDataTreeCandidateFormatter(codecSupplier);
55             }
56         };
57     }
58
59     @Override
60     String createText(final EffectiveModelContext schemaContext, final Collection<DataTreeCandidate> input,
61                       final Instant now, boolean leafNodesOnly, boolean skipData)
62             throws IOException {
63         final Writer writer = new StringWriter();
64         final JsonWriter jsonWriter = new JsonWriter(writer).beginObject();
65
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();
69
70         final JsonDataTreeCandidateSerializer serializer =
71                 new JsonDataTreeCandidateSerializer(codecSupplier, schemaContext, jsonWriter);
72         for (final DataTreeCandidate candidate : input) {
73             serializer.serialize(candidate, leafNodesOnly, skipData);
74         }
75
76         // data-change-event
77         jsonWriter.endArray();
78         // data-changed-notification
79         jsonWriter.endObject();
80
81         jsonWriter.name("event-time").value(toRFC3339(now));
82         jsonWriter.endObject();
83
84         // notification
85         jsonWriter.endObject();
86
87         return writer.toString();
88     }
89 }