8d28821b532e1febe233272966b5de3ea8a291b0
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / BindingRuntimeTypesFactory.java
1 /*
2  * Copyright (c) 2021 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.generator.impl;
9
10 import static com.google.common.base.Verify.verify;
11
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;
16 import java.util.Map;
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.ModuleGenerator;
23 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeBuilderFactory;
24 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultBindingRuntimeTypes;
25 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
26 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
27 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes;
28 import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
29 import org.opendaylight.mdsal.binding.runtime.api.IdentityRuntimeType;
30 import org.opendaylight.mdsal.binding.runtime.api.ModuleRuntimeType;
31 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
32 import org.opendaylight.yangtools.concepts.Mutable;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 final class BindingRuntimeTypesFactory implements Mutable {
40     private static final Logger LOG = LoggerFactory.getLogger(BindingRuntimeTypesFactory.class);
41
42     // Modules, indexed by their QNameModule
43     private final Map<QNameModule, ModuleRuntimeType> modules = new HashMap<>();
44     // Identities, indexed by their QName
45     private final Map<QName, IdentityRuntimeType> identities = new HashMap<>();
46     // All known types, indexed by their JavaTypeName
47     private final Map<JavaTypeName, RuntimeType> allTypes = new HashMap<>();
48     // All known 'choice's to their corresponding cases
49     private final SetMultimap<JavaTypeName, CaseRuntimeType> choiceToCases = HashMultimap.create();
50
51     private BindingRuntimeTypesFactory() {
52         // Hidden on purpose
53     }
54
55     static @NonNull BindingRuntimeTypes createTypes(final @NonNull EffectiveModelContext context) {
56         final var moduleGens = new GeneratorReactor(context).execute(TypeBuilderFactory.runtime());
57
58         final var sw = Stopwatch.createStarted();
59         final var factory = new BindingRuntimeTypesFactory();
60         factory.indexModules(moduleGens);
61         LOG.debug("Indexed {} generators in {}", moduleGens.size(), sw);
62
63         return new DefaultBindingRuntimeTypes(context, factory.modules, factory.allTypes, factory.identities,
64             factory.choiceToCases);
65     }
66
67     private void indexModules(final Map<QNameModule, ModuleGenerator> moduleGens) {
68         for (var entry : moduleGens.entrySet()) {
69             final var modGen = entry.getValue();
70
71             // index the module's runtime type
72             safePut(modules, "modules", entry.getKey(), modGen.getRuntimeType());
73
74             // index module's identities and RPC input/outputs
75             for (var gen : modGen) {
76                 if (gen instanceof IdentityGenerator idGen) {
77                     safePut(identities, "identities", idGen.statement().argument(), idGen.getRuntimeType());
78                 }
79             }
80         }
81
82         indexRuntimeTypes(moduleGens.values());
83     }
84
85     private void indexRuntimeTypes(final Iterable<? extends Generator> generators) {
86         for (var gen : generators) {
87             if (gen instanceof AbstractExplicitGenerator<?, ?> explicit) {
88                 final var type = explicit.generatedRuntimeType();
89                 if (type != null && type.javaType() instanceof GeneratedType genType) {
90                     final var name = genType.getIdentifier();
91                     final var prev = allTypes.put(name, type);
92                     verify(prev == null || prev == type, "Conflict on runtime type mapping of %s between %s and %s",
93                         name, prev, type);
94
95                     // Global indexing of cases generated for a particular choice. We look at the Generated type
96                     // and make assumptions about its shape -- which works just fine without touching
97                     // the ChoiceRuntimeType for cases.
98                     if (type instanceof CaseRuntimeType caseType) {
99                         final var ifaces = genType.getImplements();
100                         // The appropriate choice and DataObject at the very least. The choice interface is the first
101                         // one mentioned.
102                         verify(ifaces.size() >= 2, "Unexpected implemented interfaces %s", ifaces);
103                         choiceToCases.put(ifaces.get(0).getIdentifier(), caseType);
104                     }
105                 }
106             }
107             indexRuntimeTypes(gen);
108         }
109     }
110
111     private static <K, V> void safePut(final Map<K, V> map, final String name, final K key, final V value) {
112         final var prev = map.put(key, value);
113         verify(prev == null, "Conflict in %s, key %s conflicts on %s versus %s", name, key, prev, value);
114     }
115 }