2 * Copyright (c) 2021 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.mdsal.binding.generator.impl;
10 import static com.google.common.base.Verify.verify;
12 import com.google.common.base.Stopwatch;
13 import com.google.common.collect.HashMultimap;
14 import com.google.common.collect.SetMultimap;
15 import java.util.HashMap;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.binding.generator.impl.reactor.AbstractExplicitGenerator;
19 import org.opendaylight.mdsal.binding.generator.impl.reactor.Generator;
20 import org.opendaylight.mdsal.binding.generator.impl.reactor.GeneratorReactor;
21 import org.opendaylight.mdsal.binding.generator.impl.reactor.IdentityGenerator;
22 import org.opendaylight.mdsal.binding.generator.impl.reactor.InputGenerator;
23 import org.opendaylight.mdsal.binding.generator.impl.reactor.ModuleGenerator;
24 import org.opendaylight.mdsal.binding.generator.impl.reactor.OutputGenerator;
25 import org.opendaylight.mdsal.binding.generator.impl.reactor.RpcGenerator;
26 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeBuilderFactory;
27 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultBindingRuntimeTypes;
28 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
29 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
30 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes;
31 import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
32 import org.opendaylight.mdsal.binding.runtime.api.IdentityRuntimeType;
33 import org.opendaylight.mdsal.binding.runtime.api.InputRuntimeType;
34 import org.opendaylight.mdsal.binding.runtime.api.ModuleRuntimeType;
35 import org.opendaylight.mdsal.binding.runtime.api.OutputRuntimeType;
36 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
37 import org.opendaylight.yangtools.concepts.Mutable;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.common.QNameModule;
40 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 final class BindingRuntimeTypesFactory implements Mutable {
45 private static final Logger LOG = LoggerFactory.getLogger(BindingRuntimeTypesFactory.class);
47 // Modules, indexed by their QNameModule
48 private final Map<QNameModule, ModuleRuntimeType> modules = new HashMap<>();
49 // Identities, indexed by their QName
50 private final Map<QName, IdentityRuntimeType> identities = new HashMap<>();
51 // All known types, indexed by their JavaTypeName
52 private final Map<JavaTypeName, RuntimeType> allTypes = new HashMap<>();
53 // All RpcOutputs, indexed by their RPC's QName
54 private final Map<QName, OutputRuntimeType> rpcOutputs = new HashMap<>();
55 // All RpcInputs, indexed by their RPC's QName
56 private final Map<QName, InputRuntimeType> rpcInputs = new HashMap<>();
57 // All known 'choice's to their corresponding cases
58 private final SetMultimap<JavaTypeName, CaseRuntimeType> choiceToCases = HashMultimap.create();
60 private BindingRuntimeTypesFactory() {
64 static @NonNull BindingRuntimeTypes createTypes(final @NonNull EffectiveModelContext context) {
65 final var moduleGens = new GeneratorReactor(context).execute(TypeBuilderFactory.runtime());
67 final Stopwatch sw = Stopwatch.createStarted();
68 final BindingRuntimeTypesFactory factory = new BindingRuntimeTypesFactory();
69 factory.indexModules(moduleGens);
70 LOG.debug("Indexed {} generators in {}", moduleGens.size(), sw);
72 return new DefaultBindingRuntimeTypes(context, factory.modules, factory.allTypes, factory.identities,
73 factory.rpcInputs, factory.rpcOutputs, factory.choiceToCases);
76 private void indexModules(final Map<QNameModule, ModuleGenerator> moduleGens) {
77 for (var entry : moduleGens.entrySet()) {
78 final var modGen = entry.getValue();
80 // index the module's runtime type
81 safePut(modules, "modules", entry.getKey(), modGen.runtimeType().orElseThrow());
83 // index module's identities and RPC input/outputs
84 for (var gen : modGen) {
85 if (gen instanceof IdentityGenerator idGen) {
86 idGen.runtimeType().ifPresent(identity -> {
87 safePut(identities, "identities", identity.statement().argument(), identity);
90 // FIXME: do not collect these once we they generate a proper RuntimeType
91 if (gen instanceof RpcGenerator rpcGen) {
92 final QName rpcName = rpcGen.statement().argument();
93 for (var subgen : gen) {
94 if (subgen instanceof InputGenerator inputGen) {
95 inputGen.runtimeType().ifPresent(input -> rpcInputs.put(rpcName, input));
96 } else if (subgen instanceof OutputGenerator outputGen) {
97 outputGen.runtimeType().ifPresent(output -> rpcOutputs.put(rpcName, output));
104 indexRuntimeTypes(moduleGens.values());
107 private void indexRuntimeTypes(final Iterable<? extends Generator> generators) {
108 for (Generator gen : generators) {
109 if (gen instanceof AbstractExplicitGenerator<?, ?> explicitGen && gen.generatedType().isPresent()) {
110 final var type = explicitGen.runtimeType().orElseThrow();
111 if (type.javaType() instanceof GeneratedType genType) {
112 final var name = genType.getIdentifier();
113 final var prev = allTypes.put(name, type);
114 verify(prev == null || prev == type, "Conflict on runtime type mapping of %s between %s and %s",
117 // Global indexing of cases generated for a particular choice. We look at the Generated type
118 // and make assumptions about its shape -- which works just fine without touching the
119 // ChoiceRuntimeType for cases.
120 if (type instanceof CaseRuntimeType caseType) {
121 final var ifaces = genType.getImplements();
122 // The appropriate choice and DataObject at the very least. The choice interface is the first
124 verify(ifaces.size() >= 2, "Unexpected implemented interfaces %s", ifaces);
125 choiceToCases.put(ifaces.get(0).getIdentifier(), caseType);
129 indexRuntimeTypes(gen);
133 private static <K, V> void safePut(final Map<K, V> map, final String name, final K key, final V value) {
134 final var prev = map.put(key, value);
135 verify(prev == null, "Conflict in %s, key %s conflicts on %s versus %s", name, key, prev, value);