Mechanical code cleanup (sal-common-impl)
[controller.git] / opendaylight / md-sal / sal-common-impl / src / main / java / org / opendaylight / controller / md / sal / common / impl / util / compat / DataNormalizationOperation.java
index 7c5c2ba0e090b85f1833308264b16abbc38e2d1f..bbe366016342a7998c961e5028538e56a4b57f9b 100644 (file)
@@ -7,9 +7,10 @@
  */
 package org.opendaylight.controller.md.sal.common.impl.util.compat;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
+import com.google.common.base.Optional;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
@@ -17,26 +18,17 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
-
-import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.yangtools.concepts.Identifiable;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 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.YangInstanceIdentifier.NodeWithValue;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.Node;
-import org.opendaylight.yangtools.yang.data.api.SimpleNode;
-import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
@@ -50,16 +42,8 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
 
-import com.google.common.base.Optional;
-import com.google.common.collect.FluentIterable;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-
-/**
- * @deprecated This class provides compatibility between {@link CompositeNode} and {@link NormalizedNode}.
- *             Users of this class should use {@link NormalizedNode}s directly.
- */
 @Deprecated
 public abstract class DataNormalizationOperation<T extends PathArgument> implements Identifiable<T> {
 
@@ -69,7 +53,7 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
     @Override
     public T getIdentifier() {
         return identifier;
-    };
+    }
 
     protected DataNormalizationOperation(final T identifier, final SchemaNode schema) {
         super();
@@ -98,7 +82,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
 
     public abstract DataNormalizationOperation<?> getChild(QName child) throws DataNormalizationException;
 
-    public abstract NormalizedNode<?, ?> normalize(Node<?> legacyData);
 
     public abstract boolean isLeaf();
 
@@ -113,15 +96,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(identifier,potential);
         }
 
-        @Override
-        public NormalizedNode<?, ?> normalize(final Node<?> legacyData) {
-            checkArgument(legacyData != null);
-            checkArgument(legacyData instanceof SimpleNode<?>);
-            return normalizeImpl((SimpleNode<?>) legacyData);
-        }
-
-        protected abstract NormalizedNode<?, ?> normalizeImpl(SimpleNode<?> node);
-
         @Override
         public DataNormalizationOperation<?> getChild(final PathArgument child) {
             return null;
@@ -150,11 +124,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(new NodeIdentifier(potential.getQName()),potential);
         }
 
-        @Override
-        protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
-            return ImmutableNodes.leafNode(node.getNodeType(), node.getValue());
-        }
-
     }
 
     private static final class LeafListEntryNormalization extends SimpleTypeNormalization<NodeWithValue> {
@@ -163,13 +132,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(new NodeWithValue(potential.getQName(), null),potential);
         }
 
-        @Override
-        protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
-            NodeWithValue nodeId = new NodeWithValue(node.getNodeType(), node.getValue());
-            return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId).withValue(node.getValue()).build();
-        }
-
-
         @Override
         public boolean isKeyedEntry() {
             return true;
@@ -183,58 +145,11 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(identifier,schema);
         }
 
-        @SuppressWarnings({ "rawtypes", "unchecked" })
-        @Override
-        public final NormalizedNode<?, ?> normalize(final Node<?> legacyData) {
-            checkArgument(legacyData != null);
-            if (!isMixin() && getIdentifier().getNodeType() != null) {
-                checkArgument(getIdentifier().getNodeType().equals(legacyData.getNodeType()),
-                        "Node QName must be %s was %s", getIdentifier().getNodeType(), legacyData.getNodeType());
-            }
-            checkArgument(legacyData instanceof CompositeNode, "Node %s should be composite", legacyData);
-            CompositeNode compositeNode = (CompositeNode) legacyData;
-            NormalizedNodeContainerBuilder builder = createBuilder(compositeNode);
-
-            Set<DataNormalizationOperation<?>> usedMixins = new HashSet<>();
-            for (Node<?> childLegacy : compositeNode.getValue()) {
-                final DataNormalizationOperation childOp;
-
-                try {
-                    childOp = getChild(childLegacy.getNodeType());
-                } catch (DataNormalizationException e) {
-                    throw new IllegalArgumentException(String.format("Failed to normalize data %s", compositeNode.getValue()), e);
-                }
-
-                // We skip unknown nodes if this node is mixin since
-                // it's nodes and parent nodes are interleaved
-                if (childOp == null && isMixin()) {
-                    continue;
-                }
-
-                checkArgument(childOp != null, "Node %s is not allowed inside %s", childLegacy.getNodeType(),
-                        getIdentifier());
-                if (childOp.isMixin()) {
-                    if (usedMixins.contains(childOp)) {
-                        // We already run / processed that mixin, so to avoid
-                        // duplicity we are skipping next nodes.
-                        continue;
-                    }
-                    builder.addChild(childOp.normalize(compositeNode));
-                    usedMixins.add(childOp);
-                } else {
-                    builder.addChild(childOp.normalize(childLegacy));
-                }
-            }
-            return builder.build();
-        }
-
         @Override
         public boolean isLeaf() {
             return false;
         }
 
