Move OperationsContent
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / 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.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.util.Collection;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
20 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
24
25 final class JsonDataTreeCandidateSerializer extends AbstractWebsocketSerializer<IOException> {
26     private final JSONCodecFactorySupplier codecSupplier;
27     private final JsonWriter jsonWriter;
28
29     JsonDataTreeCandidateSerializer(final EffectiveModelContext context,
30             final JSONCodecFactorySupplier codecSupplier, final JsonWriter jsonWriter) {
31         super(context);
32         this.codecSupplier = requireNonNull(codecSupplier);
33         this.jsonWriter = requireNonNull(jsonWriter);
34     }
35
36     @Override
37     void serializeData(final Inference parent, final Collection<PathArgument> dataPath,
38             final DataTreeCandidateNode candidate, final boolean skipData) throws IOException {
39         NormalizedNodeStreamWriter nestedWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(
40             codecSupplier.getShared(parent.getEffectiveModelContext()), parent, null, jsonWriter);
41         jsonWriter.beginObject();
42         serializePath(dataPath);
43
44         if (!skipData && candidate.getDataAfter().isPresent()) {
45             jsonWriter.name("data").beginObject();
46             NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(nestedWriter);
47             nodeWriter.write(candidate.getDataAfter().get());
48             nodeWriter.flush();
49
50             // end data
51             jsonWriter.endObject();
52         }
53
54         serializeOperation(candidate);
55         jsonWriter.endObject();
56     }
57
58     @Override
59     void serializeOperation(final DataTreeCandidateNode candidate)
60             throws IOException {
61         jsonWriter.name("operation").value(modificationTypeToOperation(candidate, candidate.getModificationType()));
62     }
63
64     @Override
65     void serializePath(final Collection<YangInstanceIdentifier.PathArgument> pathArguments)
66             throws IOException {
67         jsonWriter.name("path").value(convertPath(pathArguments));
68     }
69 }