2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2022 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.oran.impl.dom.util;
25 import java.io.FileFilter;
26 import java.io.IOException;
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;
34 import java.util.stream.Collectors;
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;
48 public final class TestYangParserUtil {
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();
55 private static final @NonNull YangParserFactory PARSER_FACTORY;
58 final Iterator<@NonNull YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
60 throw new IllegalStateException("No YangParserFactory found");
62 PARSER_FACTORY = it.next();
65 public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config, final File... files) {
66 return parseYangFiles(config, Arrays.asList(files));
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()));
74 public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath) {
75 return parseYangResourceDirectory(resourcePath, YangParserConfiguration.DEFAULT);
78 public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath,
79 final YangParserConfiguration config) {
80 final URI directoryPath;
82 directoryPath = TestYangParserUtil.class.getResource(resourcePath).toURI();
83 } catch (URISyntaxException e) {
84 throw new IllegalArgumentException("Failed to open resource " + resourcePath, e);
86 return parseYangFiles(config, new File(directoryPath).listFiles(YANG_FILE_FILTER));
89 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
90 final Set<QName> supportedFeatures, final YangTextSchemaSource... sources) {
91 return parseSources(config, Arrays.asList(sources));
94 public static EffectiveModelContext parseSources(final YangParserConfiguration config,
95 final Collection<? extends SchemaSourceRepresentation> sources) {
96 final YangParser parser = PARSER_FACTORY.createParser(config);
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);
107 return parser.buildEffectiveModel();
108 } catch (YangParserException e) {
109 throw new IllegalStateException("Failed to assemble SchemaContext", e);