-        @SuppressWarnings("rawtypes")
-        protected abstract NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode);
 
     }
 
@@ -287,7 +202,7 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
         private DataNormalizationOperation<?> register(final DataNormalizationOperation<?> potential) {
             if (potential != null) {
                 byArg.put(potential.getIdentifier(), potential);
-                for (QName qName : potential.getQNameIdentifiers()) {
+                for (final QName qName : potential.getQNameIdentifiers()) {
                     byQName.put(qName, potential);
                 }
             }
@@ -306,25 +221,11 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             keyDefinition = schema.getKeyDefinition();
         }
 
-        @Override
-        protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
-            ImmutableMap.Builder<QName, Object> keys = ImmutableMap.builder();
-            for (QName key : keyDefinition) {
-
-                SimpleNode<?> valueNode = checkNotNull(compositeNode.getFirstSimpleByName(key),
-                        "List node %s MUST contain leaf %s with value.", getIdentifier().getNodeType(), key);
-                keys.put(key, valueNode.getValue());
-            }
-
-            return Builders.mapEntryBuilder().withNodeIdentifier(
-                    new NodeIdentifierWithPredicates(getIdentifier().getNodeType(), keys.build()));
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
-            DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = Builders
+            final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = Builders
                     .mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) currentArg);
-            for (Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg).getKeyValues().entrySet()) {
+            for (final Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg).getKeyValues().entrySet()) {
                 builder.addChild(Builders.leafBuilder()
                         //
                         .withNodeIdentifier(new NodeIdentifier(keyValue.getKey())).withValue(keyValue.getValue())
@@ -346,11 +247,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(new NodeIdentifier(schema.getQName()), schema,schema);
         }
 
-        @Override
-        protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
-            return Builders.unkeyedListEntryBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.unkeyedListEntryBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
@@ -364,11 +260,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(new NodeIdentifier(schema.getQName()),schema, schema);
         }
 
-        @Override
-        protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
-            return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.containerBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
@@ -398,11 +289,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(potential);
         }
 
-        @Override
-        protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
-            return Builders.orderedLeafSetBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.orderedLeafSetBuilder().withNodeIdentifier(getIdentifier()).build();
@@ -417,12 +303,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(new NodeIdentifier(potential.getQName()),potential);
             innerOp = new LeafListEntryNormalization(potential);
         }
-
-        @Override
-        protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
-            return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier()).build();
@@ -462,12 +342,12 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
         @Override
         protected DataNormalizationOperation<?> fromLocalSchemaAndQName(final DataNodeContainer schema, final QName child)
                 throws DataNormalizationException {
-            Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
+            final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
             if (!potential.isPresent()) {
                 return null;
             }
 
-            DataSchemaNode result = potential.get();
+            final DataSchemaNode result = potential.get();
             // We try to look up if this node was added by augmentation
             if ((schema instanceof DataSchemaNode) && result.isAugmenting()) {
                 return fromAugmentation(schema, (AugmentationTarget) schema, result);
@@ -480,12 +360,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             return getIdentifier().getPossibleChildNames();
         }
 
-        @SuppressWarnings("rawtypes")
-        @Override
-        protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
-            return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier()).build();
@@ -503,12 +377,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
                     Collections.<QName, Object> emptyMap()), list);
         }
 
-        @SuppressWarnings("rawtypes")
-        @Override
-        protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
-            return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
@@ -542,12 +410,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             this.innerNode = new UnkeyedListItemNormalization(list);
         }
 
-        @SuppressWarnings("rawtypes")
-        @Override
-        protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
-            return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier()).build();
@@ -577,12 +439,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             super(list);
         }
 
