Use schemaTreeChild() to lookup input/output
[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.mdsal.binding.runtime.api.YangDataRuntimeType;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.common.YangDataName;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37
38 /**
39  * The result of BindingGenerator run. Contains mapping between Types and SchemaNodes.
40  */
41 public final class DefaultBindingRuntimeTypes implements BindingRuntimeTypes {
42     private final @NonNull EffectiveModelContext context;
43     private final ImmutableSetMultimap<JavaTypeName, CaseRuntimeType> choiceToCases;
44     private final ImmutableMap<QNameModule, ModuleRuntimeType> modulesByNamespace;
45     private final ImmutableSortedMap<String, ModuleRuntimeType> modulesByPackage;
46     private final ImmutableMap<QName, IdentityRuntimeType> identities;
47     private final ImmutableMap<QName, OutputRuntimeType> rpcOutputs;
48     private final ImmutableMap<QName, InputRuntimeType> rpcInputs;
49     private final ImmutableMap<JavaTypeName, RuntimeType> types;
50
51     public DefaultBindingRuntimeTypes(final EffectiveModelContext context,
52             final Map<QNameModule, ModuleRuntimeType> modules, final Map<JavaTypeName, RuntimeType> types,
53             final Map<QName, IdentityRuntimeType> identities, final Map<QName, InputRuntimeType> rpcInputs,
54             final Map<QName, OutputRuntimeType> rpcOutputs,
55             final SetMultimap<JavaTypeName, CaseRuntimeType> choiceToCases) {
56         this.context = requireNonNull(context);
57         this.identities = ImmutableMap.copyOf(identities);
58         this.types = ImmutableMap.copyOf(types);
59         this.rpcInputs = ImmutableMap.copyOf(rpcInputs);
60         this.rpcOutputs = ImmutableMap.copyOf(rpcOutputs);
61         this.choiceToCases = ImmutableSetMultimap.copyOf(choiceToCases);
62
63         modulesByNamespace = ImmutableMap.copyOf(modules);
64         modulesByPackage = ImmutableSortedMap.copyOf(Maps.uniqueIndex(modules.values(),
65             module -> module.getIdentifier().packageName()));
66     }
67
68     @Override
69     public EffectiveModelContext getEffectiveModelContext() {
70         return context;
71     }
72
73     @Override
74     public Optional<IdentityRuntimeType> findIdentity(final QName qname) {
75         return Optional.ofNullable(identities.get(requireNonNull(qname)));
76     }
77
78     @Override
79     public Optional<RuntimeType> findSchema(final JavaTypeName typeName) {
80         return Optional.ofNullable(types.get(requireNonNull(typeName)));
81     }
82
83     @Override
84     public GeneratedRuntimeType bindingChild(final JavaTypeName typeName) {
85         // The type can actually specify a sub-package, hence we to perform an inexact lookup
86         final var entry = modulesByPackage.floorEntry(typeName.packageName());
87         return entry == null ? null : entry.getValue().bindingChild(typeName);
88     }
89
90     @Override
91     public RuntimeType schemaTreeChild(final QName qname) {
92         final var module = modulesByNamespace.get(qname.getModule());
93         return module == null ? null : module.schemaTreeChild(qname);
94     }
95
96     @Override
97     public Optional<InputRuntimeType> findRpcInput(final QName rpcName) {
98         return Optional.ofNullable(rpcInputs.get(requireNonNull(rpcName)));
99     }
100
101     @Override
102     public Optional<OutputRuntimeType> findRpcOutput(final QName rpcName) {
103         return Optional.ofNullable(rpcOutputs.get(requireNonNull(rpcName)));
104     }
105
106     @Override
107     public Optional<YangDataRuntimeType> findYangData(final YangDataName templateName) {
108         final var module = modulesByNamespace.get(templateName.module());
109         return module == null ? Optional.empty() : Optional.ofNullable(module.yangDataChild(templateName));
110     }
111
112     @Override
113     public Set<CaseRuntimeType> allCaseChildren(final ChoiceRuntimeType choiceType) {
114         return choiceToCases.get(choiceType.getIdentifier());
115     }
116
117     @Override
118     public String toString() {
119         return MoreObjects.toStringHelper(this)
120             .add("modules", modulesByNamespace.keySet())
121             .add("identities", identities.size())
122             .add("types", types.size())
123             .toString();
124     }
125 }