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.ImmutableSortedMap;
15 import com.google.common.collect.Maps;
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;
32 * The result of BindingGenerator run. Contains mapping between Types and SchemaNodes.
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;
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);
53 modulesByNamespace = ImmutableMap.copyOf(modules);
54 modulesByPackage = ImmutableSortedMap.copyOf(Maps.uniqueIndex(modules.values(),
55 module -> module.getIdentifier().packageName()));
59 public EffectiveModelContext getEffectiveModelContext() {
64 public Optional<IdentityRuntimeType> findIdentity(final QName qname) {
65 return Optional.ofNullable(identities.get(requireNonNull(qname)));
69 public Optional<RuntimeType> findSchema(final JavaTypeName typeName) {
70 return Optional.ofNullable(types.get(requireNonNull(typeName)));
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);
81 public RuntimeType schemaTreeChild(final QName qname) {
82 final var module = modulesByNamespace.get(qname.getModule());
83 return module == null ? null : module.schemaTreeChild(qname);
87 public Optional<InputRuntimeType> findRpcInput(final QName rpcName) {
88 return Optional.ofNullable(rpcInputs.get(requireNonNull(rpcName)));
92 public Optional<OutputRuntimeType> findRpcOutput(final QName rpcName) {
93 return Optional.ofNullable(rpcOutputs.get(requireNonNull(rpcName)));
97 public String toString() {
98 return MoreObjects.toStringHelper(this)
99 .add("modules", modulesByNamespace.keySet())
100 .add("identities", identities.size())
101 .add("types", types.size())