2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.binding.data.codec.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import java.lang.reflect.Method;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.Map.Entry;
19 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
21 import org.opendaylight.yangtools.yang.binding.Augmentable;
22 import org.opendaylight.yangtools.yang.binding.Augmentation;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 abstract class DataObjectCodecContext<T extends DataNodeContainer> extends DataContainerCodecContext<T> {
42 private static final Logger LOG = LoggerFactory.getLogger(DataObjectCodecContext.class);
44 private final ImmutableMap<String, LeafNodeCodecContext> leafChild;
45 private final ImmutableMap<YangInstanceIdentifier.PathArgument, NodeContextSupplier> byYang;
46 private final ImmutableMap<Method, NodeContextSupplier> byMethod;
47 private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byStreamClass;
48 private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byBindingArgClass;
49 protected final Method augmentationGetter;
51 protected DataObjectCodecContext(final DataContainerCodecPrototype<T> prototype) {
54 this.leafChild = factory().getLeafNodes(bindingClass(), schema());
56 Map<Class<?>, Method> clsToMethod = BindingReflections.getChildrenClassToMethod(bindingClass());
58 Map<YangInstanceIdentifier.PathArgument, NodeContextSupplier> byYangBuilder = new HashMap<>();
59 Map<Method, NodeContextSupplier> byMethodBuilder = new HashMap<>();
60 Map<Class<?>, DataContainerCodecPrototype<?>> byStreamClassBuilder = new HashMap<>();
61 Map<Class<?>, DataContainerCodecPrototype<?>> byBindingArgClassBuilder = new HashMap<>();
63 // Adds leaves to mapping
64 for (LeafNodeCodecContext leaf : leafChild.values()) {
65 byMethodBuilder.put(leaf.getGetter(), leaf);
66 byYangBuilder.put(leaf.getDomPathArgument(), leaf);
69 for (Entry<Class<?>, Method> childDataObj : clsToMethod.entrySet()) {
70 DataContainerCodecPrototype<?> childProto = loadChildPrototype(childDataObj.getKey());
71 byMethodBuilder.put(childDataObj.getValue(), childProto);
72 byStreamClassBuilder.put(childProto.getBindingClass(), childProto);
73 byYangBuilder.put(childProto.getYangArg(), childProto);
74 if (childProto.isChoice()) {
75 ChoiceNodeCodecContext choice = (ChoiceNodeCodecContext) childProto.get();
76 for(Class<?> cazeChild : choice.getCaseChildrenClasses()) {
77 byBindingArgClassBuilder.put(cazeChild, childProto);
81 this.byMethod = ImmutableMap.copyOf(byMethodBuilder);
82 if (Augmentable.class.isAssignableFrom(bindingClass())) {
84 augmentationGetter = bindingClass().getMethod("getAugmentation", Class.class);
85 } catch (NoSuchMethodException | SecurityException e) {
86 throw new IllegalStateException("Could not get required method.",e);
88 ImmutableMap<AugmentationIdentifier, Type> augmentations = factory().getRuntimeContext()
89 .getAvailableAugmentationTypes(schema());
90 for (Entry<AugmentationIdentifier, Type> augment : augmentations.entrySet()) {
91 DataContainerCodecPrototype<?> augProto = getAugmentationPrototype(augment.getValue());
92 if (augProto != null) {
93 byYangBuilder.put(augProto.getYangArg(), augProto);
94 byStreamClassBuilder.put(augProto.getBindingClass(), augProto);
98 augmentationGetter = null;
101 this.byYang = ImmutableMap.copyOf(byYangBuilder);
102 this.byStreamClass = ImmutableMap.copyOf(byStreamClassBuilder);
103 byBindingArgClassBuilder.putAll(byStreamClass);
104 this.byBindingArgClass = ImmutableMap.copyOf(byBindingArgClassBuilder);
108 protected DataContainerCodecContext<?> getStreamChild(final Class<?> childClass) {
109 DataContainerCodecPrototype<?> childProto = byStreamClass.get(childClass);
110 Preconditions.checkArgument(childProto != null, " Child %s is not valid child.",childClass);
111 return childProto.get();
115 protected Optional<DataContainerCodecContext<?>> getPossibleStreamChild(final Class<?> childClass) {
116 DataContainerCodecPrototype<?> childProto = byStreamClass.get(childClass);
117 if(childProto != null) {
118 return Optional.<DataContainerCodecContext<?>>of(childProto.get());
120 return Optional.absent();
124 protected DataContainerCodecContext<?> getIdentifierChild(final InstanceIdentifier.PathArgument arg,
125 final List<YangInstanceIdentifier.PathArgument> builder) {
127 Class<? extends DataObject> argType = arg.getType();
128 DataContainerCodecPrototype<?> ctxProto = byBindingArgClass.get(argType);
129 Preconditions.checkArgument(ctxProto != null,"Invalid child");
131 DataContainerCodecContext<?> context = ctxProto.get();
132 if(context instanceof ChoiceNodeCodecContext) {
133 ChoiceNodeCodecContext casted = (ChoiceNodeCodecContext) context;
134 casted.addYangPathArgument(arg, builder);
135 DataContainerCodecContext<?> caze = casted.getCazeByChildClass(arg.getType());
136 caze.addYangPathArgument(arg, builder);
137 return caze.getIdentifierChild(arg, builder);
139 context.addYangPathArgument(arg, builder);
144 protected NodeCodecContext getYangIdentifierChild(YangInstanceIdentifier.PathArgument arg) {
145 if(arg instanceof NodeIdentifierWithPredicates) {
146 arg = new NodeIdentifier(arg.getNodeType());
148 NodeContextSupplier childSupplier = byYang.get(arg);
149 Preconditions.checkArgument(childSupplier != null, "Argument %s is not valid child of %s", arg, schema());
150 return childSupplier.get();
153 protected final LeafNodeCodecContext getLeafChild(final String name) {
154 final LeafNodeCodecContext value = leafChild.get(name);
155 Preconditions.checkArgument(value != null, "Leaf %s is not valid for %s", name, bindingClass());
159 private DataContainerCodecPrototype<?> loadChildPrototype(final Class<?> childClass) {
160 DataSchemaNode origDef = factory().getRuntimeContext().getSchemaDefinition(childClass);
161 // Direct instantiation or use in same module in which grouping
163 DataSchemaNode sameName;
165 sameName = schema().getDataChildByName(origDef.getQName());
166 } catch (IllegalArgumentException e) {
169 final DataSchemaNode childSchema;
170 if (sameName != null) {
171 // Exactly same schema node
172 if (origDef.equals(sameName)) {
173 childSchema = sameName;
174 // We check if instantiated node was added via uses
175 // statement and is instantiation of same grouping
176 } else if (origDef.equals(SchemaNodeUtils.getRootOriginalIfPossible(sameName))) {
177 childSchema = sameName;
179 // Node has same name, but clearly is different
183 // We are looking for instantiation via uses in other module
184 QName instantiedName = QName.create(namespace(), origDef.getQName().getLocalName());
185 DataSchemaNode potential = schema().getDataChildByName(instantiedName);
186 // We check if it is really instantiated from same
187 // definition as class was derived
188 if (potential != null && origDef.equals(SchemaNodeUtils.getRootOriginalIfPossible(potential))) {
189 childSchema = potential;
194 Preconditions.checkArgument(childSchema != null, "Node %s does not have child named %s", schema(), childClass);
195 return DataContainerCodecPrototype.from(childClass, childSchema, factory());
198 private DataContainerCodecPrototype<?> getAugmentationPrototype(final Type value) {
199 ClassLoadingStrategy loader = factory().getRuntimeContext().getStrategy();
200 @SuppressWarnings("rawtypes")
201 final Class augClass;
203 augClass = loader.loadClass(value);
204 } catch (ClassNotFoundException e) {
205 LOG.warn("Failed to load augmentation prototype for {}", value, e);
209 Entry<AugmentationIdentifier, AugmentationSchema> augSchema = factory().getRuntimeContext()
210 .getResolvedAugmentationSchema(schema(), augClass);
211 return DataContainerCodecPrototype.from(augClass, augSchema.getKey(), augSchema.getValue(), factory());
214 @SuppressWarnings("rawtypes")
215 Object getBindingChildValue(final Method method, final NormalizedNodeContainer domData) {
216 NodeCodecContext childContext = byMethod.get(method).get();
217 Optional<NormalizedNode<?, ?>> domChild = domData.getChild(childContext.getDomPathArgument());
218 if (domChild.isPresent()) {
219 return childContext.dataFromNormalizedNode(domChild.get());
224 public Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(
225 final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
227 @SuppressWarnings("rawtypes")
228 Map map = new HashMap<>();
230 for(DataContainerCodecPrototype<?> value : byStreamClass.values()) {
231 if(Augmentation.class.isAssignableFrom(value.getBindingClass())) {
232 Optional<NormalizedNode<?, ?>> augData = data.getChild(value.getYangArg());
233 if(augData.isPresent()) {
234 map.put(value.getBindingClass(), value.get().dataFromNormalizedNode(augData.get()));
241 public Collection<Method> getHashCodeAndEqualsMethods() {
242 // FIXME: Sort method in same order as in hashCode for generated class.
243 return byMethod.keySet();