Update BindingRuntime{Context,Generator,Types}
[mdsal.git] / binding / mdsal-binding-runtime-api / src / main / java / org / opendaylight / binding / runtime / api / BindingRuntimeTypes.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.binding.runtime.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.collect.BiMap;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableMultimap;
18 import com.google.common.collect.Multimap;
19 import java.util.Collection;
20 import java.util.Map;
21 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.concepts.Immutable;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
30
31 /**
32  * The result of BindingGenerator run. Contains mapping between Types and SchemaNodes.
33  */
34 @Beta
35 public final class BindingRuntimeTypes implements EffectiveModelContextProvider, Immutable {
36     private final @NonNull EffectiveModelContext schemaContext;
37     private final ImmutableMap<Type, AugmentationSchemaNode> typeToAugmentation;
38     private final ImmutableBiMap<Type, WithStatus> typeToSchema;
39     private final ImmutableMultimap<Type, Type> choiceToCases;
40     private final ImmutableMap<QName, Type> identities;
41
42     public BindingRuntimeTypes(final EffectiveModelContext schemaContext,
43             final Map<Type, AugmentationSchemaNode> typeToAugmentation,
44             final BiMap<Type, WithStatus> typeToDefiningSchema, final Multimap<Type, Type> choiceToCases,
45             final Map<QName, Type> identities) {
46         this.schemaContext = requireNonNull(schemaContext);
47         this.typeToAugmentation = ImmutableMap.copyOf(typeToAugmentation);
48         this.typeToSchema = ImmutableBiMap.copyOf(typeToDefiningSchema);
49         this.choiceToCases = ImmutableMultimap.copyOf(choiceToCases);
50         this.identities = ImmutableMap.copyOf(identities);
51     }
52
53     @Override
54     public EffectiveModelContext getEffectiveModelContext() {
55         return schemaContext;
56     }
57
58     public Optional<AugmentationSchemaNode> findAugmentation(final Type type) {
59         return Optional.ofNullable(typeToAugmentation.get(type));
60     }
61
62     public Optional<Type> findIdentity(final QName qname) {
63         return Optional.ofNullable(identities.get(qname));
64     }
65
66     public Optional<WithStatus> findSchema(final Type type) {
67         return Optional.ofNullable(typeToSchema.get(type));
68     }
69
70     public Optional<Type> findType(final WithStatus schema) {
71         return Optional.ofNullable(typeToSchema.inverse().get(schema));
72     }
73
74     public Multimap<Type, Type> getChoiceToCases() {
75         return choiceToCases;
76     }
77
78     public Collection<Type> findCases(final Type choiceType) {
79         return choiceToCases.get(choiceType);
80     }
81
82     @Override
83     public String toString() {
84         return MoreObjects.toStringHelper(this)
85                 .add("typeToAugmentation", typeToAugmentation)
86                 .add("typeToSchema", typeToSchema)
87                 .add("choiceToCases", choiceToCases)
88                 .add("identities", identities)
89                 .toString();
90     }
91 }