2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 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.devicemanager.onf14.util;
24 import com.google.gson.stream.JsonReader;
25 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.StringReader;
31 import java.net.URISyntaxException;
32 import java.nio.charset.StandardCharsets;
33 import javax.xml.stream.XMLStreamException;
34 import javax.xml.stream.XMLStreamReader;
35 import org.opendaylight.yangtools.util.xml.UntrustedXML;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.common.Revision;
39 import org.opendaylight.yangtools.yang.common.XMLNamespace;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
43 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
44 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
45 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
46 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
47 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
48 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
49 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
50 //import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; //Yangtool 8.0
51 import org.xml.sax.SAXException;
53 public class Onf14DomTestUtils {
55 private static final QNameModule CORE_MODEL_1_4_MODULE =
56 QNameModule.create(XMLNamespace.of("urn:onf:yang:core-model-1-4"), Revision.of("2019-11-27"));
57 private static final QName CORE_MODEL_CONTROL_CONSTRUCT_CONTAINER =
58 QName.create(CORE_MODEL_1_4_MODULE, "control-construct");
60 private static EffectiveModelContext schemaContext;
61 private static Inference hwContainerSchema;
62 static JSONCodecFactory lhotkaCodecFactory;
63 private static String streamAsString;
64 private static NormalizedNode transformedInput;
66 public static void cleanup() {
67 lhotkaCodecFactory = null;
69 hwContainerSchema = null;
72 static String loadTextFile(final File file) throws IOException {
73 final StringBuilder result = new StringBuilder();
74 try (BufferedReader bufReader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8))) {
76 while ((line = bufReader.readLine()) != null) {
80 return result.toString();
84 public static NormalizedNode getNormalizedNodeFromJson() throws IOException, URISyntaxException {
85 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/");
86 lhotkaCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext);
88 loadTextFile(new File(Onf14DomTestUtils.class.getResource("/ControlConstruct-data-test.json").toURI()));
89 final JsonReader reader = new JsonReader(new StringReader(streamAsString));
91 NormalizedNodeResult result = new NormalizedNodeResult();
93 // StreamWriter which attaches NormalizedNode under parent
94 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
96 // JSON -> StreamWriter parser
97 try (JsonParserStream handler = JsonParserStream.create(streamWriter, lhotkaCodecFactory)) {
98 handler.parse(new JsonReader(new StringReader(streamAsString)));
101 // Finally build the node
102 transformedInput = result.getResult();
103 return transformedInput;
106 public static NormalizedNode getNormalizedNodeFromXML()
107 throws XMLStreamException, URISyntaxException, IOException, SAXException {
108 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/");
109 hwContainerSchema = Inference.ofDataTreePath(schemaContext, CORE_MODEL_CONTROL_CONSTRUCT_CONTAINER);
110 final InputStream resourceAsStream =
111 Onf14DomTestUtils.class.getResourceAsStream("/ControlConstruct-data-test.xml");
114 * final XMLInputFactory factory = XMLInputFactory.newInstance();
115 * XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
117 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
119 final NormalizedNodeResult result = new NormalizedNodeResult();
120 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
122 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, hwContainerSchema);
123 xmlParser.parse(reader);
128 transformedInput = result.getResult();
129 return transformedInput;