Rehost BindingReflections.loadModuleInfos()
[mdsal.git] / binding / mdsal-binding-runtime-spi / src / main / java / org / opendaylight / mdsal / binding / runtime / spi / BindingRuntimeHelpers.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.runtime.spi;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.ServiceLoader;
17 import java.util.stream.Collectors;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
20 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
21 import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
22 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
23 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
24 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
25 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
28 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
29
30 /**
31  * Simple helpers to help with reconstruction of BindingRuntimeContext from generated binding classes. These involve
32  * reflection and YANG model assembly, hence should not be used without any caching whatsoever or any support for
33  * dynamic schema updates.
34  */
35 @Beta
36 public final class BindingRuntimeHelpers {
37     private BindingRuntimeHelpers() {
38         // Hidden on purpose
39     }
40
41     public static @NonNull EffectiveModelContext createEffectiveModel(final Class<?>... classes) {
42         return createEffectiveModel(Arrays.stream(classes)
43             .map(BindingRuntimeHelpers::extractYangModuleInfo)
44             .collect(Collectors.toList()));
45     }
46
47     public static @NonNull EffectiveModelContext createEffectiveModel(
48             final Iterable<? extends YangModuleInfo> moduleInfos) {
49         try {
50             return createEffectiveModel(ServiceLoaderState.ParserFactory.INSTANCE, moduleInfos);
51         } catch (YangParserException e) {
52             throw new IllegalStateException("Failed to parse models", e);
53         }
54     }
55
56     public static @NonNull EffectiveModelContext createEffectiveModel(final YangParserFactory parserFactory,
57             final Iterable<? extends YangModuleInfo> moduleInfos) throws YangParserException {
58         return prepareContext(parserFactory, moduleInfos).getEffectiveModelContext();
59     }
60
61     public static @NonNull BindingRuntimeContext createRuntimeContext() {
62         final ModuleInfoSnapshot infos;
63         try {
64             infos = prepareContext(ServiceLoaderState.ParserFactory.INSTANCE, loadModuleInfos());
65         } catch (YangParserException e) {
66             throw new IllegalStateException("Failed to parse models", e);
67         }
68         return new DefaultBindingRuntimeContext(ServiceLoaderState.Generator.INSTANCE.generateTypeMapping(
69             infos.getEffectiveModelContext()), infos);
70     }
71
72     public static @NonNull BindingRuntimeContext createRuntimeContext(final Class<?>... classes) {
73         try {
74             return createRuntimeContext(ServiceLoaderState.ParserFactory.INSTANCE,
75                 ServiceLoaderState.Generator.INSTANCE, classes);
76         } catch (YangParserException e) {
77             throw new IllegalStateException("Failed to parse models", e);
78         }
79     }
80
81     public static @NonNull BindingRuntimeContext createRuntimeContext(
82             final Collection<? extends YangModuleInfo> infos) {
83         final ModuleInfoSnapshot snapshot;
84
85         try {
86             snapshot = prepareContext(ServiceLoaderState.ParserFactory.INSTANCE, infos);
87         } catch (YangParserException e) {
88             throw new IllegalStateException("Failed to parse models", e);
89         }
90
91         return new DefaultBindingRuntimeContext(
92             ServiceLoaderState.Generator.INSTANCE.generateTypeMapping(snapshot.getEffectiveModelContext()), snapshot);
93     }
94
95     public static @NonNull BindingRuntimeContext createRuntimeContext(final YangParserFactory parserFactory,
96             final BindingRuntimeGenerator generator, final Class<?>... classes) throws YangParserException {
97         return createRuntimeContext(parserFactory, generator, Arrays.asList(classes));
98     }
99
100     public static @NonNull BindingRuntimeContext createRuntimeContext(final YangParserFactory parserFactory,
101             final BindingRuntimeGenerator generator, final Collection<Class<?>> classes) throws YangParserException {
102         final ModuleInfoSnapshot infos = prepareContext(parserFactory, classes.stream()
103             .map(BindingRuntimeHelpers::extractYangModuleInfo)
104             .collect(Collectors.toList()));
105         return new DefaultBindingRuntimeContext(generator.generateTypeMapping(infos.getEffectiveModelContext()), infos);
106     }
107
108     public static @NonNull YangModuleInfo extractYangModuleInfo(final Class<?> clazz) {
109         final var namespace = BindingReflections.findQName(clazz).getNamespace();
110         return loadModuleInfos().stream()
111             .filter(info -> namespace.equals(info.getName().getNamespace()))
112             .findFirst()
113             .orElseThrow(() -> new IllegalStateException("Failed to extract module info from " + clazz));
114     }
115
116     public static @NonNull ImmutableSet<YangModuleInfo> loadModuleInfos() {
117         final var moduleInfoSet = ImmutableSet.<YangModuleInfo>builder();
118         for (var bindingProvider : ServiceLoader.load(YangModelBindingProvider.class)) {
119             var moduleInfo = bindingProvider.getModuleInfo();
120             checkState(moduleInfo != null, "Module Info for %s is not available.", bindingProvider.getClass());
121             collectYangModuleInfo(bindingProvider.getModuleInfo(), moduleInfoSet);
122         }
123         return moduleInfoSet.build();
124     }
125
126     private static void collectYangModuleInfo(final YangModuleInfo moduleInfo,
127             final ImmutableSet.Builder<YangModuleInfo> moduleInfoSet) {
128         moduleInfoSet.add(moduleInfo);
129         for (var dependency : moduleInfo.getImportedModules()) {
130             collectYangModuleInfo(dependency, moduleInfoSet);
131         }
132     }
133
134     private static @NonNull ModuleInfoSnapshot prepareContext(final YangParserFactory parserFactory,
135             final Iterable<? extends YangModuleInfo> moduleInfos) throws YangParserException {
136         return new ModuleInfoSnapshotBuilder(parserFactory).add(moduleInfos).build();
137     }
138 }