Binding2 runtime - Codecs impl - context - part2
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / ChoiceNodeCodecContext.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  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.javav2.dom.codec.impl.context;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.Iterables;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Set;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.DataContainerCodecContext;
22 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.DataContainerCodecPrototype;
23 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.NodeCodecContext;
24 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
25 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
26 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
27 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
33 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
34 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Context for prototype of choice node codec.
43  *
44  * @param <D>
45  *            - type of tree node
46  */
47 @Beta
48 public class ChoiceNodeCodecContext<D extends TreeNode> extends DataContainerCodecContext<D, ChoiceSchemaNode> {
49
50     private static final Logger LOG = LoggerFactory.getLogger(ChoiceNodeCodecContext.class);
51
52     private final ImmutableMap<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChild;
53     private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byClass;
54     private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byCaseChildClass;
55
56     /**
57      * Prepare context for choice node from prototype and all case children of choice class.
58      *
59      * @param prototype
60      *            - codec prototype of choice node
61      */
62     public ChoiceNodeCodecContext(final DataContainerCodecPrototype<ChoiceSchemaNode> prototype) {
63         super(prototype);
64         final Map<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChildBuilder =
65                 new HashMap<>();
66         final Map<Class<?>, DataContainerCodecPrototype<?>> byClassBuilder = new HashMap<>();
67         final Map<Class<?>, DataContainerCodecPrototype<?>> byCaseChildClassBuilder = new HashMap<>();
68         final Set<Class<?>> potentialSubstitutions = new HashSet<>();
69         // Walks all cases for supplied choice in current runtime context
70         for (final Class<?> caze : factory().getRuntimeContext().getCases(getBindingClass())) {
71             // We try to load case using exact match thus name
72             // and original schema must equals
73             final DataContainerCodecPrototype<ChoiceCaseNode> cazeDef = loadCase(caze);
74             // If we have case definition, this case is instantiated
75             // at current location and thus is valid in context of parent choice
76             if (cazeDef != null) {
77                 byClassBuilder.put(cazeDef.getBindingClass(), cazeDef);
78                 // Updates collection of case children
79                 @SuppressWarnings("unchecked")
80                 final Class<? extends Instantiable<?>> cazeCls = (Class<? extends Instantiable<?>>) caze;
81                 for (final Class<? extends TreeNode> cazeChild : BindingReflections.getChildrenClasses(cazeCls)) {
82                     byCaseChildClassBuilder.put(cazeChild, cazeDef);
83                 }
84                 // Updates collection of YANG instance identifier to case
85                 for (final DataSchemaNode cazeChild : cazeDef.getSchema().getChildNodes()) {
86                     if (cazeChild.isAugmenting()) {
87                         final AugmentationSchema augment =
88                                 SchemaUtils.findCorrespondingAugment(cazeDef.getSchema(), cazeChild);
89                         if (augment != null) {
90                             byYangCaseChildBuilder.put(SchemaUtils.getNodeIdentifierForAugmentation(augment), cazeDef);
91                             continue;
92                         }
93                     }
94                     byYangCaseChildBuilder.put(NodeIdentifier.create(cazeChild.getQName()), cazeDef);
95                 }
96             } else {
97                 /*
98                  * If case definition is not available, we store it for later check if it could be used as
99                  * substitution of existing one.
100                  */
101                 potentialSubstitutions.add(caze);
102             }
103         }
104
105         final Map<Class<?>, DataContainerCodecPrototype<?>> bySubstitutionBuilder = new HashMap<>();
106         /*
107          * Walks all cases which are not directly instantiated and tries to match them to instantiated cases -
108          * represent same data as instantiated case, only case name or schema path is different. This is
109          * required due property of binding specification, that if choice is in grouping schema path location
110          * is lost, and users may use incorrect case class using copy builders.
111          */
112         for (final Class<?> substitution : potentialSubstitutions) {
113             for (final Entry<Class<?>, DataContainerCodecPrototype<?>> real : byClassBuilder.entrySet()) {
114                 if (BindingReflections.isSubstitutionFor(substitution, real.getKey())) {
115                     bySubstitutionBuilder.put(substitution, real.getValue());
116                     break;
117                 }
118             }
119         }
120         byClassBuilder.putAll(bySubstitutionBuilder);
121         byYangCaseChild = ImmutableMap.copyOf(byYangCaseChildBuilder);
122         byClass = ImmutableMap.copyOf(byClassBuilder);
123         byCaseChildClass = ImmutableMap.copyOf(byCaseChildClassBuilder);
124     }
125
126     @SuppressWarnings("unchecked")
127     @Nonnull
128     @Override
129     public <DV extends TreeNode> DataContainerCodecContext<DV, ?> streamChild(@Nonnull final Class<DV> childClass) {
130         final DataContainerCodecPrototype<?> child = byClass.get(childClass);
131         return (DataContainerCodecContext<DV,
132                 ?>) childNonNull(child, childClass, "Supplied class %s is not valid case", childClass).get();
133     }
134
135     @SuppressWarnings("unchecked")
136     @Override
137     public <DV extends TreeNode> Optional<DataContainerCodecContext<DV, ?>>
138             possibleStreamChild(@Nonnull final Class<DV> childClass) {
139         final DataContainerCodecPrototype<?> child = byClass.get(childClass);
140         if (child != null) {
141             return Optional.of((DataContainerCodecContext<DV, ?>) child.get());
142         }
143         return Optional.absent();
144     }
145
146     Iterable<Class<?>> getCaseChildrenClasses() {
147         return byCaseChildClass.keySet();
148     }
149
150     private DataContainerCodecPrototype<ChoiceCaseNode> loadCase(final Class<?> childClass) {
151         final Optional<ChoiceCaseNode> childSchema =
152                 factory().getRuntimeContext().getCaseSchemaDefinition(getSchema(), childClass);
153         if (childSchema.isPresent()) {
154             return DataContainerCodecPrototype.from(childClass, childSchema.get(), factory());
155         }
156
157         LOG.debug("Supplied class %s is not valid case in schema %s", childClass, getSchema());
158         return null;
159     }
160
161     @Nonnull
162     @Override
163     public NodeCodecContext<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg) {
164         final DataContainerCodecPrototype<?> cazeProto;
165         if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
166             cazeProto = byYangCaseChild.get(new NodeIdentifier(arg.getNodeType()));
167         } else {
168             cazeProto = byYangCaseChild.get(arg);
169         }
170
171         return childNonNull(cazeProto, arg, "Argument %s is not valid child of %s", arg, getSchema()).get()
172                 .yangPathArgumentChild(arg);
173     }
174
175     @SuppressWarnings("unchecked")
176     @Nonnull
177     @Override
178     public D deserialize(@Nonnull final NormalizedNode<?, ?> data) {
179         Preconditions.checkArgument(data instanceof ChoiceNode);
180         final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> casted =
181                 (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) data;
182         final NormalizedNode<?, ?> first = Iterables.getFirst(casted.getValue(), null);
183
184         if (first == null) {
185             return null;
186         }
187         final DataContainerCodecPrototype<?> caze = byYangCaseChild.get(first.getIdentifier());
188         return (D) caze.get().deserialize(data);
189     }
190
191     @Override
192     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
193         return deserialize(normalizedNode);
194     }
195
196     @Nonnull
197     @Override
198     public TreeArgument<?> deserializePathArgument(@Nonnull final YangInstanceIdentifier.PathArgument arg) {
199         Preconditions.checkArgument(getDomPathArgument().equals(arg));
200         return null;
201     }
202
203     @Nonnull
204     @Override
205     public YangInstanceIdentifier.PathArgument serializePathArgument(@Nonnull final TreeArgument<?> arg) {
206         return getDomPathArgument();
207     }
208 }