-        @SuppressWarnings("rawtypes")
-        @Override
-        protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
-            return Builders.orderedMapBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.orderedMapBuilder().withNodeIdentifier(getIdentifier()).build();
@@ -597,14 +453,14 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
 
         protected ChoiceNodeNormalization(final ChoiceSchemaNode schema) {
             super(new NodeIdentifier(schema.getQName()),schema);
-            ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
-            ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
+            final ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
+            final ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
 
-            for (ChoiceCaseNode caze : schema.getCases()) {
-                for (DataSchemaNode cazeChild : caze.getChildNodes()) {
-                    DataNormalizationOperation<?> childOp = fromDataSchemaNode(cazeChild);
+            for (final ChoiceCaseNode caze : schema.getCases()) {
+                for (final DataSchemaNode cazeChild : caze.getChildNodes()) {
+                    final DataNormalizationOperation<?> childOp = fromDataSchemaNode(cazeChild);
                     byArgBuilder.put(childOp.getIdentifier(), childOp);
-                    for (QName qname : childOp.getQNameIdentifiers()) {
+                    for (final QName qname : childOp.getQNameIdentifiers()) {
                         byQNameBuilder.put(qname, childOp);
                     }
                 }
@@ -623,11 +479,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             return byQName.get(child);
         }
 
-        @Override
-        protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
-            return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
-        }
-
         @Override
         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
             return Builders.choiceBuilder().withNodeIdentifier(getIdentifier()).build();
@@ -650,16 +501,6 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             return null;
         }
 
-        @Override
-        public NormalizedNode<?, ?> normalize( final Node<?> legacyData ) {
-            NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> builder =
-                    Builders.anyXmlBuilder().withNodeIdentifier(
-                            new NodeIdentifier( legacyData.getNodeType() ) );
-            // Will be removed
-//            builder.withValue(legacyData);
-            return builder.build();
-        }
-
         @Override
         public boolean isLeaf() {
             return false;
@@ -674,7 +515,7 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
     private static final Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent,final QName child) {
         DataSchemaNode potential = parent.getDataChildByName(child);
         if (potential == null) {
-            Iterable<ChoiceSchemaNode> choices = FluentIterable.from(parent.getChildNodes()).filter(ChoiceSchemaNode.class);
+            final Iterable<ChoiceSchemaNode> choices = FluentIterable.from(parent.getChildNodes()).filter(ChoiceSchemaNode.class);
             potential = findChoice(choices, child);
         }
         return Optional.fromNullable(potential);
@@ -683,12 +524,12 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
     private static DataNormalizationOperation<?> fromSchemaAndQNameChecked(final DataNodeContainer schema,
             final QName child) throws DataNormalizationException {
 
-        Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
+        final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
         if (!potential.isPresent()) {
             throw new DataNormalizationException(String.format("Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,schema.getChildNodes()));
         }
 
-        DataSchemaNode result = potential.get();
+        final DataSchemaNode result = potential.get();
         // We try to look up if this node was added by augmentation
         if ((schema instanceof DataSchemaNode) && result.isAugmenting()) {
             return fromAugmentation(schema, (AugmentationTarget) schema, result);
@@ -698,8 +539,8 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
 
     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
         ChoiceSchemaNode foundChoice = null;
-        choiceLoop: for (ChoiceSchemaNode choice : choices) {
-            for (ChoiceCaseNode caze : choice.getCases()) {
+        choiceLoop: for (final ChoiceSchemaNode choice : choices) {
+            for (final ChoiceCaseNode caze : choice.getCases()) {
                 if (findChildSchemaNode(caze, child).isPresent()) {
                     foundChoice = choice;
                     break choiceLoop;
@@ -710,19 +551,19 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
     }
 
     public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
-        ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
-        for (DataSchemaNode child : augmentation.getChildNodes()) {
+        final ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
+        for (final DataSchemaNode child : augmentation.getChildNodes()) {
             potentialChildren.add(child.getQName());
         }
         return new AugmentationIdentifier(potentialChildren.build());
     }
 
     private static DataNodeContainer augmentationProxy(final AugmentationSchema augmentation, final DataNodeContainer schema) {
-        Set<DataSchemaNode> children = new HashSet<>();
-        for (DataSchemaNode augNode : augmentation.getChildNodes()) {
+        final Set<DataSchemaNode> children = new HashSet<>();
+        for (final DataSchemaNode augNode : augmentation.getChildNodes()) {
             children.add(schema.getDataChildByName(augNode.getQName()));
         }
-        return new DataSchemaContainerProxy(children);
+        return new EffectiveAugmentationSchema(augmentation, children);
     }
 
     /**
@@ -742,8 +583,8 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
     private static DataNormalizationOperation<?> fromAugmentation(final DataNodeContainer parent,
             final AugmentationTarget parentAug, final DataSchemaNode child) {
         AugmentationSchema augmentation = null;
-        for (AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
-            DataSchemaNode potential = aug.getDataChildByName(child.getQName());
+        for (final AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
+            final DataSchemaNode potential = aug.getDataChildByName(child.getQName());
             if (potential != null) {
                 augmentation = aug;
                 break;
@@ -776,7 +617,7 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
     }
 
     private static DataNormalizationOperation<?> fromListSchemaNode(final ListSchemaNode potential) {
-        List<QName> keyDefinition = potential.getKeyDefinition();
+        final List<QName> keyDefinition = potential.getKeyDefinition();
         if(keyDefinition == null || keyDefinition.isEmpty()) {
             return new UnkeyedListMixinNormalization(potential);
         }