Eliminate SubscribeToStreamUtil
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / 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.nb.rfc8040.streams.listeners;
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.time.Instant;
16 import java.util.Collection;
17 import javax.xml.xpath.XPathExpressionException;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.$YangModuleInfoImpl;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.DataChangedNotification;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.data.changed.notification.DataChangeEvent;
22 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25
26 public final class JSONDataTreeCandidateFormatter extends DataTreeCandidateFormatter {
27     private static final @NonNull String NOTIFICATION_NAME = "ietf-restconf:notification";
28
29     private static final @NonNull String DATA_CHANGED_NOTIFICATION_NAME;
30     private static final @NonNull String DATA_CHANGED_EVENT_NAME = DataChangeEvent.QNAME.getLocalName();
31
32     static {
33         final var salRemoteName = $YangModuleInfoImpl.getInstance().getName().getLocalName();
34         DATA_CHANGED_NOTIFICATION_NAME = salRemoteName + ":" + DataChangedNotification.QNAME.getLocalName();
35     }
36
37     private final JSONCodecFactorySupplier codecSupplier;
38
39     private JSONDataTreeCandidateFormatter(final TextParameters textParams,
40             final JSONCodecFactorySupplier codecSupplier) {
41         super(textParams);
42         this.codecSupplier = requireNonNull(codecSupplier);
43     }
44
45     private JSONDataTreeCandidateFormatter(final TextParameters textParams, final String xpathFilter,
46             final JSONCodecFactorySupplier codecSupplier) throws XPathExpressionException {
47         super(textParams, xpathFilter);
48         this.codecSupplier = requireNonNull(codecSupplier);
49     }
50
51     public static DataTreeCandidateFormatterFactory createFactory(
52             final JSONCodecFactorySupplier codecSupplier) {
53         final var empty = new JSONDataTreeCandidateFormatter(TextParameters.EMPTY, codecSupplier);
54         return new DataTreeCandidateFormatterFactory(empty) {
55             @Override
56             DataTreeCandidateFormatter newFormatter(final TextParameters textParams) {
57                 return new JSONDataTreeCandidateFormatter(textParams, codecSupplier);
58             }
59
60             @Override
61             DataTreeCandidateFormatter getFormatter(final TextParameters textParams, final String xpathFilter)
62                     throws XPathExpressionException {
63                 return new JSONDataTreeCandidateFormatter(textParams, xpathFilter, codecSupplier);
64             }
65         };
66     }
67
68     @Override
69     String createText(final TextParameters params, final EffectiveModelContext schemaContext,
70             final Collection<DataTreeCandidate> input, final Instant now) throws IOException {
71         try (var writer = new StringWriter()) {
72             boolean nonEmpty = false;
73             try (var jsonWriter = new JsonWriter(writer)) {
74                 jsonWriter.beginObject()
75                     .name(NOTIFICATION_NAME).beginObject()
76                         .name("event-time").value(toRFC3339(now))
77                         .name(DATA_CHANGED_NOTIFICATION_NAME).beginObject()
78                             .name(DATA_CHANGED_EVENT_NAME).beginArray();
79
80                 final var serializer = new JsonDataTreeCandidateSerializer(schemaContext, codecSupplier, jsonWriter);
81                 for (var candidate : input) {
82                     nonEmpty |= serializer.serialize(candidate, params);
83                 }
84
85                 jsonWriter.endArray().endObject().endObject().endObject();
86             }
87
88             return nonEmpty ? writer.toString() : null;
89         }
90     }
91 }