add fixes for wt sulfur
[ccsdk/features.git] / sdnr / wt / devicemanager-onap / onf14 / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / onf14 / util / Onf14DomTestUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.util;
23
24 import com.google.gson.stream.JsonReader;
25 import java.io.BufferedReader;
26 import java.io.File;
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;
52
53 public class Onf14DomTestUtils {
54
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");
59
60     private static EffectiveModelContext schemaContext;
61     private static Inference hwContainerSchema;
62     static JSONCodecFactory lhotkaCodecFactory;
63     private static String streamAsString;
64     private static NormalizedNode transformedInput;
65
66     public static void cleanup() {
67         lhotkaCodecFactory = null;
68         schemaContext = null;
69         hwContainerSchema = null;
70     }
71
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))) {
75             String line = null;
76             while ((line = bufReader.readLine()) != null) {
77                 result.append(line);
78             }
79         }
80         return result.toString();
81     }
82
83
84     public static NormalizedNode getNormalizedNodeFromJson() throws IOException, URISyntaxException {
85         schemaContext = TestYangParserUtil.parseYangResourceDirectory("/");
86         lhotkaCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext);
87         streamAsString =
88                 loadTextFile(new File(Onf14DomTestUtils.class.getResource("/ControlConstruct-data-test.json").toURI()));
89         final JsonReader reader = new JsonReader(new StringReader(streamAsString));
90
91         NormalizedNodeResult result = new NormalizedNodeResult();
92
93         // StreamWriter which attaches NormalizedNode under parent
94         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
95
96         // JSON -> StreamWriter parser
97         try (JsonParserStream handler = JsonParserStream.create(streamWriter, lhotkaCodecFactory)) {
98             handler.parse(new JsonReader(new StringReader(streamAsString)));
99         }
100
101         // Finally build the node
102         transformedInput = result.getResult();
103         return transformedInput;
104     }
105
106     public static NormalizedNode getNormalizedNodeFromXML()
107             throws XMLStreamException, URISyntaxException, IOException, SAXException {
108         schemaContext = TestYangParserUtil.parseYangResourceDirectory("/");
109         hwContainerSchema = Inference.ofDataTreePath(schemaContext, CORE_MODEL_CONTROL_CONSTRUCT_CONTAINER);
110         final InputStream resourceAsStream =
111                 Onf14DomTestUtils.class.getResourceAsStream("/ControlConstruct-data-test.xml");
112
113         /*
114          * final XMLInputFactory factory = XMLInputFactory.newInstance();
115          * XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
116          */
117         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
118
119         final NormalizedNodeResult result = new NormalizedNodeResult();
120         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
121
122         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, hwContainerSchema);
123         xmlParser.parse(reader);
124
125         xmlParser.flush();
126         xmlParser.close();
127
128         transformedInput = result.getResult();
129         return transformedInput;
130     }
131
132 }