Bump upstreams to SNAPSHOTs
[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.codec.gson.JSONCodecFactorySupplier;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23
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;
28
29     private JSONDataTreeCandidateFormatter(final JSONCodecFactorySupplier codecSupplier) {
30         this.codecSupplier = requireNonNull(codecSupplier);
31     }
32
33     private JSONDataTreeCandidateFormatter(final String xpathFilter, final JSONCodecFactorySupplier codecSupplier)
34             throws XPathExpressionException {
35         super(xpathFilter);
36         this.codecSupplier = requireNonNull(codecSupplier);
37     }
38
39     public static DataTreeCandidateFormatterFactory createFactory(
40             final JSONCodecFactorySupplier codecSupplier) {
41         requireNonNull(codecSupplier);
42         return new DataTreeCandidateFormatterFactory() {
43             @Override
44             public DataTreeCandidateFormatter getFormatter(final String xpathFilter)
45                     throws XPathExpressionException {
46                 return new JSONDataTreeCandidateFormatter(xpathFilter, codecSupplier);
47             }
48
49             @Override
50             public DataTreeCandidateFormatter getFormatter() {
51                 return new JSONDataTreeCandidateFormatter(codecSupplier);
52             }
53         };
54     }
55
56     @Override
57     String createText(final EffectiveModelContext schemaContext, final Collection<DataTreeCandidate> input,
58                       final Instant now, final boolean leafNodesOnly, final boolean skipData)
59             throws IOException {
60         final Writer writer = new StringWriter();
61         final JsonWriter jsonWriter = new JsonWriter(writer).beginObject();
62
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();
66
67         final JsonDataTreeCandidateSerializer serializer =
68                 new JsonDataTreeCandidateSerializer(schemaContext, codecSupplier, jsonWriter);
69         for (final DataTreeCandidate candidate : input) {
70             serializer.serialize(candidate, leafNodesOnly, skipData);
71         }
72
73         // data-change-event
74         jsonWriter.endArray();
75         // data-changed-notification
76         jsonWriter.endObject();
77
78         jsonWriter.name("event-time").value(toRFC3339(now));
79         jsonWriter.endObject();
80
81         // notification
82         jsonWriter.endObject();
83
84         return writer.toString();
85     }
86 }