From 25423c50e504676f15c7a57c03aad40bfc35c7e6 Mon Sep 17 00:00:00 2001 From: Michael Dürre Date: Wed, 20 Jul 2022 09:32:50 +0200 Subject: migrate sdnr features to sulfur MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix sdnr code for sulfur Issue-ID: CCSDK-3692 Signed-off-by: Michael Dürre Change-Id: I0a62ade424bb978222e7ce6450215fb327f957b7 Signed-off-by: Michael Dürre --- sdnr/wt/devicemanager-onap/onf14/provider/pom.xml | 38 +- .../dataprovider/Onf14DomToInternalDataModel.java | 5 +- .../onf14/util/Onf14DomTestUtils.java | 4 +- .../onf14/util/TestYangParserUtil.java | 112 ------ .../onf14/util/YangParserTestUtils.java | 382 +++++++++++++++++++++ 5 files changed, 416 insertions(+), 125 deletions(-) delete mode 100644 sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/TestYangParserUtil.java create mode 100644 sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/YangParserTestUtils.java (limited to 'sdnr/wt/devicemanager-onap/onf14/provider') diff --git a/sdnr/wt/devicemanager-onap/onf14/provider/pom.xml b/sdnr/wt/devicemanager-onap/onf14/provider/pom.xml index b1040dedd..4714fe94c 100644 --- a/sdnr/wt/devicemanager-onap/onf14/provider/pom.xml +++ b/sdnr/wt/devicemanager-onap/onf14/provider/pom.xml @@ -22,13 +22,14 @@ ~ ============LICENSE_END======================================================= ~ --> + 4.0.0 org.onap.ccsdk.parent binding-parent - 2.4.0 + 2.4.1-SNAPSHOT @@ -53,14 +54,6 @@ - - - org.mockito - mockito-core - test - - - ${project.groupId} sdnr-wt-devicemanager-onap-onf14-model @@ -71,7 +64,25 @@ sdnr-wt-devicemanager-core-model ${project.version} provided + + + ${project.groupId} + sdnr-wt-websocketmanager-model + ${project.version} + provided + + + ${project.groupId} + sdnr-wt-data-provider-model + ${project.version} + provided + + ${project.groupId} + sdnr-wt-common + ${project.version} + provided + org.opendaylight.mdsal mdsal-binding-dom-codec-api @@ -86,7 +97,6 @@ org.opendaylight.netconf sal-netconf-connector - 2.0.4 provided @@ -99,6 +109,13 @@ rfc6991-ietf-yang-types provided + + + org.mockito + mockito-core + test + + @@ -112,6 +129,7 @@ org.opendaylight.yangtools yang-maven-plugin + 7.0.9 binding diff --git a/sdnr/wt/devicemanager-onap/onf14/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/dom/impl/dataprovider/Onf14DomToInternalDataModel.java b/sdnr/wt/devicemanager-onap/onf14/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/dom/impl/dataprovider/Onf14DomToInternalDataModel.java index 8a75bbc11..2a931378f 100644 --- a/sdnr/wt/devicemanager-onap/onf14/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/dom/impl/dataprovider/Onf14DomToInternalDataModel.java +++ b/sdnr/wt/devicemanager-onap/onf14/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/dom/impl/dataprovider/Onf14DomToInternalDataModel.java @@ -2,8 +2,11 @@ package org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.dataprovide import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; + import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.util.Onf14DMDOMUtility; import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.util.Onf14DevicemanagerQNames; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory; @@ -35,7 +38,7 @@ public class Onf14DomToInternalDataModel { inventoryBuilder.setUuid(Onf14DMDOMUtility.getUuidFromEquipment(currentEq)); inventoryBuilder.setParentUuid(parentUuid); - List containedHolderKeyList = new ArrayList<>(); + Set containedHolderKeyList = new HashSet<>(); MapNode containedHolderMap = (MapNode) currentEq .childByArg(new NodeIdentifier(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT_CONTAINED_HOLDER)); if (containedHolderMap != null) { diff --git a/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/Onf14DomTestUtils.java b/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/Onf14DomTestUtils.java index ad79a5c43..6dc9b50b5 100644 --- a/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/Onf14DomTestUtils.java +++ b/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/Onf14DomTestUtils.java @@ -82,7 +82,7 @@ public class Onf14DomTestUtils { public static NormalizedNode getNormalizedNodeFromJson() throws IOException, URISyntaxException { - schemaContext = TestYangParserUtil.parseYangResourceDirectory("/"); + schemaContext = YangParserTestUtils.parseYangResourceDirectory("/"); lhotkaCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext); streamAsString = loadTextFile(new File(Onf14DomTestUtils.class.getResource("/ControlConstruct-data-test.json").toURI())); @@ -105,7 +105,7 @@ public class Onf14DomTestUtils { public static NormalizedNode getNormalizedNodeFromXML() throws XMLStreamException, URISyntaxException, IOException, SAXException { - schemaContext = TestYangParserUtil.parseYangResourceDirectory("/"); + schemaContext = YangParserTestUtils.parseYangResourceDirectory("/"); hwContainerSchema = Inference.ofDataTreePath(schemaContext, CORE_MODEL_CONTROL_CONSTRUCT_CONTAINER); final InputStream resourceAsStream = Onf14DomTestUtils.class.getResourceAsStream("/ControlConstruct-data-test.xml"); diff --git a/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/TestYangParserUtil.java b/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/TestYangParserUtil.java deleted file mode 100644 index a0cfa6760..000000000 --- a/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/TestYangParserUtil.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2022 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - */ -package org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.util; - -import java.io.File; -import java.io.FileFilter; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -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.model.repo.api.SchemaSourceRepresentation; -import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; -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; - -public final class TestYangParserUtil { - - private static final FileFilter YANG_FILE_FILTER = file -> { - final String name = file.getName(); - 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(); - } - - public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config, final File... files) { - return parseYangFiles(config, Arrays.asList(files)); - } - - public static EffectiveModelContext parseYangFiles(final YangParserConfiguration config, - final Collection files) { - return parseSources(config, files.stream().map(YangTextSchemaSource::forFile).collect(Collectors.toList())); - } - - public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath) { - return parseYangResourceDirectory(resourcePath, YangParserConfiguration.DEFAULT); - } - - public static EffectiveModelContext parseYangResourceDirectory(final String resourcePath, - final YangParserConfiguration config) { - final URI directoryPath; - try { - directoryPath = TestYangParserUtil.class.getResource(resourcePath).toURI(); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Failed to open resource " + resourcePath, e); - } - return parseYangFiles(config, new File(directoryPath).listFiles(YANG_FILE_FILTER)); - } - - public static EffectiveModelContext parseYangSources(final YangParserConfiguration config, - final Set supportedFeatures, final YangTextSchemaSource... sources) { - return parseSources(config, Arrays.asList(sources)); - } - - public static EffectiveModelContext parseSources(final YangParserConfiguration config, - final Collection sources) { - final YangParser parser = PARSER_FACTORY.createParser(config); - - 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); - } - } -} diff --git a/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/YangParserTestUtils.java b/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/YangParserTestUtils.java new file mode 100644 index 000000000..ee20dccae --- /dev/null +++ b/sdnr/wt/devicemanager-onap/onf14/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/onf14/util/YangParserTestUtils.java @@ -0,0 +1,382 @@ +/* + * 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.onf14.util; + +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.nio.file.Path; +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 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 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 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 supportedFeatures, final File... files) { + return parseYangFiles(supportedFeatures, Arrays.asList(files)); + } + + public static EffectiveModelContext parseYangFiles(final Set supportedFeatures, + final Collection files) { + return parseYangFiles(supportedFeatures, YangParserConfiguration.DEFAULT, files.stream().map(e->e.toPath()).collect(Collectors.toList())); + } + + /** + * 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 files) { + return parseYangFiles(null, config, files.stream().map(e->e.toPath()).collect(Collectors.toList())); + } + + /** + * 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 supportedFeatures, + final YangParserConfiguration config, final Path... 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 supportedFeatures, + final YangParserConfiguration config, final Collection files) { + return parseSources(config, supportedFeatures, + files.stream().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 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 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); + } + File[] files = new File(directoryPath).listFiles(YANG_FILE_FILTER); + return parseYangFiles(supportedFeatures, config, Arrays.asList(files).stream().map(e->e.toPath()).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 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 resources) { + final List 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 yangDirs, final List yangFiles, + final Set 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 yangResourceDirs, + final List 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 yangResourceDirs, + final List yangResources, final Set supportedFeatures, + final YangParserConfiguration config) { + final List 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.stream().map(e->e.toPath()).collect(Collectors.toList())); + } + + public static EffectiveModelContext parseYangSources(final YangParserConfiguration config, + final Set supportedFeatures, final YangTextSchemaSource... sources) { + return parseSources(config, supportedFeatures, Arrays.asList(sources)); + } + + public static EffectiveModelContext parseSources(final YangParserConfiguration config, + final Set supportedFeatures, final Collection 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 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)); + } +} -- cgit 1.2.3-korg