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.controller.md.sal.common.impl.util.compat;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.Map.Entry;
19 import java.util.concurrent.ConcurrentHashMap;
21 import org.opendaylight.yangtools.concepts.Identifiable;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.Node;
30 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
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.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
38 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
39 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
40 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
41 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
43 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import com.google.common.collect.FluentIterable;
50 import com.google.common.collect.ImmutableMap;
51 import com.google.common.collect.ImmutableSet;
53 public abstract class DataNormalizationOperation<T extends PathArgument> implements Identifiable<T> {
55 private final T identifier;
58 public T getIdentifier() {
62 protected DataNormalizationOperation(final T identifier) {
64 this.identifier = identifier;
67 public boolean isMixin() {
72 public boolean isKeyedEntry() {
76 protected Set<QName> getQNameIdentifiers() {
77 return Collections.singleton(identifier.getNodeType());
80 public abstract DataNormalizationOperation<?> getChild(final PathArgument child) throws DataNormalizationException;
82 public abstract DataNormalizationOperation<?> getChild(QName child) throws DataNormalizationException;
84 public abstract NormalizedNode<?, ?> normalize(Node<?> legacyData);
86 private static abstract class SimpleTypeNormalization<T extends PathArgument> extends DataNormalizationOperation<T> {
88 protected SimpleTypeNormalization(final T identifier) {
93 public NormalizedNode<?, ?> normalize(final Node<?> legacyData) {
94 checkArgument(legacyData != null);
95 checkArgument(legacyData instanceof SimpleNode<?>);
96 return normalizeImpl((SimpleNode<?>) legacyData);
99 protected abstract NormalizedNode<?, ?> normalizeImpl(SimpleNode<?> node);
102 public DataNormalizationOperation<?> getChild(final PathArgument child) {
107 public DataNormalizationOperation<?> getChild(final QName child) {
112 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
113 // TODO Auto-generated method stub
119 private static final class LeafNormalization extends SimpleTypeNormalization<NodeIdentifier> {
121 protected LeafNormalization(final NodeIdentifier identifier) {
126 protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
127 return ImmutableNodes.leafNode(node.getNodeType(), node.getValue());
132 private static final class LeafListEntryNormalization extends SimpleTypeNormalization<NodeWithValue> {
134 public LeafListEntryNormalization(final LeafListSchemaNode potential) {
135 super(new NodeWithValue(potential.getQName(), null));
139 protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
140 NodeWithValue nodeId = new NodeWithValue(node.getNodeType(), node.getValue());
141 return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId).withValue(node.getValue()).build();
146 public boolean isKeyedEntry() {
151 private static abstract class CompositeNodeNormalizationOpertation<T extends PathArgument> extends
152 DataNormalizationOperation<T> {
154 protected CompositeNodeNormalizationOpertation(final T identifier) {
158 @SuppressWarnings({ "rawtypes", "unchecked" })
160 public final NormalizedNodeContainer<?, ?, ?> normalize(final Node<?> legacyData) {
161 checkArgument(legacyData != null);
162 if (!isMixin() && getIdentifier().getNodeType() != null) {
163 checkArgument(getIdentifier().getNodeType().equals(legacyData.getNodeType()),
164 "Node QName must be %s was %s", getIdentifier().getNodeType(), legacyData.getNodeType());
166 checkArgument(legacyData instanceof CompositeNode, "Node %s should be composite", legacyData);
167 CompositeNode compositeNode = (CompositeNode) legacyData;
168 NormalizedNodeContainerBuilder builder = createBuilder(compositeNode);
170 Set<DataNormalizationOperation<?>> usedMixins = new HashSet<>();
171 for (Node<?> childLegacy : compositeNode.getValue()) {
172 final DataNormalizationOperation childOp;
175 childOp = getChild(childLegacy.getNodeType());
176 } catch (DataNormalizationException e) {
177 throw new IllegalArgumentException(String.format("Failed to normalize data %s", compositeNode.getValue()), e);
180 // We skip unknown nodes if this node is mixin since
181 // it's nodes and parent nodes are interleaved
182 if (childOp == null && isMixin()) {
186 checkArgument(childOp != null, "Node %s is not allowed inside %s", childLegacy.getNodeType(),
188 if (childOp.isMixin()) {
189 if (usedMixins.contains(childOp)) {
190 // We already run / processed that mixin, so to avoid
191 // duplicity we are skipping next nodes.
194 builder.addChild(childOp.normalize(compositeNode));
195 usedMixins.add(childOp);
197 builder.addChild(childOp.normalize(childLegacy));
200 return (NormalizedNodeContainer<?, ?, ?>) builder.build();
203 @SuppressWarnings("rawtypes")
204 protected abstract NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode);
208 private static abstract class DataContainerNormalizationOperation<T extends PathArgument> extends
209 CompositeNodeNormalizationOpertation<T> {
211 private final DataNodeContainer schema;
212 private final Map<QName, DataNormalizationOperation<?>> byQName;
213 private final Map<PathArgument, DataNormalizationOperation<?>> byArg;
215 protected DataContainerNormalizationOperation(final T identifier, final DataNodeContainer schema) {
217 this.schema = schema;
218 this.byArg = new ConcurrentHashMap<>();
219 this.byQName = new ConcurrentHashMap<>();
223 public DataNormalizationOperation<?> getChild(final PathArgument child) throws DataNormalizationException {
224 DataNormalizationOperation<?> potential = byArg.get(child);
225 if (potential != null) {
228 potential = fromSchema(schema, child);
229 return register(potential);
233 public DataNormalizationOperation<?> getChild(final QName child) throws DataNormalizationException {
234 DataNormalizationOperation<?> potential = byQName.get(child);
235 if (potential != null) {
238 potential = fromSchemaAndPathArgument(schema, child);
239 return register(potential);
242 private DataNormalizationOperation<?> register(final DataNormalizationOperation<?> potential) {
243 if (potential != null) {
244 byArg.put(potential.getIdentifier(), potential);
245 for (QName qName : potential.getQNameIdentifiers()) {
246 byQName.put(qName, potential);
254 private static final class ListItemNormalization extends
255 DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
257 private final List<QName> keyDefinition;
259 protected ListItemNormalization(final NodeIdentifierWithPredicates identifier, final ListSchemaNode schema) {
260 super(identifier, schema);
261 keyDefinition = schema.getKeyDefinition();
265 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
266 ImmutableMap.Builder<QName, Object> keys = ImmutableMap.builder();
267 for (QName key : keyDefinition) {
269 SimpleNode<?> valueNode = checkNotNull(compositeNode.getFirstSimpleByName(key),
270 "List node %s MUST contain leaf %s with value.", getIdentifier().getNodeType(), key);
271 keys.put(key, valueNode.getValue());
274 return Builders.mapEntryBuilder().withNodeIdentifier(
275 new NodeIdentifierWithPredicates(getIdentifier().getNodeType(), keys.build()));
279 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
280 DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = Builders
281 .mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) currentArg);
282 for (Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg).getKeyValues().entrySet()) {
283 builder.addChild(Builders.leafBuilder()
285 .withNodeIdentifier(new NodeIdentifier(keyValue.getKey())).withValue(keyValue.getValue())
288 return builder.build();
293 public boolean isKeyedEntry() {
298 private static final class ContainerNormalization extends DataContainerNormalizationOperation<NodeIdentifier> {
300 protected ContainerNormalization(final ContainerSchemaNode schema) {
301 super(new NodeIdentifier(schema.getQName()), schema);
305 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
306 return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
310 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
311 return Builders.containerBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
316 private static abstract class MixinNormalizationOp<T extends PathArgument> extends
317 CompositeNodeNormalizationOpertation<T> {
319 protected MixinNormalizationOp(final T identifier) {
324 public final boolean isMixin() {
330 private static final class LeafListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
332 private final DataNormalizationOperation<?> innerOp;
334 public LeafListMixinNormalization(final LeafListSchemaNode potential) {
335 super(new NodeIdentifier(potential.getQName()));
336 innerOp = new LeafListEntryNormalization(potential);
340 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
341 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
345 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
346 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier()).build();
350 public DataNormalizationOperation<?> getChild(final PathArgument child) {
351 if (child instanceof NodeWithValue) {
358 public DataNormalizationOperation<?> getChild(final QName child) {
359 if (getIdentifier().getNodeType().equals(child)) {
366 private static final class AugmentationNormalization extends MixinNormalizationOp<AugmentationIdentifier> {
368 private final Map<QName, DataNormalizationOperation<?>> byQName;
369 private final Map<PathArgument, DataNormalizationOperation<?>> byArg;
371 public AugmentationNormalization(final AugmentationSchema augmentation, final DataNodeContainer schema) {
372 super(augmentationIdentifierFrom(augmentation));
374 ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
375 ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
377 for (DataSchemaNode augNode : augmentation.getChildNodes()) {
378 DataSchemaNode resolvedNode = schema.getDataChildByName(augNode.getQName());
379 DataNormalizationOperation<?> resolvedOp = fromDataSchemaNode(resolvedNode);
380 byArgBuilder.put(resolvedOp.getIdentifier(), resolvedOp);
381 for (QName resQName : resolvedOp.getQNameIdentifiers()) {
382 byQNameBuilder.put(resQName, resolvedOp);
385 byQName = byQNameBuilder.build();
386 byArg = byArgBuilder.build();
391 public DataNormalizationOperation<?> getChild(final PathArgument child) {
392 return byArg.get(child);
396 public DataNormalizationOperation<?> getChild(final QName child) {
397 return byQName.get(child);
401 protected Set<QName> getQNameIdentifiers() {
402 return getIdentifier().getPossibleChildNames();
405 @SuppressWarnings("rawtypes")
407 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
408 return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
412 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
413 return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier()).build();
418 private static final class ListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
420 private final ListItemNormalization innerNode;
422 public ListMixinNormalization(final ListSchemaNode list) {
423 super(new NodeIdentifier(list.getQName()));
424 this.innerNode = new ListItemNormalization(new NodeIdentifierWithPredicates(list.getQName(),
425 Collections.<QName, Object> emptyMap()), list);
428 @SuppressWarnings("rawtypes")
430 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
431 return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
435 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
436 return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
440 public DataNormalizationOperation<?> getChild(final PathArgument child) {
441 if (child.getNodeType().equals(getIdentifier().getNodeType())) {
448 public DataNormalizationOperation<?> getChild(final QName child) {
449 if (getIdentifier().getNodeType().equals(child)) {
457 private static class ChoiceNodeNormalization extends MixinNormalizationOp<NodeIdentifier> {
459 private final ImmutableMap<QName, DataNormalizationOperation<?>> byQName;
460 private final ImmutableMap<PathArgument, DataNormalizationOperation<?>> byArg;
462 protected ChoiceNodeNormalization(final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
463 super(new NodeIdentifier(schema.getQName()));
464 ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
465 ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
467 for (ChoiceCaseNode caze : schema.getCases()) {
468 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
469 DataNormalizationOperation<?> childOp = fromDataSchemaNode(cazeChild);
470 byArgBuilder.put(childOp.getIdentifier(), childOp);
471 for (QName qname : childOp.getQNameIdentifiers()) {
472 byQNameBuilder.put(qname, childOp);
476 byQName = byQNameBuilder.build();
477 byArg = byArgBuilder.build();
481 public DataNormalizationOperation<?> getChild(final PathArgument child) {
482 return byArg.get(child);
486 public DataNormalizationOperation<?> getChild(final QName child) {
487 return byQName.get(child);
491 protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
492 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
496 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
497 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier()).build();
501 private static DataNormalizationOperation<?> fromSchemaAndPathArgument(final DataNodeContainer schema,
502 final QName child) throws DataNormalizationException {
503 DataSchemaNode potential = schema.getDataChildByName(child);
504 if (potential == null) {
505 Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices = FluentIterable.from(
506 schema.getChildNodes()).filter(org.opendaylight.yangtools.yang.model.api.ChoiceNode.class);
507 potential = findChoice(choices, child);
510 if (potential == null) {
511 throw new DataNormalizationException(String.format("Supplied QName %s is not valid according to schema %s", child, schema));
514 if ((schema instanceof DataSchemaNode) && !((DataSchemaNode) schema).isAugmenting() && potential.isAugmenting()) {
515 return fromAugmentation(schema, (AugmentationTarget) schema, potential);
517 return fromDataSchemaNode(potential);
520 private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
521 final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices, final QName child) {
522 org.opendaylight.yangtools.yang.model.api.ChoiceNode foundChoice = null;
523 choiceLoop: for (org.opendaylight.yangtools.yang.model.api.ChoiceNode choice : choices) {
524 for (ChoiceCaseNode caze : choice.getCases()) {
525 if (caze.getDataChildByName(child) != null) {
526 foundChoice = choice;
534 public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
535 ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
536 for (DataSchemaNode child : augmentation.getChildNodes()) {
537 potentialChildren.add(child.getQName());
539 return new AugmentationIdentifier(null, potentialChildren.build());
542 private static AugmentationNormalization fromAugmentation(final DataNodeContainer schema,
543 final AugmentationTarget augments, final DataSchemaNode potential) {
544 AugmentationSchema augmentation = null;
545 for (AugmentationSchema aug : augments.getAvailableAugmentations()) {
546 DataSchemaNode child = aug.getDataChildByName(potential.getQName());
553 if (augmentation != null) {
554 return new AugmentationNormalization(augmentation, schema);
560 private static DataNormalizationOperation<?> fromSchema(final DataNodeContainer schema, final PathArgument child) throws DataNormalizationException {
561 if (child instanceof AugmentationIdentifier) {
562 return fromSchemaAndPathArgument(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
565 return fromSchemaAndPathArgument(schema, child.getNodeType());
568 public static DataNormalizationOperation<?> fromDataSchemaNode(final DataSchemaNode potential) {
569 if (potential instanceof ContainerSchemaNode) {
570 return new ContainerNormalization((ContainerSchemaNode) potential);
571 } else if (potential instanceof ListSchemaNode) {
572 return new ListMixinNormalization((ListSchemaNode) potential);
573 } else if (potential instanceof LeafSchemaNode) {
574 return new LeafNormalization(new NodeIdentifier(potential.getQName()));
575 } else if (potential instanceof org.opendaylight.yangtools.yang.model.api.ChoiceNode) {
576 return new ChoiceNodeNormalization((org.opendaylight.yangtools.yang.model.api.ChoiceNode) potential);
577 } else if (potential instanceof LeafListSchemaNode) {
578 return new LeafListMixinNormalization((LeafListSchemaNode) potential);
583 public static DataNormalizationOperation<?> from(final SchemaContext ctx) {
584 return new ContainerNormalization(ctx);
587 public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);