2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.MoreObjects;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableSetMultimap;
15 import com.google.common.collect.ImmutableSortedMap;
16 import com.google.common.collect.Maps;
17 import com.google.common.collect.SetMultimap;
19 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
23 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes;
24 import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
25 import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType;
26 import org.opendaylight.mdsal.binding.runtime.api.GeneratedRuntimeType;
27 import org.opendaylight.mdsal.binding.runtime.api.IdentityRuntimeType;
28 import org.opendaylight.mdsal.binding.runtime.api.ModuleRuntimeType;
29 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
30 import org.opendaylight.mdsal.binding.runtime.api.YangDataRuntimeType;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.common.YangDataName;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 * The result of BindingGenerator run. Contains mapping between Types and SchemaNodes.
39 public final class DefaultBindingRuntimeTypes implements BindingRuntimeTypes {
40 private final @NonNull EffectiveModelContext context;
41 private final ImmutableSetMultimap<JavaTypeName, CaseRuntimeType> choiceToCases;
42 private final ImmutableMap<QNameModule, ModuleRuntimeType> modulesByNamespace;
43 private final ImmutableSortedMap<String, ModuleRuntimeType> modulesByPackage;
44 private final ImmutableMap<QName, IdentityRuntimeType> identities;
45 private final ImmutableMap<JavaTypeName, RuntimeType> types;
47 public DefaultBindingRuntimeTypes(final EffectiveModelContext context,
48 final Map<QNameModule, ModuleRuntimeType> modules, final Map<JavaTypeName, RuntimeType> types,
49 final Map<QName, IdentityRuntimeType> identities,
50 final SetMultimap<JavaTypeName, CaseRuntimeType> choiceToCases) {
51 this.context = requireNonNull(context);
52 this.identities = ImmutableMap.copyOf(identities);
53 this.types = ImmutableMap.copyOf(types);
54 this.choiceToCases = ImmutableSetMultimap.copyOf(choiceToCases);
56 modulesByNamespace = ImmutableMap.copyOf(modules);
57 modulesByPackage = ImmutableSortedMap.copyOf(Maps.uniqueIndex(modules.values(),
58 module -> module.getIdentifier().packageName()));
62 public EffectiveModelContext getEffectiveModelContext() {
67 public IdentityRuntimeType identityChild(final QName qname) {
68 return identities.get(requireNonNull(qname));
72 public Optional<RuntimeType> findSchema(final JavaTypeName typeName) {
73 return Optional.ofNullable(types.get(requireNonNull(typeName)));
77 public GeneratedRuntimeType bindingChild(final JavaTypeName typeName) {
78 // The type can actually specify a sub-package, hence we to perform an inexact lookup
79 final var entry = modulesByPackage.floorEntry(typeName.packageName());
80 return entry == null ? null : entry.getValue().bindingChild(typeName);
84 public RuntimeType schemaTreeChild(final QName qname) {
85 final var module = modulesByNamespace.get(qname.getModule());
86 return module == null ? null : module.schemaTreeChild(qname);
90 public Optional<YangDataRuntimeType> findYangData(final YangDataName templateName) {
91 final var module = modulesByNamespace.get(templateName.module());
92 return module == null ? Optional.empty() : Optional.ofNullable(module.yangDataChild(templateName));
96 public Set<CaseRuntimeType> allCaseChildren(final ChoiceRuntimeType choiceType) {
97 return choiceToCases.get(choiceType.getIdentifier());
101 public String toString() {
102 return MoreObjects.toStringHelper(this)
103 .add("modules", modulesByNamespace.keySet())
104 .add("identities", identities.size())
105 .add("types", types.size())