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.rt;
10 import static java.util.Objects.requireNonNull;
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;
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;
35 AbstractCompositeRuntimeType(final GeneratedType bindingType, final S statement, final List<RuntimeType> children,
36 final List<AugmentRuntimeType> augments) {
37 super(bindingType, statement);
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);
46 mismatchedBuilder.add(aug);
49 this.augments = correctBuilder.build();
50 this.mismatchedAugments = mismatchedBuilder.build();
52 byClass = children.stream()
53 .filter(GeneratedRuntimeType.class::isInstance)
54 .map(GeneratedRuntimeType.class::cast)
55 .collect(ImmutableMap.toImmutableMap(GeneratedRuntimeType::getIdentifier, Functions.identity()));
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);
65 bySchemaTree = builder.build();
69 public final List<AugmentRuntimeType> augments() {
74 public final List<AugmentRuntimeType> mismatchedAugments() {
75 return mismatchedAugments;
79 public final RuntimeType schemaTreeChild(final QName qname) {
80 return bySchemaTree.get(requireNonNull(qname));
84 public final GeneratedRuntimeType bindingChild(final JavaTypeName typeName) {
85 return byClass.get(requireNonNull(typeName));
88 final @NonNull ImmutableCollection<RuntimeType> schemaTreeChildren() {
89 return bySchemaTree.values();