summaryrefslogtreecommitdiffstats
path: root/infra/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/JsonUtils.java
blob: d9798d07ddd1cbe49a1c022f3353a21f2e141f50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.v3po.translate.util;

import com.google.common.base.Charsets;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;

public final class JsonUtils {

    private JsonUtils() {}

    /**
     * Serialize normalized node root structure into provided output stream
     *
     * @throws IOException if serialized data cannot be written into provided output stream
     */
    public static void writeJsonRoot(@Nonnull final NormalizedNode<?, ?> rootData,
                                     @Nonnull final SchemaContext schemaContext,
                                     @Nonnull final OutputStream outputStream) throws IOException {
        final JsonWriter
            jsonWriter = createJsonWriter(outputStream, true);
        final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter
            .createNestedWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null, jsonWriter);
        final NormalizedNodeWriter normalizedNodeWriter =
            NormalizedNodeWriter.forStreamWriter(streamWriter, true);
        jsonWriter.beginObject();
        writeChildren(normalizedNodeWriter,(ContainerNode) rootData);
        jsonWriter.endObject();
        jsonWriter.flush();
    }

    /**
     * Read json serialized normalized node root structure and parse them into normalized nodes
     *
     * @return artificial normalized node holding all the top level nodes from provided stream as children. In case
     *         the stream is empty, empty artificial normalized node is returned
     *
     * @throws IllegalArgumentException if content in the provided input stream is not restore-able
     */
    public static ContainerNode readJsonRoot(@Nonnull final SchemaContext schemaContext,
                                             @Nonnull final InputStream stream) {
        final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder =
            Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()));
        final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(builder);

        final JsonParserStream jsonParser = JsonParserStream.create(writer, schemaContext);
        final JsonReader reader = new JsonReader(new InputStreamReader(stream, Charsets.UTF_8));
        jsonParser.parse(reader);

        return builder.build();
    }

    private static void writeChildren(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
        for(final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> child : data.getValue()) {
            nnWriter.write(child);
        }
    }

    private static JsonWriter createJsonWriter(final OutputStream entityStream, boolean prettyPrint) {
        if (prettyPrint) {
            return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8), 2);
        } else {
            return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
        }
    }
}