Move cps files to root dir
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / YangUtils.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.utils;
21
22 import com.google.gson.stream.JsonReader;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.Iterator;
27 import java.util.ServiceLoader;
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.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
37 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
38 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
39 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
40 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
41
42 public class YangUtils {
43
44     private static final YangParserFactory PARSER_FACTORY;
45
46     private YangUtils() {
47         throw new IllegalStateException("Utility class");
48     }
49
50     static {
51         final Iterator<YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
52         if (!it.hasNext()) {
53             throw new IllegalStateException("No YangParserFactory found");
54         }
55         PARSER_FACTORY = it.next();
56     }
57
58     /**
59      * Parse a file containing yang modules.
60      * @param yangModelFile  a file containing one or more yang modules
61      *                   (please note the file has to have a .yang extension if not an exception will be thrown)
62      * @return a SchemaContext representing the yang model
63      * @throws IOException when the system as an IO issue
64      * @throws YangParserException when the file does not contain a valid yang structure
65      */
66     public static SchemaContext parseYangModelFile(final File yangModelFile) throws IOException, YangParserException {
67         YangTextSchemaSource yangTextSchemaSource = YangTextSchemaSource.forFile(yangModelFile);
68         final YangParser yangParser = PARSER_FACTORY
69                 .createParser(StatementParserMode.DEFAULT_MODE);
70         yangParser.addSource(yangTextSchemaSource);
71         return yangParser.buildEffectiveModel();
72     }
73
74     /**
75      * Parse a file containing json data for a certain model (schemaContext).
76      * @param jsonData a string containing json data for the given model
77      * @param schemaContext the SchemaContext for the given data
78      * @return the NormalizedNode representing the json data
79      */
80     public static NormalizedNode<?, ?> parseJsonData(final String jsonData, final SchemaContext schemaContext)
81             throws IOException {
82         JSONCodecFactory jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
83                 .getShared(schemaContext);
84         final NormalizedNodeResult normalizedNodeResult = new NormalizedNodeResult();
85         final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
86                 .from(normalizedNodeResult);
87         try (JsonParserStream jsonParserStream = JsonParserStream
88                 .create(normalizedNodeStreamWriter, jsonCodecFactory)) {
89             final JsonReader jsonReader = new JsonReader(new StringReader(jsonData));
90             jsonParserStream.parse(jsonReader);
91         }
92         return normalizedNodeResult.getResult();
93     }
94
95     public static void chopNormalizedNode(NormalizedNode<?, ?> tree) {
96         //TODO Toine Siebelink, add code from proto-type (other user story)
97     }
98
99 }