Rework AugmentRuntimeType and Choice/Case linkage
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / rt / DefaultBindingRuntimeTypes.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o.  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.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;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.Set;
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.InputRuntimeType;
29 import org.opendaylight.mdsal.binding.runtime.api.ModuleRuntimeType;
30 import org.opendaylight.mdsal.binding.runtime.api.OutputRuntimeType;
31 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35
36 /**
37  * The result of BindingGenerator run. Contains mapping between Types and SchemaNodes.
38  */
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<QName, OutputRuntimeType> rpcOutputs;
46     private final ImmutableMap<QName, InputRuntimeType> rpcInputs;
47     private final ImmutableMap<JavaTypeName, RuntimeType> types;
48
49     public DefaultBindingRuntimeTypes(final EffectiveModelContext context,
50             final Map<QNameModule, ModuleRuntimeType> modules, final Map<JavaTypeName, RuntimeType> types,
51             final Map<QName, IdentityRuntimeType> identities, final Map<QName, InputRuntimeType> rpcInputs,
52             final Map<QName, OutputRuntimeType> rpcOutputs,
53             final SetMultimap<JavaTypeName, CaseRuntimeType> choiceToCases) {
54         this.context = requireNonNull(context);
55         this.identities = ImmutableMap.copyOf(identities);
56         this.types = ImmutableMap.copyOf(types);
57         this.rpcInputs = ImmutableMap.copyOf(rpcInputs);
58         this.rpcOutputs = ImmutableMap.copyOf(rpcOutputs);
59         this.choiceToCases = ImmutableSetMultimap.copyOf(choiceToCases);
60
61         modulesByNamespace = ImmutableMap.copyOf(modules);
62         modulesByPackage = ImmutableSortedMap.copyOf(Maps.uniqueIndex(modules.values(),
63             module -> module.getIdentifier().packageName()));
64     }
65
66     @Override
67     public EffectiveModelContext getEffectiveModelContext() {
68         return context;
69     }
70
71     @Override
72     public Optional<IdentityRuntimeType> findIdentity(final QName qname) {
73         return Optional.ofNullable(identities.get(requireNonNull(qname)));
74     }
75
76     @Override
77     public Optional<RuntimeType> findSchema(final JavaTypeName typeName) {
78         return Optional.ofNullable(types.get(requireNonNull(typeName)));
79     }
80
81     @Override
82     public GeneratedRuntimeType bindingChild(final JavaTypeName typeName) {
83         // The type can actually specify a sub-package, hence we to perform an inexact lookup
84         final var entry = modulesByPackage.floorEntry(typeName.packageName());
85         return entry == null ? null : entry.getValue().bindingChild(typeName);
86     }
87
88     @Override
89     public RuntimeType schemaTreeChild(final QName qname) {
90         final var module = modulesByNamespace.get(qname.getModule());
91         return module == null ? null : module.schemaTreeChild(qname);
92     }
93
94     @Override
95     public Optional<InputRuntimeType> findRpcInput(final QName rpcName) {
96         return Optional.ofNullable(rpcInputs.get(requireNonNull(rpcName)));
97     }
98
99     @Override
100     public Optional<OutputRuntimeType> findRpcOutput(final QName rpcName) {
101         return Optional.ofNullable(rpcOutputs.get(requireNonNull(rpcName)));
102     }
103
104     @Override
105     public Set<CaseRuntimeType> allCaseChildren(final ChoiceRuntimeType choiceType) {
106         return choiceToCases.get(choiceType.getIdentifier());
107     }
108
109     @Override
110     public String toString() {
111         return MoreObjects.toStringHelper(this)
112             .add("modules", modulesByNamespace.keySet())
113             .add("identities", identities.size())
114             .add("types", types.size())
115             .toString();
116     }
117 }