2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.repo;
10 import com.google.common.collect.SetMultimap;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.io.IOException;
13 import java.util.Arrays;
14 import java.util.stream.Collectors;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
17 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
18 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
19 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
23 public abstract class AbstractSchemaRepositoryTest {
24 static ListenableFuture<EffectiveModelContext> createSchemaContext(
25 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
26 final var sharedSchemaRepository = new SharedSchemaRepository();
27 final var requiredSources = Arrays.stream(resources)
29 final var yangSource = assertYangTextResource(resource);
30 yangSource.register(sharedSchemaRepository);
31 yangSource.setResult();
32 return yangSource.getId();
34 .collect(Collectors.toUnmodifiableList());
36 return sharedSchemaRepository
37 .createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.builder()
38 .setModulesDeviatedByModules(modulesWithSupportedDeviations)
40 .createEffectiveModelContext(requiredSources);
43 private static SettableSchemaProvider<IRSchemaSource> assertYangTextResource(final String resourceName) {
44 final IRSchemaSource yangSource;
46 yangSource = TextToIRTransformer.transformText(YangTextSchemaSource.forResource(resourceName));
47 } catch (YangSyntaxErrorException | IOException e) {
48 throw new AssertionError("Failed to parse " + resourceName, e);
50 return SettableSchemaProvider.createImmediate(yangSource, IRSchemaSource.class);