fix lic issue
[ccsdk/features.git] / sdnr / wt / devicemanager-o-ran-sc / o-ran / ru-fh / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / oran / impl / dom / util / TestYangParserUtil.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2022 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.oran.impl.dom.util;
23
24 import java.io.File;
25 import java.io.FileFilter;
26 import java.io.IOException;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Iterator;
32 import java.util.ServiceLoader;
33 import java.util.Set;
34 import java.util.stream.Collectors;
35
36 import org.eclipse.jdt.annotation.NonNull;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.YangConstants;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
41 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
42 import org.opendaylight.yangtools.yang.parser.api.YangParser;
43 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
44 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
45 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
46 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
47
48 public final class TestYangParserUtil {
49
50         private static final FileFilter YANG_FILE_FILTER = file -> {
51                 final String name = file.getName();
52                 return name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
53         };
54
55         private static final @NonNull YangParserFactory PARSER_FACTORY;
56
57         static {
58                 final Iterator<@NonNull YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
59                 if (!it.hasNext()) {
60                         throw new IllegalStateException("No YangParserFactory found");
61                 }
62                 PARSER_FACTORY = it.next();
63         }
64
65         public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config, final File... files) {
66                 return parseYangFiles(config, Arrays.asList(files));
67         }
68
69         public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config,
70                         final Collection<File> files) {
71                 return parseSources(config, files.stream().map(YangTextSchemaSource::forFile).collect(Collectors.toList()));
72         }
73
74         public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath) {
75                 return parseYangResourceDirectory(resourcePath, YangParserConfiguration.DEFAULT);
76         }
77
78         public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath,
79                         final YangParserConfiguration config) {
80                 final URI directoryPath;
81                 try {
82                         directoryPath = TestYangParserUtil.class.getResource(resourcePath).toURI();
83                 } catch (URISyntaxException e) {
84                         throw new IllegalArgumentException("Failed to open resource " + resourcePath, e);
85                 }
86                 return parseYangFiles(config, new File(directoryPath).listFiles(YANG_FILE_FILTER));
87         }
88
89         public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
90                         final Set<QName> supportedFeatures, final YangTextSchemaSource... sources) {
91                 return parseSources(config, Arrays.asList(sources));
92         }
93
94         public static EffectiveModelContext parseSources(final YangParserConfiguration config,
95                         final Collection<? extends SchemaSourceRepresentation> sources) {
96                 final YangParser parser = PARSER_FACTORY.createParser(config);
97
98                 try {
99                         parser.addSources(sources);
100                 } catch (YangSyntaxErrorException e) {
101                         throw new IllegalArgumentException("Malformed source", e);
102                 } catch (IOException e) {
103                         throw new IllegalArgumentException("Failed to read a source", e);
104                 }
105
106                 try {
107                         return parser.buildEffectiveModel();
108                 } catch (YangParserException e) {
109                         throw new IllegalStateException("Failed to assemble SchemaContext", e);
110                 }
111         }
112 }