1 package org.opendaylight.controller.md.sal.common.impl.util.compat;
3 import static com.google.common.base.Preconditions.checkArgument;
4 import static com.google.common.base.Preconditions.checkNotNull;
6 import java.util.Collections;
7 import java.util.HashSet;
10 import java.util.Map.Entry;
12 import java.util.concurrent.ConcurrentHashMap;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.Node;
23 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
32 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
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.api.LeafListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import com.google.common.collect.FluentIterable;
43 import com.google.common.collect.ImmutableMap;
44 import com.google.common.collect.ImmutableSet;
46 public abstract class DataNormalizationOperation<T extends PathArgument> implements Identifiable<T> {
48 private final T identifier;
51 public T getIdentifier() {
55 protected DataNormalizationOperation(final T identifier) {
57 this.identifier = identifier;
60 public boolean isMixin() {
64 protected Set<QName> getQNameIdentifiers() {
65 return Collections.singleton(identifier.getNodeType());
68 public abstract DataNormalizationOperation<?> getChild(final PathArgument child);
70 public abstract DataNormalizationOperation<?> getChild(QName child);
72 public abstract NormalizedNode<?, ?> normalize(Node<?> legacyData);
74 private static abstract class SimpleTypeNormalization<T extends PathArgument> extends DataNormalizationOperation<T> {
76 protected SimpleTypeNormalization(final T identifier) {
81 public NormalizedNode<?, ?> normalize(final Node<?> legacyData) {
82 checkArgument(legacyData != null);
83 checkArgument(legacyData instanceof SimpleNode<?>);
84 return normalizeImpl((SimpleNode<?>) legacyData);
87 protected abstract NormalizedNode<?, ?> normalizeImpl(SimpleNode<?> node);
90 public DataNormalizationOperation<?> getChild(final PathArgument child) {
95 public DataNormalizationOperation<?> getChild(final QName child) {
100 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
101 // TODO Auto-generated method stub
107 private static final class LeafNormalization extends SimpleTypeNormalization<NodeIdentifier> {
109 protected LeafNormalization(final NodeIdentifier identifier) {
114 protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
115 return ImmutableNodes.leafNode(node.getNodeType(), node.getValue());
120 private static final class LeafListEntryNormalization extends SimpleTypeNormalization<NodeWithValue> {
122 public LeafListEntryNormalization(final LeafListSchemaNode potential) {
123 super(new NodeWithValue(potential.getQName(), null));
127 protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
128 NodeWithValue nodeId = new NodeWithValue(node.getNodeType(), node.getValue());
129 return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId).withValue(node.getValue()).build();
134 private static abstract class CompositeNodeNormalizationOpertation<T extends PathArgument> extends
135 DataNormalizationOperation<T> {
137 protected CompositeNodeNormalizationOpertation(final T identifier) {
141 @SuppressWarnings({ "rawtypes", "unchecked" })
143 public final NormalizedNodeContainer<?, ?, ?> normalize(final Node<?> legacyData) {
144 checkArgument(legacyData != null);
145 if (!isMixin() && getIdentifier().getNodeType() != null) {
146 checkArgument(getIdentifier().getNodeType().equals(legacyData.getNodeType()),
147 "Node QName must be %s was %s", getIdentifier().getNodeType(), legacyData.getNodeType());
149 checkArgument(legacyData instanceof CompositeNode, "Node %s should be composite", legacyData);
150 CompositeNode compositeNode = (CompositeNode) legacyData;
151 NormalizedNodeContainerBuilder builder = createBuilder(compositeNode);
153 Set<DataNormalizationOperation<?>> usedMixins = new HashSet<>();
154 for (Node<?> childLegacy : compositeNode.getValue()) {
155 DataNormalizationOperation childOp = getChild(childLegacy.getNodeType());
157 // We skip unknown nodes if this node is mixin since
158 // it's nodes and parent nodes are interleaved
159 if (childOp == null && isMixin()) {
163 checkArgument(childOp != null, "Node %s is not allowed inside %s", childLegacy.getNodeType(),
165 if (childOp.isMixin()) {
166 if (usedMixins.contains(childOp)) {
167 // We already run / processed that mixin, so to avoid
169 // skiping next nodes.
172 builder.addChild(childOp.normalize(compositeNode));
173 usedMixins.add(childOp);
175 builder.addChild(childOp.normalize(childLegacy));
178 return (NormalizedNodeContainer<?, ?, ?>) builder.build();
181 @SuppressWarnings("rawtypes")
182 protected abstract NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode);
186 private static abstract class DataContainerNormalizationOperation<T extends PathArgument> extends
187 CompositeNodeNormalizationOpertation<T> {
189 private final DataNodeContainer schema;
190 private final Map<QName, DataNormalizationOperation<?>> byQName;
191 private final Map<PathArgument, DataNormalizationOperation<?>> byArg;
193 protected DataContainerNormalizationOperation(final T identifier, final DataNodeContainer schema) {
195 this.schema = schema;
196 this.byArg = new ConcurrentHashMap<>();
197 this.byQName = new ConcurrentHashMap<>();
201 public DataNormalizationOperation<?> getChild(final PathArgument child) {
202 DataNormalizationOperation<?> potential = byArg.get(child);
203 if (potential != null) {
206 potential = fromSchema(schema, child);
207 return register(potential);
211 public DataNormalizationOperation<?> getChild(final QName child) {
212 DataNormalizationOperation<?> potential = byQName.get(child);
213 if (potential != null) {
216 potential = fromSchemaAndPathArgument(schema, child);
217 return register(potential);
220 private DataNormalizationOperation<?> register(final DataNormalizationOperation<?> potential) {
221 if (potential != null) {
222 byArg.put(potential.getIdentifier(), potential);
223 for (QName qName : potential.getQNameIdentifiers()) {
224 byQName.put(qName, potential);
232 private static final class ListItemNormalization extends
233 DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
235 private final List<QName> keyDefinition;
237 protected ListItemNormalization(final NodeIdentifierWithPredicates identifier, final ListSchemaNode schema) {
238 super(identifier, schema);
239 keyDefinition = schema.getKeyDefinition();
243 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
244 ImmutableMap.Builder<QName, Object> keys = ImmutableMap.builder();
245 for (QName key : keyDefinition) {
246 SimpleNode<?> valueNode = checkNotNull(compositeNode.getFirstSimpleByName(key),"List node %s MUST contain leaf %s with value.",getIdentifier().getNodeType(),key);
247 keys.put(key, valueNode.getValue());
250 return Builders.mapEntryBuilder().withNodeIdentifier(
251 new NodeIdentifierWithPredicates(getIdentifier().getNodeType(), keys.build()));
255 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
256 DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = Builders
257 .mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) currentArg);
258 for (Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg).getKeyValues().entrySet()) {
259 builder.addChild(Builders.leafBuilder()
261 .withNodeIdentifier(new NodeIdentifier(keyValue.getKey())).withValue(keyValue.getValue())
264 return builder.build();
268 private static final class ContainerNormalization extends DataContainerNormalizationOperation<NodeIdentifier> {
270 protected ContainerNormalization(final ContainerSchemaNode schema) {
271 super(new NodeIdentifier(schema.getQName()), schema);
275 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
276 return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
280 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
281 return Builders.containerBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
286 private static abstract class MixinNormalizationOp<T extends PathArgument> extends
287 CompositeNodeNormalizationOpertation<T> {
289 protected MixinNormalizationOp(final T identifier) {
294 public final boolean isMixin() {
300 private static final class LeafListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
302 private final DataNormalizationOperation<?> innerOp;
304 public LeafListMixinNormalization(final LeafListSchemaNode potential) {
305 super(new NodeIdentifier(potential.getQName()));
306 innerOp = new LeafListEntryNormalization(potential);
310 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
311 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
315 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
316 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier()).build();
320 public DataNormalizationOperation<?> getChild(final PathArgument child) {
321 if (child instanceof NodeWithValue) {
328 public DataNormalizationOperation<?> getChild(final QName child) {
329 if (getIdentifier().getNodeType().equals(child)) {
336 private static final class AugmentationNormalization extends MixinNormalizationOp<AugmentationIdentifier> {
338 private final Map<QName, DataNormalizationOperation<?>> byQName;
339 private final Map<PathArgument, DataNormalizationOperation<?>> byArg;
341 public AugmentationNormalization(final AugmentationSchema augmentation, final DataNodeContainer schema) {
342 super(augmentationIdentifierFrom(augmentation));
344 ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
345 ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
347 for (DataSchemaNode augNode : augmentation.getChildNodes()) {
348 DataSchemaNode resolvedNode = schema.getDataChildByName(augNode.getQName());
349 DataNormalizationOperation<?> resolvedOp = fromDataSchemaNode(resolvedNode);
350 byArgBuilder.put(resolvedOp.getIdentifier(), resolvedOp);
351 for (QName resQName : resolvedOp.getQNameIdentifiers()) {
352 byQNameBuilder.put(resQName, resolvedOp);
355 byQName = byQNameBuilder.build();
356 byArg = byArgBuilder.build();
361 public DataNormalizationOperation<?> getChild(final PathArgument child) {
362 return byArg.get(child);
366 public DataNormalizationOperation<?> getChild(final QName child) {
367 return byQName.get(child);
371 protected Set<QName> getQNameIdentifiers() {
372 return getIdentifier().getPossibleChildNames();
375 @SuppressWarnings("rawtypes")
377 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
378 return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
382 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
383 return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier()).build();
388 private static final class ListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
390 private final ListItemNormalization innerNode;
392 public ListMixinNormalization(final ListSchemaNode list) {
393 super(new NodeIdentifier(list.getQName()));
394 this.innerNode = new ListItemNormalization(new NodeIdentifierWithPredicates(list.getQName(),
395 Collections.<QName, Object> emptyMap()), list);
398 @SuppressWarnings("rawtypes")
400 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
401 return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
405 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
406 return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
410 public DataNormalizationOperation<?> getChild(final PathArgument child) {
411 if (child.getNodeType().equals(getIdentifier().getNodeType())) {
418 public DataNormalizationOperation<?> getChild(final QName child) {
419 if (getIdentifier().getNodeType().equals(child)) {
427 private static class ChoiceNodeNormalization extends MixinNormalizationOp<NodeIdentifier> {
429 private final ImmutableMap<QName, DataNormalizationOperation<?>> byQName;
430 private final ImmutableMap<PathArgument, DataNormalizationOperation<?>> byArg;
432 protected ChoiceNodeNormalization(final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
433 super(new NodeIdentifier(schema.getQName()));
434 ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
435 ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
437 for (ChoiceCaseNode caze : schema.getCases()) {
438 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
439 DataNormalizationOperation<?> childOp = fromDataSchemaNode(cazeChild);
440 byArgBuilder.put(childOp.getIdentifier(), childOp);
441 for (QName qname : childOp.getQNameIdentifiers()) {
442 byQNameBuilder.put(qname, childOp);
446 byQName = byQNameBuilder.build();
447 byArg = byArgBuilder.build();
451 public DataNormalizationOperation<?> getChild(final PathArgument child) {
452 return byArg.get(child);
456 public DataNormalizationOperation<?> getChild(final QName child) {
457 return byQName.get(child);
461 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
462 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
466 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
467 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier()).build();
471 public static DataNormalizationOperation<?> fromSchemaAndPathArgument(final DataNodeContainer schema,
473 DataSchemaNode potential = schema.getDataChildByName(child);
474 if (potential == null) {
475 Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices = FluentIterable.from(
476 schema.getChildNodes()).filter(org.opendaylight.yangtools.yang.model.api.ChoiceNode.class);
477 potential = findChoice(choices, child);
479 checkArgument(potential != null, "Supplied QName %s is not valid according to schema %s", child, schema);
480 if ((schema instanceof DataSchemaNode) && !((DataSchemaNode) schema).isAugmenting() && potential.isAugmenting()) {
481 return fromAugmentation(schema, (AugmentationTarget) schema, potential);
483 return fromDataSchemaNode(potential);
486 private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
487 final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices, final QName child) {
488 org.opendaylight.yangtools.yang.model.api.ChoiceNode foundChoice = null;
489 choiceLoop: for (org.opendaylight.yangtools.yang.model.api.ChoiceNode choice : choices) {
490 for (ChoiceCaseNode caze : choice.getCases()) {
491 if (caze.getDataChildByName(child) != null) {
492 foundChoice = choice;
500 public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
501 ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
502 for (DataSchemaNode child : augmentation.getChildNodes()) {
503 potentialChildren.add(child.getQName());
505 return new AugmentationIdentifier(null, potentialChildren.build());
508 private static AugmentationNormalization fromAugmentation(final DataNodeContainer schema,
509 final AugmentationTarget augments, final DataSchemaNode potential) {
510 AugmentationSchema augmentation = null;
511 for (AugmentationSchema aug : augments.getAvailableAugmentations()) {
512 DataSchemaNode child = aug.getDataChildByName(potential.getQName());
519 if (augmentation != null) {
520 return new AugmentationNormalization(augmentation, schema);
526 private static DataNormalizationOperation<?> fromSchema(final DataNodeContainer schema, final PathArgument child) {
527 if (child instanceof AugmentationIdentifier) {
528 return fromSchemaAndPathArgument(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
531 return fromSchemaAndPathArgument(schema, child.getNodeType());
534 public static DataNormalizationOperation<?> fromDataSchemaNode(final DataSchemaNode potential) {
535 if (potential instanceof ContainerSchemaNode) {
536 return new ContainerNormalization((ContainerSchemaNode) potential);
537 } else if (potential instanceof ListSchemaNode) {
538 return new ListMixinNormalization((ListSchemaNode) potential);
539 } else if (potential instanceof LeafSchemaNode) {
540 return new LeafNormalization(new NodeIdentifier(potential.getQName()));
541 } else if (potential instanceof org.opendaylight.yangtools.yang.model.api.ChoiceNode) {
542 return new ChoiceNodeNormalization((org.opendaylight.yangtools.yang.model.api.ChoiceNode) potential);
543 } else if (potential instanceof LeafListSchemaNode) {
544 return new LeafListMixinNormalization((LeafListSchemaNode) potential);
549 public static DataNormalizationOperation<?> from(final SchemaContext ctx) {
550 return new ContainerNormalization(ctx);
553 public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);