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 / 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.onf14.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.nio.file.Path;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Iterator;
33 import java.util.ServiceLoader;
34 import java.util.Set;
35 import java.util.stream.Collectors;
36
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;
48
49 public final class TestYangParserUtil {
50
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();
54         };
55
56         private static final @NonNull YangParserFactory PARSER_FACTORY;
57
58         static {
59                 final Iterator<@NonNull YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
60                 if (!it.hasNext()) {
61                         throw new IllegalStateException("No YangParserFactory found");
62                 }
63                 PARSER_FACTORY = it.next();
64         }
65
66         public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config, final File... files) {
67                 return parseYangFiles(config, Arrays.asList(files));
68         }
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()));
72         }
73
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()));
78         }
79
80
81         public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath) {
82                 return parseYangResourceDirectory(resourcePath, YangParserConfiguration.DEFAULT);
83         }
84
85         public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath,
86                         final YangParserConfiguration config) {
87                 final URI directoryPath;
88                 try {
89                         directoryPath = TestYangParserUtil.class.getResource(resourcePath).toURI();
90                 } catch (URISyntaxException e) {
91                         throw new IllegalArgumentException("Failed to open resource " + resourcePath, e);
92                 }
93                 return parseYangFiles(config, new File(directoryPath).listFiles(YANG_FILE_FILTER));
94         }
95
96
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);
102                 }
103
104                 try {
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);
110                 }
111
112                 try {
113                         return parser.buildEffectiveModel();
114                 } catch (YangParserException e) {
115                         throw new IllegalStateException("Failed to assemble SchemaContext", e);
116                 }
117         }
118
119 }