*/
package org.opendaylight.yangtools.binding.data.codec.impl;
+import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
final class ChoiceNodeCodecContext extends DataContainerCodecContext<ChoiceNode> {
-
+ private static final Logger LOG = LoggerFactory.getLogger(ChoiceNodeCodecContext.class);
private final ImmutableMap<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChild;
private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byClass;
private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byCaseChildClass;
for (Class<?> caze : factory().getRuntimeContext().getCases(bindingClass())) {
DataContainerCodecPrototype<ChoiceCaseNode> cazeDef = loadCase(caze);
- byClassBuilder.put(cazeDef.getBindingClass(), cazeDef);
- for (Class<? extends DataObject> cazeChild : BindingReflections.getChildrenClasses((Class) caze)) {
- byCaseChildClassBuilder.put(cazeChild, cazeDef);
- }
- for (DataSchemaNode cazeChild : cazeDef.getSchema().getChildNodes()) {
- byYangCaseChildBuilder.put(new NodeIdentifier(cazeChild.getQName()), cazeDef);
+ if (cazeDef != null) {
+ byClassBuilder.put(cazeDef.getBindingClass(), cazeDef);
+ for (Class<? extends DataObject> cazeChild : BindingReflections.getChildrenClasses((Class) caze)) {
+ byCaseChildClassBuilder.put(cazeChild, cazeDef);
+ }
+ for (DataSchemaNode cazeChild : cazeDef.getSchema().getChildNodes()) {
+ byYangCaseChildBuilder.put(new NodeIdentifier(cazeChild.getQName()), cazeDef);
+ }
}
}
}
protected DataContainerCodecPrototype<ChoiceCaseNode> loadCase(final Class<?> childClass) {
- ChoiceCaseNode childSchema = factory().getRuntimeContext().getCaseSchemaDefinition(schema(), childClass);
- return DataContainerCodecPrototype.from(childClass, childSchema, factory());
+ Optional<ChoiceCaseNode> childSchema = factory().getRuntimeContext().getCaseSchemaDefinition(schema(), childClass);
+ if (childSchema.isPresent()) {
+ return DataContainerCodecPrototype.from(childClass, childSchema.get(), factory());
+ }
+
+ LOG.debug("Supplied class %s is not valid case in schema %s", childClass, schema());
+ return null;
}
@Override
*
* @param schema Resolved parent choice schema
* @param childClass Class representing case.
- * @return Resolved case schema.
- * @throws IllegalArgumentException If supplied class does not represent case or supplied case class is not
- * valid in the context of parent choice schema.
+ * @return Optionally a resolved case schema, absent if the choice is not legal in
+ * the given context.
+ * @throws IllegalArgumentException If supplied class does not represent case.
*/
- public ChoiceCaseNode getCaseSchemaDefinition(final ChoiceNode schema, final Class<?> childClass) throws IllegalArgumentException {
+ public Optional<ChoiceCaseNode> getCaseSchemaDefinition(final ChoiceNode schema, final Class<?> childClass) throws IllegalArgumentException {
DataSchemaNode origSchema = getSchemaDefinition(childClass);
Preconditions.checkArgument(origSchema instanceof ChoiceCaseNode, "Supplied schema %s is not case.", origSchema);
+
/* FIXME: Make sure that if there are multiple augmentations of same
* named case, with same structure we treat it as equals
* this is due property of Binding specification and copy builders
* that user may be unaware that he is using incorrect case
* which was generated for choice inside grouping.
*/
- Optional<ChoiceCaseNode> found = BindingSchemaContextUtils.findInstantiatedCase(schema,
+ final Optional<ChoiceCaseNode> found = BindingSchemaContextUtils.findInstantiatedCase(schema,
(ChoiceCaseNode) origSchema);
- Preconditions.checkArgument(found.isPresent(), "Supplied class %s (schema %s) is not valid case in schema %s", childClass, origSchema, schema);
- return found.get();
+ return found;
}
private static Type referencedType(final Class<?> type) {