d67f1cf94c7ed0f57eb17f9c2af684dbc7f1dfe1
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / rt / AbstractCompositeRuntimeType.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.rt;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Functions;
13 import com.google.common.collect.ImmutableCollection;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
19 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
20 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
21 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
22 import org.opendaylight.mdsal.binding.runtime.api.GeneratedRuntimeType;
23 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
27
28 abstract class AbstractCompositeRuntimeType<S extends EffectiveStatement<?, ?>>
29         extends AbstractRuntimeType<S, GeneratedType> implements CompositeRuntimeType {
30     private final ImmutableMap<JavaTypeName, GeneratedRuntimeType> byClass;
31     private final ImmutableMap<QName, RuntimeType> bySchemaTree;
32     private final @NonNull ImmutableList<AugmentRuntimeType> augments;
33     private final @NonNull ImmutableList<AugmentRuntimeType> mismatchedAugments;
34
35     AbstractCompositeRuntimeType(final GeneratedType bindingType, final S statement, final List<RuntimeType> children,
36             final List<AugmentRuntimeType> augments) {
37         super(bindingType, statement);
38
39         final var substatements = statement.effectiveSubstatements();
40         final var correctBuilder = ImmutableList.<AugmentRuntimeType>builder();
41         final var mismatchedBuilder = ImmutableList.<AugmentRuntimeType>builder();
42         for (var aug : augments) {
43             if (substatements.contains(aug.statement())) {
44                 correctBuilder.add(aug);
45             } else {
46                 mismatchedBuilder.add(aug);
47             }
48         }
49         this.augments = correctBuilder.build();
50         this.mismatchedAugments = mismatchedBuilder.build();
51
52         byClass = children.stream()
53             .filter(GeneratedRuntimeType.class::isInstance)
54             .map(GeneratedRuntimeType.class::cast)
55             .collect(ImmutableMap.toImmutableMap(GeneratedRuntimeType::getIdentifier, Functions.identity()));
56
57         // Note: this may be over-sized, but we typically deal with schema tree statements, hence it is kind of accurate
58         final var builder = ImmutableMap.<QName, RuntimeType>builderWithExpectedSize(children.size());
59         for (var child : children) {
60             final var stmt = child.statement();
61             if (stmt instanceof SchemaTreeEffectiveStatement) {
62                 builder.put(((SchemaTreeEffectiveStatement<?>)stmt).argument(), child);
63             }
64         }
65         bySchemaTree = builder.build();
66     }
67
68     @Override
69     public final List<AugmentRuntimeType> augments() {
70         return augments;
71     }
72
73     @Override
74     public final List<AugmentRuntimeType> mismatchedAugments() {
75         return mismatchedAugments;
76     }
77
78     @Override
79     public final RuntimeType schemaTreeChild(final QName qname) {
80         return bySchemaTree.get(requireNonNull(qname));
81     }
82
83     @Override
84     public final GeneratedRuntimeType bindingChild(final JavaTypeName typeName) {
85         return byClass.get(requireNonNull(typeName));
86     }
87
88     final @NonNull ImmutableCollection<RuntimeType> schemaTreeChildren() {
89         return bySchemaTree.values();
90     }
91 }