2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom;
24 import com.google.gson.stream.JsonReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
31 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
32 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 public class DomParser {
40 * Parsing JSON file into {@link NormalizedNode}.
42 * @param path path to the file
43 * @param schemaContext schema context
44 * @return created {@link NormalizedNode}
46 public static NormalizedNode parseJsonFile(final String path, final EffectiveModelContext schemaContext) {
47 final JSONCodecFactory codecFactory = JSONCodecFactorySupplier.RFC7951.createSimple(schemaContext);
48 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
49 try (NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
50 JsonParserStream jsonParser = JsonParserStream.create(writer, codecFactory);
51 InputStream inputStream = NetconfDomAccessorImpl.class.getResourceAsStream(path);
52 JsonReader reader = new JsonReader(new InputStreamReader(inputStream))) {
53 jsonParser.parse(reader);
54 } catch (IOException e) {
55 throw new IllegalStateException("Failed to parse input JSON file: " + path, e);
57 return resultHolder.getResult();