2 * Copyright (c) 2014 Cisco Systems, Inc. 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.yangtools.yang.data.codec.gson;
10 import com.google.gson.stream.JsonReader;
12 import java.io.BufferedReader;
14 import java.io.FileNotFoundException;
15 import java.io.FileReader;
16 import java.io.IOException;
17 import java.io.StringReader;
18 import java.io.StringWriter;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.List;
24 import org.junit.BeforeClass;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.LoggingNormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
38 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class StreamToNormalizedNodeTest {
43 private static final Logger LOG = LoggerFactory.getLogger(StreamToNormalizedNodeTest.class);
44 private static SchemaContext schemaContext;
45 private static String streamAsString;
48 public static void initialization() throws IOException, URISyntaxException {
49 schemaContext = loadModules("/complexjson/yang");
50 streamAsString = loadTextFile(new File(StreamToNormalizedNodeTest.class.getResource(
51 "/complexjson/complex-json.json").toURI()));
55 * Demonstrates how to log events produced by a {@link JsonReader}.
60 public void ownStreamWriterImplementationDemonstration() throws IOException {
61 // GSON's JsonReader reading from the loaded string (our event source)
62 final JsonReader reader = new JsonReader(new StringReader(streamAsString));
64 // StreamWriter which outputs SLF4J events
65 final LoggingNormalizedNodeStreamWriter logWriter = new LoggingNormalizedNodeStreamWriter();
67 // JSON -> StreamWriter parser
68 try (final JsonParserStream jsonHandler = JsonParserStream.create(logWriter, schemaContext)) {
69 // Process multiple readers, flush()/close() as needed
70 jsonHandler.parse(reader);
75 * Demonstrates how to create an immutable NormalizedNode tree from a {@link JsonReader} and
76 * then writes the data back into string representation.
82 public void immutableNormalizedNodeStreamWriterDemonstration() throws IOException {
84 * This is the parsing part
86 // This is where we will output the nodes
87 NormalizedNodeResult result = new NormalizedNodeResult();
89 // StreamWriter which attaches NormalizedNode under parent
90 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
92 // JSON -> StreamWriter parser
93 try (JsonParserStream handler = JsonParserStream.create(streamWriter, schemaContext)) {
94 handler.parse(new JsonReader(new StringReader(streamAsString)));
97 // Finally build the node
98 final NormalizedNode<?, ?> parsedData = result.getResult();
99 LOG.debug("Parsed NormalizedNodes: {}", parsedData);
102 * This is the serialization part.
104 // We want to write the first child out
105 final DataContainerChild<? extends PathArgument, ?> firstChild = (DataContainerChild<? extends PathArgument, ?>) parsedData;
106 LOG.debug("Serializing first child: {}", firstChild);
109 final StringWriter writer = new StringWriter();
111 // StreamWriter which outputs JSON strings
112 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
114 // NormalizedNode -> StreamWriter
115 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
117 // Write multiple NormalizedNodes fluently, flush()/close() as needed
118 nodeWriter.write(firstChild).close();
120 // Just to put it somewhere
121 LOG.debug("Serialized JSON: {}", writer.toString());
124 private static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException {
125 YangContextParser parser = new YangParserImpl();
126 URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
127 final File testDir = new File(path);
128 final String[] fileList = testDir.list();
129 final List<File> testFiles = new ArrayList<File>();
130 if (fileList == null) {
131 throw new FileNotFoundException(resourceDirectory);
133 for (String fileName : fileList) {
134 if (new File(testDir, fileName).isDirectory() == false) {
135 testFiles.add(new File(testDir, fileName));
138 return parser.parseFiles(testFiles);
141 private static String loadTextFile(final File file) throws IOException {
142 FileReader fileReader = new FileReader(file);
143 BufferedReader bufReader = new BufferedReader(fileReader);
146 StringBuilder result = new StringBuilder();
147 while ((line = bufReader.readLine()) != null) {
151 return result.toString();