Add utility wrappers for instantiating builders/nodes
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / DataNodeContainerModificationStrategy.java
index 30af8fce2926407db91633a9de88664a9572fcd8..fcd07ca7b1378621a2ab5025085c1e3a15228325 100644 (file)
@@ -8,36 +8,24 @@
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
 
-import java.util.HashSet;
-import java.util.Set;
+import com.google.common.base.MoreObjects.ToStringHelper;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import java.util.Optional;
 import java.util.concurrent.ExecutionException;
-
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
-import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Base strategy for applying changes to a ContainerNode, irrespective of its
@@ -45,116 +33,49 @@ import com.google.common.cache.LoadingCache;
  *
  * @param <T> Type of the container node
  */
-abstract class DataNodeContainerModificationStrategy<T extends DataNodeContainer> extends NormalizedNodeContainerModificationStrategy {
+class DataNodeContainerModificationStrategy<T extends DataNodeContainer>
+        extends AbstractNodeContainerModificationStrategy {
+    private static final Logger LOG = LoggerFactory.getLogger(DataNodeContainerModificationStrategy.class);
 
-    private final T schema;
     private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
-            .build(CacheLoader.from(new Function<PathArgument, ModificationApplyOperation>() {
-
+            .build(new CacheLoader<PathArgument, ModificationApplyOperation>() {
                 @Override
-                public ModificationApplyOperation apply(final PathArgument identifier) {
-                    if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
-                        return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema, (AugmentationIdentifier) identifier);
+                public ModificationApplyOperation load(@Nonnull final PathArgument key) {
+                    if (key instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
+                        return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema,
+                            (AugmentationIdentifier) key, treeConfig);
                     }
 
-                    DataSchemaNode child = schema.getDataChildByName(identifier.getNodeType());
-                    if (child == null) {
-                        return null;
-                    }
-                    return SchemaAwareApplyOperation.from(child);
+                    final DataSchemaNode child = schema.getDataChildByName(key.getNodeType());
+                    checkArgument(child != null, "Schema %s does not have a node for child %s", schema,
+                            key.getNodeType());
+                    return SchemaAwareApplyOperation.from(child, treeConfig);
                 }
-            }));
+            });
 
-    protected DataNodeContainerModificationStrategy(final T schema,
-            final Class<? extends NormalizedNode<?, ?>> nodeClass) {
-        super(nodeClass);
-        this.schema = schema;
-    }
+    private final DataTreeConfiguration treeConfig;
+    private final T schema;
 
-    protected T getSchema() {
-        return schema;
+    DataNodeContainerModificationStrategy(final NormalizedNodeContainerSupport<?, ?> support, final T schema,
+            final DataTreeConfiguration treeConfig) {
+        super(support, treeConfig);
+        this.schema = requireNonNull(schema, "schema");
+        this.treeConfig = requireNonNull(treeConfig, "treeConfig");
     }
 
     @Override
-    public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
+    public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
         try {
-            return Optional.<ModificationApplyOperation> fromNullable(childCache.get(identifier));
-        } catch (ExecutionException e) {
-            return Optional.absent();
+            return Optional.ofNullable(childCache.get(identifier));
+        } catch (ExecutionException | UncheckedExecutionException e) {
+            LOG.trace("Child {} not present in container schema {} children {}", identifier, this,
+                schema.getChildNodes(), e);
+            return Optional.empty();
         }
     }
 
     @Override
-    @SuppressWarnings("rawtypes")
-    protected abstract DataContainerNodeBuilder createBuilder(NormalizedNode<?, ?> original);
-
-    @Override
-    public String toString() {
-        return getClass().getSimpleName() + " [" + schema + "]";
-    }
-
-    public static class AugmentationModificationStrategy extends DataNodeContainerModificationStrategy<AugmentationSchema> {
-
-        protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
-            super(createAugmentProxy(schema,resolved), AugmentationNode.class);
-        }
-
-        @Override
-        @SuppressWarnings("rawtypes")
-        protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
-            checkArgument(original instanceof AugmentationNode);
-            return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
-        }
-
-
-        private static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
-            Set<DataSchemaNode> realChildSchemas = new HashSet<>();
-            for(DataSchemaNode augChild : schema.getChildNodes()) {
-                realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
-            }
-            return new AugmentationSchemaProxy(schema, realChildSchemas);
-        }
-    }
-
-    public static class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
-
-        public ContainerModificationStrategy(final ContainerSchemaNode schemaNode) {
-            super(schemaNode, ContainerNode.class);
-        }
-
-        @Override
-        @SuppressWarnings("rawtypes")
-        protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
-            checkArgument(original instanceof ContainerNode);
-            return ImmutableContainerNodeBuilder.create((ContainerNode) original);
-        }
-    }
-
-    public static class ListEntryModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
-
-        protected ListEntryModificationStrategy(final ListSchemaNode schema) {
-            super(schema, MapEntryNode.class);
-        }
-
-        @Override
-        @SuppressWarnings("rawtypes")
-        protected final DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
-            checkArgument(original instanceof MapEntryNode);
-            return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
-        }
-    }
-
-    public static class UnkeyedListItemModificationStrategy extends DataNodeContainerModificationStrategy<ListSchemaNode> {
-
-        public UnkeyedListItemModificationStrategy(final ListSchemaNode schemaNode) {
-            super(schemaNode, UnkeyedListEntryNode.class);
-        }
-
-        @Override
-        @SuppressWarnings("rawtypes")
-        protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
-            checkArgument(original instanceof UnkeyedListEntryNode);
-            return ImmutableUnkeyedListEntryNodeBuilder.create((UnkeyedListEntryNode) original);
-        }
+    ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+        return super.addToStringAttributes(helper).add("schema", schema);
     }
-}
\ No newline at end of file
+}