9bd748f7ddb1933c88cc94e9e75e51a2cef149e4
[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.ImmutableSortedMap;
15 import com.google.common.collect.Maps;
16 import java.util.Map;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
20 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes;
21 import org.opendaylight.mdsal.binding.runtime.api.GeneratedRuntimeType;
22 import org.opendaylight.mdsal.binding.runtime.api.IdentityRuntimeType;
23 import org.opendaylight.mdsal.binding.runtime.api.InputRuntimeType;
24 import org.opendaylight.mdsal.binding.runtime.api.ModuleRuntimeType;
25 import org.opendaylight.mdsal.binding.runtime.api.OutputRuntimeType;
26 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30
31 /**
32  * The result of BindingGenerator run. Contains mapping between Types and SchemaNodes.
33  */
34 public final class DefaultBindingRuntimeTypes implements BindingRuntimeTypes {
35     private final @NonNull EffectiveModelContext context;
36     private final ImmutableMap<QNameModule, ModuleRuntimeType> modulesByNamespace;
37     private final ImmutableSortedMap<String, ModuleRuntimeType> modulesByPackage;
38     private final ImmutableMap<QName, IdentityRuntimeType> identities;
39     private final ImmutableMap<QName, OutputRuntimeType> rpcOutputs;
40     private final ImmutableMap<QName, InputRuntimeType> rpcInputs;
41     private final ImmutableMap<JavaTypeName, RuntimeType> types;
42
43     public DefaultBindingRuntimeTypes(final EffectiveModelContext context,
44             final Map<QNameModule, ModuleRuntimeType> modules, final Map<JavaTypeName, RuntimeType> types,
45             final Map<QName, IdentityRuntimeType> identities, final Map<QName, InputRuntimeType> rpcInputs,
46             final Map<QName, OutputRuntimeType> rpcOutputs) {
47         this.context = requireNonNull(context);
48         this.identities = ImmutableMap.copyOf(identities);
49         this.types = ImmutableMap.copyOf(types);
50         this.rpcInputs = ImmutableMap.copyOf(rpcInputs);
51         this.rpcOutputs = ImmutableMap.copyOf(rpcOutputs);
52
53         modulesByNamespace = ImmutableMap.copyOf(modules);
54         modulesByPackage = ImmutableSortedMap.copyOf(Maps.uniqueIndex(modules.values(),
55             module -> module.getIdentifier().packageName()));
56     }
57
58     @Override
59     public EffectiveModelContext getEffectiveModelContext() {
60         return context;
61     }
62
63     @Override
64     public Optional<IdentityRuntimeType> findIdentity(final QName qname) {
65         return Optional.ofNullable(identities.get(requireNonNull(qname)));
66     }
67
68     @Override
69     public Optional<RuntimeType> findSchema(final JavaTypeName typeName) {
70         return Optional.ofNullable(types.get(requireNonNull(typeName)));
71     }
72
73     @Override
74     public GeneratedRuntimeType bindingChild(final JavaTypeName typeName) {
75         // The type can actually specify a sub-package, hence we to perform an inexact lookup
76         final var entry = modulesByPackage.floorEntry(typeName.packageName());
77         return entry == null ? null : entry.getValue().bindingChild(typeName);
78     }
79
80     @Override
81     public RuntimeType schemaTreeChild(final QName qname) {
82         final var module = modulesByNamespace.get(qname.getModule());
83         return module == null ? null : module.schemaTreeChild(qname);
84     }
85
86     @Override
87     public Optional<InputRuntimeType> findRpcInput(final QName rpcName) {
88         return Optional.ofNullable(rpcInputs.get(requireNonNull(rpcName)));
89     }
90
91     @Override
92     public Optional<OutputRuntimeType> findRpcOutput(final QName rpcName) {
93         return Optional.ofNullable(rpcOutputs.get(requireNonNull(rpcName)));
94     }
95
96     @Override
97     public String toString() {
98         return MoreObjects.toStringHelper(this)
99             .add("modules", modulesByNamespace.keySet())
100             .add("identities", identities.size())
101             .add("types", types.size())
102             .toString();
103     }
104 }