import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
+import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
import org.opendaylight.yangtools.sal.binding.model.api.Type;
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
-import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
import org.opendaylight.yangtools.yang.binding.Augmentable;
import org.opendaylight.yangtools.yang.binding.Augmentation;
import org.opendaylight.yangtools.yang.binding.BaseIdentity;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
GeneratorListener {
private static final Logger LOG = LoggerFactory.getLogger(LazyGeneratedCodecRegistry.class);
- private static final LateMixinCodec NOT_READY_CODEC = new LateMixinCodec();
// Concrete class to codecs
private static final Map<Class<?>, DataContainerCodec<?>> containerCodecs = Collections
@SuppressWarnings("rawtypes")
private static final Map<Type, WeakReference<Class>> typeToClass = new ConcurrentHashMap<>();
- @SuppressWarnings("rawtypes")
- private static final ConcurrentMap<Type, ChoiceCaseCodecImpl> typeToCaseCodecs = new ConcurrentHashMap<>();
+ private static final ConcurrentMap<Type, ChoiceCaseNode> caseTypeToCaseSchema = new ConcurrentHashMap<>();
private static final Map<SchemaPath, Type> pathToType = new ConcurrentHashMap<>();
private static final Map<List<QName>, Type> pathToInstantiatedType = new ConcurrentHashMap<>();
.<Type, Type> create());
private final InstanceIdentifierCodec instanceIdentifierCodec = new InstanceIdentifierCodecImpl(this);
- private final CaseClassMapFacade classToCaseRawCodec = new CaseClassMapFacade();
private final IdentityCompositeCodec identityRefCodec = new IdentityCompositeCodec();
private final ClassLoadingStrategy classLoadingStrategy;
private final AbstractTransformerGenerator generator;
return potential;
}
ConcreteType typeref = Types.typeForClass(caseClass);
- ChoiceCaseCodecImpl caseCodec = typeToCaseCodecs.get(typeref);
+ ChoiceCaseNode caseSchema = caseTypeToCaseSchema.get(typeref);
- Preconditions.checkState(caseCodec != null, "Case Codec was not created proactivelly for %s",
- caseClass.getName());
- Preconditions.checkState(caseCodec.getSchema() != null, "Case schema is not available for %s",
- caseClass.getName());
- Class<? extends BindingCodec> newCodec = generator.caseCodecFor(caseClass, caseCodec.getSchema());
+ Preconditions.checkState(caseSchema != null, "Case schema is not available for %s", caseClass.getName());
+ Class<? extends BindingCodec> newCodec = generator.caseCodecFor(caseClass, caseSchema);
BindingCodec newInstance = newInstanceOf(newCodec);
- caseCodec.setDelegate(newInstance);
+ ChoiceCaseCodecImpl caseCodec = new ChoiceCaseCodecImpl(caseClass, caseSchema, newInstance);
caseCodecs.put(caseClass, caseCodec);
for (Entry<Class<?>, PublicChoiceCodecImpl<?>> choice : choiceCodecs.entrySet()) {
synchronized (choiceToCases) {
choiceToCases.putAll(context.getChoiceToCases());
}
- captureCases(context.getCases(), schemaContext);
- }
-
- private void captureCases(final Map<SchemaPath, GeneratedTypeBuilder> cases, final SchemaContext module) {
- for (Entry<SchemaPath, GeneratedTypeBuilder> caseNode : cases.entrySet()) {
- ReferencedTypeImpl typeref = new ReferencedTypeImpl(caseNode.getValue().getPackageName(), caseNode
- .getValue().getName());
-
- pathToType.put(caseNode.getKey(), caseNode.getValue());
-
- ChoiceCaseNode node = (ChoiceCaseNode) SchemaContextUtil.findDataSchemaNode(module, caseNode.getKey());
-
- if (node == null) {
- LOG.warn("Failed to find YANG SchemaNode for {}, with path {} was not found in context.",
- typeref.getFullyQualifiedName(), caseNode.getKey());
- @SuppressWarnings("rawtypes")
- ChoiceCaseCodecImpl value = new ChoiceCaseCodecImpl();
- typeToCaseCodecs.putIfAbsent(typeref, value);
- continue;
- }
- @SuppressWarnings("rawtypes")
- ChoiceCaseCodecImpl value = new ChoiceCaseCodecImpl(node);
- typeToCaseCodecs.putIfAbsent(typeref, value);
+ synchronized (caseTypeToCaseSchema) {
+ caseTypeToCaseSchema.putAll(context.getCaseTypeToSchemas());
}
}
Preconditions.checkState(oldCodec == null);
BindingCodec<Map<QName, Object>, Object> delegate = newInstanceOf(choiceCodec);
PublicChoiceCodecImpl<?> newCodec = new PublicChoiceCodecImpl(delegate);
+ DispatchChoiceCodecImpl dispatchCodec = new DispatchChoiceCodecImpl(choiceClass);
choiceCodecs.put(choiceClass, newCodec);
- CodecMapping.setClassToCaseMap(choiceCodec, classToCaseRawCodec);
- CodecMapping.setCompositeNodeToCaseMap(choiceCodec, newCodec.getCompositeToCase());
-
- tryToCreateCasesCodecs(schema);
-
- }
-
- @Deprecated
- private void tryToCreateCasesCodecs(final ChoiceNode schema) {
- for (ChoiceCaseNode choiceCase : schema.getCases()) {
- ChoiceCaseNode caseNode = choiceCase;
- if (caseNode.isAddedByUses()) {
- DataSchemaNode origCaseNode = SchemaContextUtil.findOriginal(caseNode, currentSchema);
- if (origCaseNode instanceof ChoiceCaseNode) {
- caseNode = (ChoiceCaseNode) origCaseNode;
- }
- }
- SchemaPath path = caseNode.getPath();
-
- Type type;
- if (path != null && (type = pathToType.get(path)) != null) {
- ReferencedTypeImpl typeref = new ReferencedTypeImpl(type.getPackageName(), type.getName());
- @SuppressWarnings("rawtypes")
- ChoiceCaseCodecImpl partialCodec = typeToCaseCodecs.get(typeref);
- if (partialCodec.getSchema() == null) {
- partialCodec.setSchema(caseNode);
- }
- try {
- Class<?> caseClass = classLoadingStrategy.loadClass(type.getFullyQualifiedName());
- getCaseCodecFor(caseClass);
- } catch (ClassNotFoundException e) {
- LOG.trace("Could not proactivelly create case codec for {}", type, e);
- }
- }
- }
-
+ CodecMapping.setDispatchCodec(choiceCodec, dispatchCodec);
}
@Override
@SuppressWarnings("rawtypes")
private static class ChoiceCaseCodecImpl<T extends DataContainer> implements ChoiceCaseCodec<T>, //
Delegator<BindingCodec>, LocationAwareBindingCodec<Node<?>, ValueWithQName<T>> {
- private boolean augmenting;
- private boolean uses;
- private BindingCodec delegate;
-
- private Set<String> validNames;
- private Set<QName> validQNames;
- private ChoiceCaseNode schema;
- private Set<InstanceIdentifier<?>> applicableLocations;
+ private final BindingCodec delegate;
+ private final ChoiceCaseNode schema;
+ private final Map<InstanceIdentifier<?>, ChoiceCaseNode> instantiatedLocations;
+ private final Class<?> dataType;
@Override
public boolean isApplicable(final InstanceIdentifier location) {
- return applicableLocations.contains(location);
+ return instantiatedLocations.containsKey(location);
}
- public void setSchema(final ChoiceCaseNode caseNode) {
+ public ChoiceCaseCodecImpl(final Class<?> caseClass, final ChoiceCaseNode caseNode, final BindingCodec newInstance) {
+ this.delegate = newInstance;
+ this.dataType = caseClass;
this.schema = caseNode;
- validNames = new HashSet<>();
- validQNames = new HashSet<>();
- for (DataSchemaNode node : caseNode.getChildNodes()) {
- QName qname = node.getQName();
- validQNames.add(qname);
- validNames.add(qname.getLocalName());
- }
- augmenting = caseNode.isAugmenting();
- uses = caseNode.isAddedByUses();
- applicableLocations = new HashSet<>();
- }
-
- public ChoiceCaseCodecImpl() {
- this.delegate = NOT_READY_CODEC;
- }
-
- public ChoiceCaseCodecImpl(final ChoiceCaseNode caseNode) {
- this.delegate = NOT_READY_CODEC;
- setSchema(caseNode);
+ instantiatedLocations = new HashMap<>();
}
@Override
@Override
public ValueWithQName<T> deserialize(final Node<?> input, final InstanceIdentifier<?> bindingIdentifier) {
- throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
+ if (input == null) {
+ return null;
+ }
+ QName qname = input.getNodeType();
+ synchronized (instantiatedLocations) {
+ ChoiceCaseNode instantiation = instantiatedLocations.get(bindingIdentifier);
+ if (instantiation != null) {
+ qname = instantiatedLocations.get(bindingIdentifier).getQName();
+ }
+ }
+ @SuppressWarnings("unchecked")
+ T value = (T) getDelegate().deserialize(new SimpleEntry(qname, input), bindingIdentifier);
+ return new ValueWithQName<T>(qname, value);
}
@Override
return delegate;
}
- public void setDelegate(final BindingCodec delegate) {
- this.delegate = delegate;
- }
-
public ChoiceCaseNode getSchema() {
return schema;
}
@Override
+ @Deprecated
public boolean isAcceptable(final Node<?> input) {
- if (input instanceof CompositeNode) {
- if (augmenting && !uses) {
- return checkAugmenting((CompositeNode) input);
- } else {
- return checkLocal((CompositeNode) input);
- }
- }
- return false;
+ return checkAgainstSchema(schema, input);
}
- @SuppressWarnings("deprecation")
- private boolean checkLocal(final CompositeNode input) {
- QName parent = input.getNodeType();
- for (Node<?> childNode : input.getChildren()) {
- QName child = childNode.getNodeType();
- if (!Objects.equals(parent.getNamespace(), child.getNamespace())
- || !Objects.equals(parent.getRevision(), child.getRevision())) {
- continue;
- }
- if (validNames.contains(child.getLocalName())) {
- return true;
+ private static boolean checkAgainstSchema(final ChoiceCaseNode schema, final Node<?> node) {
+ if (node instanceof CompositeNode) {
+ CompositeNode input = (CompositeNode) node;
+ for (Node<?> childNode : input.getValue()) {
+ QName child = childNode.getNodeType();
+ if (schema.getDataChildByName(child) != null) {
+ return true;
+ }
}
}
return false;
}
- @SuppressWarnings("deprecation")
- private boolean checkAugmenting(final CompositeNode input) {
- for (Node<?> child : input.getChildren()) {
- if (validQNames.contains(child.getNodeType())) {
- return true;
- }
+ @Override
+ public Class<?> getDataType() {
+ return dataType;
+ }
+
+ public void adaptForPath(final InstanceIdentifier<?> augTarget, final ChoiceCaseNode choiceCaseNode) {
+ synchronized (instantiatedLocations) {
+ instantiatedLocations.put(augTarget, choiceCaseNode);
}
- return false;
}
- @Override
- public Class<?> getDataType() {
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException("Not implemented Yet.");
+ public boolean isAcceptable(final InstanceIdentifier path, final CompositeNode input) {
+ ChoiceCaseNode instantiatedSchema = null;
+ synchronized (instantiatedLocations) {
+ instantiatedSchema = instantiatedLocations.get(path);
+ }
+ if (instantiatedSchema == null) {
+ return false;
+ }
+ return checkAgainstSchema(instantiatedSchema, input);
}
}
private final Map<Class, ChoiceCaseCodecImpl<?>> cases = Collections
.synchronizedMap(new WeakHashMap<Class, ChoiceCaseCodecImpl<?>>());
- private final CaseCompositeNodeMapFacade CompositeToCase;
-
public PublicChoiceCodecImpl(final BindingCodec<Map<QName, Object>, Object> delegate) {
this.delegate = delegate;
- this.CompositeToCase = new CaseCompositeNodeMapFacade(cases);
}
@Override
throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
}
- public CaseCompositeNodeMapFacade getCompositeToCase() {
- return CompositeToCase;
- }
-
@Override
public BindingCodec<Map<QName, Object>, Object> getDelegate() {
return delegate;
}
- @SuppressWarnings("unused")
- private class DispatchChoiceCodecImpl extends LocationAwareDispatchCodec<ChoiceCaseCodecImpl<?>> {
+ class DispatchChoiceCodecImpl extends LocationAwareDispatchCodec<ChoiceCaseCodecImpl<?>> {
+ private final Class<?> choiceType;
+ private final QName choiceName;
- @Override
- public Object deserialize(final Object input,
- @SuppressWarnings("rawtypes") final InstanceIdentifier bindingIdentifier) {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public Object serialize(final Object input) {
- // TODO Auto-generated method stub
- return null;
+ private DispatchChoiceCodecImpl(final Class<?> type) {
+ choiceType = type;
+ choiceName = BindingReflections.findQName(type);
}
@Override
- protected ChoiceCaseCodecImpl<?> tryToLoadImplementation(final Class<? extends DataContainer> inputType) {
- return getCaseCodecFor(inputType);
- }
-
- @Override
- protected void tryToLoadImplementations() {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- protected void adaptForPathImpl(final InstanceIdentifier<?> path, final DataNodeContainer ctx) {
- // TODO Auto-generated method stub
-
- }
- }
-
- @SuppressWarnings("rawtypes")
- private class CaseClassMapFacade extends MapFacadeBase {
-
- @Override
- public Set<Entry<Class, BindingCodec<Object, Object>>> entrySet() {
- return Collections.emptySet();
- }
-
- @Override
- public BindingCodec get(final Object key) {
- if (key instanceof Class) {
- Class cls = (Class) key;
- // bindingClassEncountered(cls);
- ChoiceCaseCodecImpl caseCodec = getCaseCodecFor(cls);
- return caseCodec.getDelegate();
- }
- return null;
- }
- }
-
- @SuppressWarnings("rawtypes")
- private static class CaseCompositeNodeMapFacade extends MapFacadeBase<CompositeNode> {
-
- final Map<Class, ChoiceCaseCodecImpl<?>> choiceCases;
-
- public CaseCompositeNodeMapFacade(final Map<Class, ChoiceCaseCodecImpl<?>> choiceCases) {
- this.choiceCases = choiceCases;
- }
+ public Object deserialize(final Object input, @SuppressWarnings("rawtypes") final InstanceIdentifier path) {
+ adaptForPath(path);
- @Override
- public BindingCodec get(final Object key) {
- if (!(key instanceof CompositeNode)) {
- return null;
- }
- for (Entry<Class, ChoiceCaseCodecImpl<?>> entry : choiceCases.entrySet()) {
- ChoiceCaseCodecImpl<?> codec = entry.getValue();
- if (codec.isAcceptable((CompositeNode) key)) {
- return codec.getDelegate();
+ if (input instanceof CompositeNode) {
+ List<Entry<Class, ChoiceCaseCodecImpl<?>>> codecs = new ArrayList<>(getImplementations().entrySet());
+ for (Entry<Class, ChoiceCaseCodecImpl<?>> codec : codecs) {
+ ChoiceCaseCodecImpl<?> caseCodec = codec.getValue();
+ if (caseCodec.isAcceptable(path, (CompositeNode) input)) {
+ ValueWithQName<?> value = caseCodec.deserialize((CompositeNode) input, path);
+ if (value != null) {
+ return value.getValue();
+ }
+ return null;
+ }
}
}
return null;
}
- }
-
- /**
- * This map is used as only facade for
- * {@link org.opendaylight.yangtools.yang.binding.BindingCodec} in different
- * classloaders to retrieve codec dynamicly based on provided key.
- *
- * @param <T>
- * Key type
- */
- @SuppressWarnings("rawtypes")
- private abstract static class MapFacadeBase<T> implements Map<T, BindingCodec<?, ?>> {
-
- @Override
- public boolean containsKey(final Object key) {
- return get(key) != null;
- }
-
- @Override
- public void clear() {
- throw notModifiable();
- }
-
- @Override
- public boolean equals(final Object obj) {
- return super.equals(obj);
- }
-
- @Override
- public BindingCodec remove(final Object key) {
- return null;
- }
-
+ @SuppressWarnings("unchecked")
@Override
- public int size() {
- return 0;
+ public Object serialize(final Object input) {
+ Preconditions.checkArgument(input instanceof Map.Entry<?, ?>, "Input must be QName, Value");
+ @SuppressWarnings("rawtypes")
+ Object inputValue = ((Map.Entry) input).getValue();
+ Preconditions.checkArgument(inputValue instanceof DataObject);
+ Class<? extends DataContainer> inputType = ((DataObject) inputValue).getImplementedInterface();
+ ChoiceCaseCodecImpl<?> codec = tryToLoadImplementation(inputType);
+ Preconditions.checkState(codec != null, "Unable to get codec for %s", inputType);
+ if(isAugmenting(codec.getSchema())) {
+ // If choice is augmenting we use QName which defined this augmentation
+ return codec.getDelegate().serialize(new ValueWithQName<>(codec.getSchema().getQName(), inputValue));
+ }
+ return codec.getDelegate().serialize(input);
}
- @Override
- public Collection<BindingCodec<?, ?>> values() {
- return Collections.emptySet();
+ private boolean isAugmenting(final ChoiceCaseNode schema) {
+ if(schema.isAugmenting()) {
+ return true;
+ }
+ QName parentQName = Iterables.get(schema.getPath().getPathTowardsRoot(), 2); // choice QName
+ if(!parentQName.getNamespace().equals(schema.getQName().getNamespace())) {
+ return true;
+ }
+ return false;
}
- private UnsupportedOperationException notModifiable() {
- return new UnsupportedOperationException("Not externally modifiable.");
+ @SuppressWarnings("rawtypes")
+ protected Optional<ChoiceCaseCodecImpl> tryToLoadImplementation(final Type potential) {
+ try {
+ @SuppressWarnings("unchecked")
+ Class<? extends DataContainer> clazz = (Class<? extends DataContainer>) classLoadingStrategy
+ .loadClass(potential);
+ ChoiceCaseCodecImpl codec = tryToLoadImplementation(clazz);
+ addImplementation(codec);
+ return Optional.of(codec);
+ } catch (ClassNotFoundException e) {
+ LOG.warn("Failed to find class for choice {}", potential, e);
+ }
+ return Optional.absent();
}
@Override
- public BindingCodec<Map<QName, Object>, Object> put(final T key, final BindingCodec<?, ?> value) {
- throw notModifiable();
+ protected ChoiceCaseCodecImpl<?> tryToLoadImplementation(final Class<? extends DataContainer> inputType) {
+ ChoiceCaseCodecImpl<?> codec = getCaseCodecFor(inputType);
+ addImplementation(codec);
+ return codec;
}
@Override
- public void putAll(final Map<? extends T, ? extends BindingCodec<?, ?>> m) {
- throw notModifiable();
+ protected void tryToLoadImplementations() {
+ Type type = referencedType(choiceType);
+ Collection<Type> potentialCases;
+ synchronized (choiceToCases) {
+ potentialCases = choiceToCases.get(type);
+ }
+ for (Type potential : potentialCases) {
+ try {
+ tryToLoadImplementation(potential);
+ } catch (CodeGenerationException e) {
+ LOG.warn("Failed to proactively generate choice code for {}", type, e);
+ }
+ }
}
@Override
- public int hashCode() {
- return super.hashCode();
+ protected void adaptForPathImpl(final InstanceIdentifier<?> augTarget, final DataNodeContainer ctxNode) {
+ Optional<ChoiceNode> newChoice = findInstantiatedChoice(ctxNode, choiceName);
+ tryToLoadImplementations();
+ if (newChoice.isPresent()) {
+ for (@SuppressWarnings("rawtypes")
+ Entry<Class, ChoiceCaseCodecImpl<?>> codec : getImplementations().entrySet()) {
+ ChoiceCaseCodecImpl<?> caseCodec = codec.getValue();
+ Optional<ChoiceCaseNode> instantiatedSchema = findInstantiatedCase(newChoice.get(),
+ caseCodec.getSchema());
+ if (instantiatedSchema.isPresent()) {
+ caseCodec.adaptForPath(augTarget, instantiatedSchema.get());
+ }
+ }
+ }
}
- @Override
- public boolean isEmpty() {
- return true;
- }
+ private Optional<ChoiceNode> findInstantiatedChoice(final DataNodeContainer ctxNode, final QName choiceName) {
+ DataSchemaNode potential = ctxNode.getDataChildByName(choiceName);
+ if (potential == null) {
+ potential = ctxNode.getDataChildByName(choiceName.getLocalName());
+ }
- @Override
- public Set<T> keySet() {
- return Collections.emptySet();
- }
+ if (potential instanceof ChoiceNode) {
+ return Optional.of((ChoiceNode) potential);
+ }
- @Override
- public Set<Entry<T, BindingCodec<?, ?>>> entrySet() {
- return Collections.emptySet();
+ return Optional.absent();
}
- @Override
- public boolean containsValue(final Object value) {
- return false;
+ private Optional<ChoiceCaseNode> findInstantiatedCase(final ChoiceNode newChoice, final ChoiceCaseNode schema) {
+ ChoiceCaseNode potential = newChoice.getCaseNodeByName(schema.getQName());
+ if (potential != null) {
+ return Optional.of(potential);
+ }
+ // FIXME: Probably requires more extensive check
+ // e.g. we have one choice and two augmentations from different
+ // modules using same local name
+ // but different namespace / contents
+ return Optional.fromNullable(newChoice.getCaseNodeByName(schema.getQName().getLocalName()));
}
}
}
}
- @SuppressWarnings({ "rawtypes", "unchecked" })
- private static class LateMixinCodec implements BindingCodec, Delegator<BindingCodec> {
-
- private BindingCodec delegate;
-
- @Override
- public BindingCodec getDelegate() {
- if (delegate == null) {
- throw new IllegalStateException("Codec not initialized yet.");
- }
- return delegate;
- }
-
- @Override
- public Object deserialize(final Object input) {
- return getDelegate().deserialize(input);
- }
-
- @Override
- public Object deserialize(final Object input, final InstanceIdentifier bindingIdentifier) {
- return getDelegate().deserialize(input, bindingIdentifier);
- }
-
- @Override
- public Object serialize(final Object input) {
- return getDelegate().serialize(input);
- }
-
- }
-
@SuppressWarnings("rawtypes")
private static class AugmentationCodecWrapper<T extends Augmentation<?>> implements AugmentationCodec<T>,
Delegator<BindingCodec>, LocationAwareBindingCodec<Node<?>, ValueWithQName<T>> {
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.restconf.utils;
+
+import static junit.framework.Assert.assertNotNull;
+import static org.junit.Assert.assertEquals;
+
+import java.io.InputStream;
+import java.util.AbstractMap.SimpleEntry;
+import java.util.Map.Entry;
+
+import javassist.ClassPool;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.unix.rev131222.network.topology.topology.node.path.computation.client.reported.lsp.lsp.tlvs.vs.tlv.vendor.payload.unix.UnixSubTlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.ReportedLsp1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.network.topology.topology.node.path.computation.client.reported.lsp.lsp.tlvs.vs.tlv.vendor.payload.linux.LinuxSubTlvs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClient;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
+import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+
+public class Bug1196Test {
+
+ private static final InstanceIdentifier<PathComputationClient> PATH_TO_CLIENT = InstanceIdentifier.builder(NetworkTopology.class)
+ .child(Topology.class)
+ .child(Node.class)
+ .augmentation(Node1.class)
+ .child(PathComputationClient.class)
+ .build();
+ private RuntimeGeneratedMappingServiceImpl mappingService;
+
+ @Before
+ public void setup() {
+ this.mappingService = new RuntimeGeneratedMappingServiceImpl(new ClassPool());
+
+ final ModuleInfoBackedContext moduleInfo = ModuleInfoBackedContext.create();
+ moduleInfo.addModuleInfos(BindingReflections.loadModuleInfos());
+ this.mappingService.onGlobalContextUpdated(moduleInfo.tryToCreateSchemaContext().get());
+ }
+
+ @Test
+ public void testXmlDataToDataObjectLinuxCase() {
+ final InstanceIdentifier<Topology> instanceIdentifier = InstanceIdentifier.builder(NetworkTopology.class)
+ .child(Topology.class, new TopologyKey(new TopologyId("example-pcep-topology"))).toInstance();
+ final InputStream is = this.getClass().getClassLoader().getResourceAsStream("topology-bug1196-linux.xml");
+ final DataSchemaNode dataSchema = RestconfUtils.toRestconfIdentifier(instanceIdentifier, this.mappingService,
+ this.mappingService.getSchemaContext()).getValue();
+ Topology topology = (Topology) RestconfUtils.dataObjectFromInputStream(instanceIdentifier, is,
+ this.mappingService.getSchemaContext(), this.mappingService, dataSchema);
+ assertNotNull(topology);
+ assertNotNull(topology.getNode());
+ assertEquals(1, topology.getNode().size());
+ Node node = topology.getNode().get(0);
+ Node1 node1 = node.getAugmentation(Node1.class);
+ assertNotNull(node1);
+ final PathComputationClient pcc = node1.getPathComputationClient();
+ final Lsp lsp = pcc.getReportedLsp().get(0).getAugmentation(ReportedLsp1.class).getLsp();
+ org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs tlvs = lsp.getTlvs();
+ assertNotNull(tlvs);
+ VsTlv vsTlv = tlvs.getVsTlv();
+ assertNotNull(vsTlv.getVendorPayload());
+
+ Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> domPcc = mappingService.toDataDom(new SimpleEntry<InstanceIdentifier<?>,DataObject>(PATH_TO_CLIENT,pcc));
+ CompositeNode domPccValue = domPcc.getValue();
+ assertNotNull(domPccValue);
+ CompositeNode domPccTlvs = getFirstReportedLspVsTlvs(domPccValue);
+ assertNotNull(domPccTlvs);
+ assertNotNull(domPccTlvs.getFirstCompositeByName(LinuxSubTlvs.QNAME));
+
+ }
+
+ private CompositeNode getFirstReportedLspVsTlvs(final CompositeNode domPccValue) {
+ return domPccValue.getFirstCompositeByName(ReportedLsp.QNAME).getFirstCompositeByName(Lsp.QNAME).getFirstCompositeByName(Tlvs.QNAME).getFirstCompositeByName(QName.create(Tlvs.QNAME,VsTlv.QNAME.getLocalName()));
+ }
+
+ @Test
+ public void testXmlDataToDataObjectUnixCase() {
+ final InstanceIdentifier<Topology> instanceIdentifier = InstanceIdentifier.builder(NetworkTopology.class)
+ .child(Topology.class, new TopologyKey(new TopologyId("example-pcep-topology"))).toInstance();
+ final InputStream is = this.getClass().getClassLoader().getResourceAsStream("topology-bug1196-unix.xml");
+ final DataSchemaNode dataSchema = RestconfUtils.toRestconfIdentifier(instanceIdentifier, this.mappingService,
+ this.mappingService.getSchemaContext()).getValue();
+ Topology topology = (Topology) RestconfUtils.dataObjectFromInputStream(instanceIdentifier, is,
+ this.mappingService.getSchemaContext(), this.mappingService, dataSchema);
+ assertNotNull(topology);
+ assertNotNull(topology.getNode());
+ assertEquals(1, topology.getNode().size());
+ Node node = topology.getNode().get(0);
+ Node1 node1 = node.getAugmentation(Node1.class);
+ assertNotNull(node1);
+ final PathComputationClient pcc = node1.getPathComputationClient();
+ final Lsp lsp = pcc.getReportedLsp().get(0).getAugmentation(ReportedLsp1.class).getLsp();
+ org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs tlvs = lsp.getTlvs();
+ assertNotNull(tlvs);
+ VsTlv vsTlv = tlvs.getVsTlv();
+ assertNotNull(vsTlv.getVendorPayload());
+
+ Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> domPcc = mappingService.toDataDom(new SimpleEntry<InstanceIdentifier<?>,DataObject>(PATH_TO_CLIENT,pcc));
+ CompositeNode domPccValue = domPcc.getValue();
+ assertNotNull(domPccValue);
+ CompositeNode domPccTlvs = getFirstReportedLspVsTlvs(domPccValue);
+ assertNotNull(domPccTlvs);
+ assertNotNull(domPccTlvs.getFirstCompositeByName(UnixSubTlvs.QNAME));
+ }
+
+}