From 9b232b2e329b5432e3af28789c30b289750f1c52 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Tue, 9 Sep 2014 14:18:16 +0200 Subject: [PATCH] Bug 1698: Updated handling of choice node inside choice node As it turned out, Composite Node to Binding codec had problem with handling situations, where case was augmented to choice and that case contained another choice directly on first level. Updated code to detect this situation and do proper lookup to find choice in case in choice. Change-Id: I2c39542520b842857884468dc0dcd28502ff41b3 Signed-off-by: Tony Tkacik --- .../impl/LazyGeneratedCodecRegistry.java | 25 ++++++++++++++++++- .../restconf/utils/RestconfUtils.java | 4 +-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java index f72856e1c1..65e80d0584 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java @@ -984,8 +984,21 @@ class LazyGeneratedCodecRegistry implements CodecRegistry, SchemaContextListener @Override protected void adaptForPathImpl(final InstanceIdentifier augTarget, final DataNodeContainer ctxNode) { - Optional newChoice = BindingSchemaContextUtils.findInstantiatedChoice(ctxNode, choiceType); tryToLoadImplementations(); + Optional newChoice = BindingSchemaContextUtils.findInstantiatedChoice(ctxNode, choiceType); + if(!newChoice.isPresent()) { + // Choice is nested inside other choice, so we need to look two levels deep. + in_choices: for(DataSchemaNode child : ctxNode.getChildNodes()) { + if(child instanceof ChoiceNode) { + Optional potential = findChoiceInChoiceCases((ChoiceNode) child, choiceType); + if(potential.isPresent()) { + newChoice = potential; + break in_choices; + } + } + } + } + Preconditions.checkState(newChoice.isPresent(), "BUG: Unable to find instantiated choice node in schema."); for (@SuppressWarnings("rawtypes") Entry> codec : getImplementations().entrySet()) { @@ -998,6 +1011,16 @@ class LazyGeneratedCodecRegistry implements CodecRegistry, SchemaContextListener } } + private Optional findChoiceInChoiceCases(ChoiceNode choice, Class choiceType) { + for(ChoiceCaseNode caze : choice.getCases()) { + Optional potential = BindingSchemaContextUtils.findInstantiatedChoice(caze, choiceType); + if(potential.isPresent()) { + return potential; + } + } + return Optional.absent(); + } + @Override public String toString() { return "DispatchChoiceCodecImpl [choiceType=" + choiceType + "]"; diff --git a/restconf/restconf-util/src/main/java/org/opendaylight/yangtools/restconf/utils/RestconfUtils.java b/restconf/restconf-util/src/main/java/org/opendaylight/yangtools/restconf/utils/RestconfUtils.java index 8c3d8e9488..6a772f4ae4 100644 --- a/restconf/restconf-util/src/main/java/org/opendaylight/yangtools/restconf/utils/RestconfUtils.java +++ b/restconf/restconf-util/src/main/java/org/opendaylight/yangtools/restconf/utils/RestconfUtils.java @@ -31,10 +31,10 @@ import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.RpcService; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.CompositeNode; +import org.opendaylight.yangtools.yang.data.api.Node; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; -import org.opendaylight.yangtools.yang.data.api.Node; import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode; import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService; import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException; @@ -293,7 +293,7 @@ public class RestconfUtils { Document doc = builder.parse(inputStream); Element rootElement = doc.getDocumentElement(); Node domNode = XmlDocumentUtils.toDomNode(rootElement, Optional.of(dataSchema), - Optional. absent()); + Optional. absent(), Optional.of(schemaContext)); DataObject dataObject = mappingService.dataObjectFromDataDom(path, (CompositeNode) domNode); // getDataFromResponse return dataObject; } catch (DeserializationException e) { -- 2.36.6