aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/devicemanager-o-ran-sc/o-ran/ru-fh/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/oran/impl/dom/YangParserTestUtils.java
blob: 80c18aef2d75e5cdb00b8a6baeef9460c623ec81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl.dom;

import com.google.common.annotations.Beta;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.YangConstants;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.parser.api.YangParser;
import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
import org.opendaylight.yangtools.yang.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * Utility class which provides convenience methods for producing effective schema context based on the supplied
 * yang/yin sources or paths to these sources.
 */
@Beta
public final class YangParserTestUtils {

    private static final FileFilter YANG_FILE_FILTER = file -> {
        // Locale keeps SpotBugs happy. It should not matter that much anyway.
        final String name = file.getName().toLowerCase(Locale.ENGLISH);
        return name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
    };

    private static final @NonNull YangParserFactory PARSER_FACTORY;

    static {
        final Iterator<@NonNull YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
        if (!it.hasNext()) {
            throw new IllegalStateException("No YangParserFactory found");
        }
        PARSER_FACTORY = it.next();
    }

    private YangParserTestUtils() {
        // Hidden on purpose
    }

    /**
     * Creates a new effective schema context containing the specified YANG source. Statement parser mode is set to
     * default mode and all YANG features are supported.
     *
     * @param resource relative path to the YANG file to be parsed
     *
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResource(final String resource) {
        return parseYangResource(resource, YangParserConfiguration.DEFAULT);
    }

    /**
     * Creates a new effective schema context containing the specified YANG source. All YANG features are supported.
     *
     * @param resource relative path to the YANG file to be parsed
     * @param parserMode mode of statement parser
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResource(final String resource, final YangParserConfiguration config) {
        return parseYangResource(resource, config, null);
    }

    /**
     * Creates a new effective schema context containing the specified YANG source. Statement parser mode is set to
     * default mode.
     *
     * @param resource relative path to the YANG file to be parsed
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          model are resolved
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResource(final String resource, final Set<QName> supportedFeatures) {
        return parseYangResource(resource, YangParserConfiguration.DEFAULT, supportedFeatures);
    }

    /**
     * Creates a new effective schema context containing the specified YANG source.
     *
     * @param resource relative path to the YANG file to be parsed
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          model are resolved
     * @param parserMode mode of statement parser
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResource(final String resource, final YangParserConfiguration config,
            final Set<QName> supportedFeatures) {
        final YangTextSchemaSource source = YangTextSchemaSource.forResource(YangParserTestUtils.class, resource);
        return parseYangSources(config, supportedFeatures, source);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
     * default mode and all YANG features are supported.
     *
     * @param files YANG files to be parsed
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangFiles(final File... files) {
        return parseYangFiles(Arrays.asList(files));
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
     * default mode and all YANG features are supported.
     *
     * @param files collection of YANG files to be parsed
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangFiles(final Collection<File> files) {
        return parseYangFiles(YangParserConfiguration.DEFAULT, files);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
     * default mode.
     *
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          models are resolved
     * @param files YANG files to be parsed
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangFiles(final Set<QName> supportedFeatures, final File... files) {
        return parseYangFiles(supportedFeatures, Arrays.asList(files));
    }

    public static EffectiveModelContext parseYangFiles(final Set<QName> supportedFeatures,
            final Collection<File> files) {
        return parseYangFiles(supportedFeatures, YangParserConfiguration.DEFAULT, files);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
     *
     * @param parserMode mode of statement parser
     * @param files YANG files to be parsed
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config, final File... files) {
        return parseYangFiles(config, Arrays.asList(files));
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
     *
     * @param parserMode mode of statement parser
     * @param files collection of YANG files to be parsed
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config,
            final Collection<File> files) {
        return parseYangFiles(null, config, files);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources.
     *
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          models are resolved
     * @param parserMode mode of statement parser
     * @param files YANG files to be parsed
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangFiles(final Set<QName> supportedFeatures,
    		final YangParserConfiguration config, final File... files) {
        return parseYangFiles(supportedFeatures, config, Arrays.asList(files));
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources.
     *
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          models are resolved
     * @param parserMode mode of statement parser
     * @param files YANG files to be parsed
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangFiles(final Set<QName> supportedFeatures,
    		final YangParserConfiguration config, final Collection<File> files) {
        return parseSources(config, supportedFeatures,
            files.stream().map(e->e.toPath()).map(YangTextSchemaSource::forPath).collect(Collectors.toList()));
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
     * default mode and all YANG features are supported.
     *
     * @param resourcePath relative path to the directory with YANG files to be parsed
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath) {
        return parseYangResourceDirectory(resourcePath, YangParserConfiguration.DEFAULT);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
     *
     * @param resourcePath relative path to the directory with YANG files to be parsed
     * @param parserMode mode of statement parser
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath,
    		final YangParserConfiguration config) {
        return parseYangResourceDirectory(resourcePath, null, config);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
     * default mode.
     *
     * @param resourcePath relative path to the directory with YANG files to be parsed
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          models are resolved
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath,
            final Set<QName> supportedFeatures) {
        return parseYangResourceDirectory(resourcePath, supportedFeatures, YangParserConfiguration.DEFAULT);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources.
     *
     * @param resourcePath relative path to the directory with YANG files to be parsed
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          models are resolved
     * @param parserMode mode of statement parser
     * @return effective schema context
     */
    @SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "Wrong inferent on listFiles")
    public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath,
            final Set<QName> supportedFeatures, final YangParserConfiguration config) {
        final URI directoryPath;
        try {
            directoryPath = YangParserTestUtils.class.getResource(resourcePath).toURI();
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Failed to open resource " + resourcePath, e);
        }
        return parseYangFiles(supportedFeatures, config, new File(directoryPath).listFiles(YANG_FILE_FILTER));
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
     * default mode and all YANG features are supported.
     *
     * @param clazz Resource lookup base
     * @param resources Resource names to be looked up
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResources(final Class<?> clazz, final String... resources) {
        return parseYangResources(clazz, Arrays.asList(resources));
    }

    public static EffectiveModelContext parseYangResources(final Class<?> clazz, final Collection<String> resources) {
        final List<YangTextSchemaSource> sources = new ArrayList<>(resources.size());
        for (final String r : resources) {
            sources.add(YangTextSchemaSource.forResource(clazz, r));
        }
        return parseSources(YangParserConfiguration.DEFAULT, null, sources);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
     * default mode.
     *
     * @param yangDirs relative paths to the directories containing YANG files to be parsed
     * @param yangFiles relative paths to the YANG files to be parsed
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          models are resolved
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResources(final List<String> yangDirs, final List<String> yangFiles,
            final Set<QName> supportedFeatures) {
        return parseYangResources(yangDirs, yangFiles, supportedFeatures, YangParserConfiguration.DEFAULT);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
     *
     * @param yangResourceDirs relative paths to the directories containing YANG files to be parsed
     * @param yangResources relative paths to the YANG files to be parsed
     * @param statementParserMode mode of statement parser
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResources(final List<String> yangResourceDirs,
            final List<String> yangResources, final YangParserConfiguration config) {
        return parseYangResources(yangResourceDirs, yangResources, null, config);
    }

    /**
     * Creates a new effective schema context containing the specified YANG sources.
     *
     * @param yangResourceDirs relative paths to the directories containing YANG files to be parsed
     * @param yangResources relative paths to the YANG files to be parsed
     * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
     *                          models are resolved
     * @param statementParserMode mode of statement parser
     * @return effective schema context
     */
    public static EffectiveModelContext parseYangResources(final List<String> yangResourceDirs,
            final List<String> yangResources, final Set<QName> supportedFeatures,
            final YangParserConfiguration config) {
        final List<File> allYangFiles = new ArrayList<>();
        for (final String yangDir : yangResourceDirs) {
            allYangFiles.addAll(getYangFiles(yangDir));
        }

        for (final String yangFile : yangResources) {
            try {
                allYangFiles.add(new File(YangParserTestUtils.class.getResource(yangFile).toURI()));
            } catch (URISyntaxException e) {
                throw new IllegalArgumentException("Invalid resource " + yangFile, e);
            }
        }

        return parseYangFiles(supportedFeatures, config, allYangFiles);
    }

    public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
            final Set<QName> supportedFeatures, final YangTextSchemaSource... sources) {
        return parseSources(config, supportedFeatures, Arrays.asList(sources));
    }

    public static EffectiveModelContext parseSources(final YangParserConfiguration config,
            final Set<QName> supportedFeatures, final Collection<? extends SchemaSourceRepresentation> sources) {
        final YangParser parser = PARSER_FACTORY.createParser(config);
        if (supportedFeatures != null) {
            parser.setSupportedFeatures(supportedFeatures);
        }

        try {
            parser.addSources(sources);
        } catch (YangSyntaxErrorException e) {
            throw new IllegalArgumentException("Malformed source", e);
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed to read a source", e);
        }

        try {
            return parser.buildEffectiveModel();
        } catch (YangParserException e) {
            throw new IllegalStateException("Failed to assemble SchemaContext", e);
        }
    }

    @SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "Wrong inferent on listFiles")
    private static Collection<File> getYangFiles(final String resourcePath) {
        final URI directoryPath;
        try {
            directoryPath = YangParserTestUtils.class.getResource(resourcePath).toURI();
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Failed to open resource directory " + resourcePath, e);
        }
        return Arrays.asList(new File(directoryPath).listFiles(YANG_FILE_FILTER));
    }
}