Make ListenerAdapter serialize JSON directly
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / serializer / JsonDataTreeCandidateSerializer.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.serializer;
9
10 import com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import java.util.Collection;
13 import java.util.stream.Collectors;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
16 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
18 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22
23 public class JsonDataTreeCandidateSerializer extends AbstractWebsocketSerializer<IOException> {
24
25     private final JSONCodecFactorySupplier codecSupplier;
26     private final EffectiveModelContext context;
27     private final JsonWriter jsonWriter;
28
29     public JsonDataTreeCandidateSerializer(final JSONCodecFactorySupplier codecSupplier,
30                                            final EffectiveModelContext context,
31                                            final JsonWriter jsonWriter) {
32
33         this.codecSupplier = codecSupplier;
34         this.context = context;
35         this.jsonWriter = jsonWriter;
36     }
37
38     void serializeData(Collection<YangInstanceIdentifier.PathArgument> nodePath, DataTreeCandidateNode candidate,
39                        boolean skipData)
40             throws IOException {
41         final SchemaPath path = SchemaPath.create(nodePath.stream()
42                 .filter(p -> !(p instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates))
43                 .map(YangInstanceIdentifier.PathArgument::getNodeType).collect(Collectors.toList()), true);
44         final NormalizedNodeStreamWriter nestedWriter =
45                 JSONNormalizedNodeStreamWriter
46                         .createNestedWriter(codecSupplier.getShared(context), path.getParent(), null, jsonWriter);
47
48         jsonWriter.beginObject();
49         serializePath(nodePath);
50
51         if (!skipData && candidate.getDataAfter().isPresent()) {
52             jsonWriter.name("data").beginObject();
53             NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(nestedWriter);
54             nodeWriter.write(candidate.getDataAfter().get());
55             nodeWriter.flush();
56
57             // end data
58             jsonWriter.endObject();
59         }
60
61         serializeOperation(candidate);
62         jsonWriter.endObject();
63     }
64
65     @Override
66     void serializeOperation(final DataTreeCandidateNode candidate)
67             throws IOException {
68         jsonWriter.name("operation").value(modificationTypeToOperation(candidate, candidate.getModificationType()));
69     }
70
71     @Override
72     void serializePath(Collection<YangInstanceIdentifier.PathArgument> pathArguments)
73             throws IOException {
74         jsonWriter.name("path").value(convertPath(pathArguments));
75     }
76 }