Simplify ModuleInfoSnapshotBuilder
[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 com.google.common.annotations.Beta;
11 import com.google.common.base.Throwables;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.stream.Collectors;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
17 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
18 import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
19 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
20 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
21 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
24 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
25
26 /**
27  * Simple helpers to help with reconstruction of BindingRuntimeContext from generated binding classes. These involve
28  * reflection and YANG model assembly, hence should not be used without any caching whatsoever or any support for
29  * dynamic schema updates.
30  */
31 @Beta
32 public final class BindingRuntimeHelpers {
33     private BindingRuntimeHelpers() {
34
35     }
36
37     public static @NonNull EffectiveModelContext createEffectiveModel(final Class<?>... classes) {
38         return createEffectiveModel(Arrays.stream(classes)
39             .map(BindingRuntimeHelpers::extractYangModuleInfo)
40             .collect(Collectors.toList()));
41     }
42
43     public static @NonNull EffectiveModelContext createEffectiveModel(
44             final Iterable<? extends YangModuleInfo> moduleInfos) {
45         try {
46             return createEffectiveModel(ServiceLoaderState.ParserFactory.INSTANCE, moduleInfos);
47         } catch (YangParserException e) {
48             throw new IllegalStateException("Failed to parse models", e);
49         }
50     }
51
52     public static @NonNull EffectiveModelContext createEffectiveModel(final YangParserFactory parserFactory,
53             final Iterable<? extends YangModuleInfo> moduleInfos) throws YangParserException {
54         return prepareContext(parserFactory, moduleInfos).getEffectiveModelContext();
55     }
56
57     public static @NonNull BindingRuntimeContext createRuntimeContext() {
58         final ModuleInfoSnapshot infos;
59         try {
60             infos = prepareContext(ServiceLoaderState.ParserFactory.INSTANCE,
61                 BindingReflections.loadModuleInfos());
62         } catch (YangParserException e) {
63             throw new IllegalStateException("Failed to parse models", e);
64         }
65         return new DefaultBindingRuntimeContext(ServiceLoaderState.Generator.INSTANCE.generateTypeMapping(
66             infos.getEffectiveModelContext()), infos);
67     }
68
69     public static @NonNull BindingRuntimeContext createRuntimeContext(final Class<?>... classes) {
70         try {
71             return createRuntimeContext(ServiceLoaderState.ParserFactory.INSTANCE,
72                 ServiceLoaderState.Generator.INSTANCE, classes);
73         } catch (YangParserException e) {
74             throw new IllegalStateException("Failed to parse models", e);
75         }
76     }
77
78     public static @NonNull BindingRuntimeContext createRuntimeContext(final YangParserFactory parserFactory,
79             final BindingRuntimeGenerator generator, final Class<?>... classes) throws YangParserException {
80         return createRuntimeContext(parserFactory, generator, Arrays.asList(classes));
81     }
82
83     public static @NonNull BindingRuntimeContext createRuntimeContext(final YangParserFactory parserFactory,
84             final BindingRuntimeGenerator generator, final Collection<Class<?>> classes) throws YangParserException {
85         final ModuleInfoSnapshot infos = prepareContext(parserFactory, classes.stream()
86             .map(BindingRuntimeHelpers::extractYangModuleInfo)
87             .collect(Collectors.toList()));
88         return new DefaultBindingRuntimeContext(generator.generateTypeMapping(infos.getEffectiveModelContext()), infos);
89     }
90
91     @SuppressWarnings("checkstyle:IllegalCatch")
92     static @NonNull YangModuleInfo extractYangModuleInfo(final Class<?> clazz) {
93         try {
94             return BindingReflections.getModuleInfo(clazz);
95         } catch (Exception e) {
96             Throwables.throwIfUnchecked(e);
97             throw new IllegalStateException("Failed to extract module info from " + clazz, e);
98         }
99     }
100
101     private static @NonNull ModuleInfoSnapshot prepareContext(final YangParserFactory parserFactory,
102             final Iterable<? extends YangModuleInfo> moduleInfos) throws YangParserException {
103         return new ModuleInfoSnapshotBuilder(parserFactory).add(moduleInfos).build();
104     }
105 }