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.nio.file.Path;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Iterator;
33 import java.util.ServiceLoader;
35 import java.util.stream.Collectors;
37 import org.eclipse.jdt.annotation.NonNull;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.common.YangConstants;
40 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
41 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
42 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
43 import org.opendaylight.yangtools.yang.parser.api.YangParser;
44 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
45 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
46 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
47 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
49 public final class TestYangParserUtil {
51 private static final FileFilter YANG_FILE_FILTER = file -> {
52 final String name = file.getName();
53 return name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
56 private static final @NonNull YangParserFactory PARSER_FACTORY;
59 final Iterator<@NonNull YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
61 throw new IllegalStateException("No YangParserFactory found");
63 PARSER_FACTORY = it.next();
66 public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config, final File... files) {
67 return parseYangFiles(config, Arrays.asList(files));
69 public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config,
70 final Collection<File> files) {
71 return parseYangFiles(null, config, files.stream().map(e->e.toPath()).collect(Collectors.toList()));
74 public static EffectiveModelContext parseYangFiles(final Set<QName> supportedFeatures,
75 final YangParserConfiguration config, final Collection<Path> files) {
76 return parseSources(config, supportedFeatures,
77 files.stream().map(YangTextSchemaSource::forPath).collect(Collectors.toList()));
81 public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath) {
82 return parseYangResourceDirectory(resourcePath, YangParserConfiguration.DEFAULT);
85 public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath,
86 final YangParserConfiguration config) {
87 final URI directoryPath;
89 directoryPath = TestYangParserUtil.class.getResource(resourcePath).toURI();
90 } catch (URISyntaxException e) {
91 throw new IllegalArgumentException("Failed to open resource " + resourcePath, e);
93 return parseYangFiles(config, new File(directoryPath).listFiles(YANG_FILE_FILTER));
97 public static EffectiveModelContext parseSources(final YangParserConfiguration config,
98 final Set<QName> supportedFeatures, final Collection<? extends SchemaSourceRepresentation> sources) {
99 final YangParser parser = PARSER_FACTORY.createParser(config);
100 if (supportedFeatures != null) {
101 parser.setSupportedFeatures(supportedFeatures);
105 parser.addSources(sources);
106 } catch (YangSyntaxErrorException e) {
107 throw new IllegalArgumentException("Malformed source", e);
108 } catch (IOException e) {
109 throw new IllegalArgumentException("Failed to read a source", e);
113 return parser.buildEffectiveModel();
114 } catch (YangParserException e) {
115 throw new IllegalStateException("Failed to assemble SchemaContext", e);