bb54c9c5abe62754b970349934bcb79967ce4d99
[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 java.util.Collection;
14 import java.util.HashMap;
15 import java.util.IdentityHashMap;
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.ModuleGenerator;
22 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeBuilderFactory;
23 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes;
26 import org.opendaylight.yangtools.concepts.Mutable;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
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     private final Map<Type, AugmentationSchemaNode> augmentationToSchema = new HashMap<>();
43     private final Map<Type, WithStatus> typeToSchema = new HashMap<>();
44     private final Map<QName, Type> identities = new HashMap<>();
45
46     // Note: we are keying through WithStatus, but these nodes compare on semantics, so equivalent schema nodes
47     //       can result in two distinct types. We certainly need to keep them separate.
48     private final Map<WithStatus, Type> schemaToType = new IdentityHashMap<>();
49
50     private BindingRuntimeTypesFactory() {
51         // Hidden on purpose
52     }
53
54     static @NonNull BindingRuntimeTypes createTypes(final @NonNull EffectiveModelContext context) {
55         final Collection<ModuleGenerator> moduleGens = new GeneratorReactor(context)
56             .execute(TypeBuilderFactory.runtime())
57             .values();
58
59         final Stopwatch sw = Stopwatch.createStarted();
60         final BindingRuntimeTypesFactory factory = new BindingRuntimeTypesFactory();
61         factory.indexTypes(moduleGens);
62         LOG.debug("Indexed {} generators in {}", moduleGens.size(), sw);
63
64         return new BindingRuntimeTypes(context, factory.augmentationToSchema, factory.typeToSchema,
65             factory.schemaToType, factory.identities);
66     }
67
68     private void indexTypes(final Iterable<? extends Generator> generators) {
69         for (Generator gen : generators) {
70             gen.generatedType().ifPresent(type -> indexType(gen, type));
71             indexTypes(gen);
72         }
73     }
74
75     private void indexType(final @NonNull Generator generator, final @NonNull GeneratedType type) {
76         if (generator instanceof AbstractExplicitGenerator) {
77             final EffectiveStatement<?, ?> stmt = ((AbstractExplicitGenerator<?>) generator).statement();
78             if (stmt instanceof IdentityEffectiveStatement) {
79                 identities.put(((IdentityEffectiveStatement) stmt).argument(), type);
80             } else if (stmt instanceof AugmentEffectiveStatement) {
81                 verify(stmt instanceof AugmentationSchemaNode, "Unexpected statement %s", stmt);
82                 augmentationToSchema.put(type, (AugmentationSchemaNode) stmt);
83             }
84
85             final WithStatus schema;
86             if (stmt instanceof TypedDataSchemaNode) {
87                 schema = ((TypedDataSchemaNode) stmt).getType();
88             } else if (stmt instanceof TypedefEffectiveStatement) {
89                 schema = ((TypedefEffectiveStatement) stmt).getTypeDefinition();
90             } else if (stmt instanceof WithStatus) {
91                 schema = (WithStatus) stmt;
92             } else {
93                 return;
94             }
95
96             typeToSchema.put(type, schema);
97             final var prevType = schemaToType.put(schema, type);
98             verify(prevType == null, "Conflicting types %s and %s on %s", type, prevType, schema);
99         }
100     }
101 }