2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.common.serializer;
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;
23 public class JsonDataTreeCandidateSerializer extends AbstractWebsocketSerializer<IOException> {
25 private final JSONCodecFactorySupplier codecSupplier;
26 private final EffectiveModelContext context;
27 private final JsonWriter jsonWriter;
29 public JsonDataTreeCandidateSerializer(final JSONCodecFactorySupplier codecSupplier,
30 final EffectiveModelContext context,
31 final JsonWriter jsonWriter) {
33 this.codecSupplier = codecSupplier;
34 this.context = context;
35 this.jsonWriter = jsonWriter;
38 void serializeData(Collection<YangInstanceIdentifier.PathArgument> nodePath, DataTreeCandidateNode candidate,
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);
48 jsonWriter.beginObject();
49 serializePath(nodePath);
51 if (!skipData && candidate.getDataAfter().isPresent()) {
52 jsonWriter.name("data").beginObject();
53 NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(nestedWriter);
54 nodeWriter.write(candidate.getDataAfter().get());
58 jsonWriter.endObject();
61 serializeOperation(candidate);
62 jsonWriter.endObject();
66 void serializeOperation(final DataTreeCandidateNode candidate)
68 jsonWriter.name("operation").value(modificationTypeToOperation(candidate, candidate.getModificationType()));
72 void serializePath(Collection<YangInstanceIdentifier.PathArgument> pathArguments)
74 jsonWriter.name("path").value(convertPath(pathArguments));