Introduce RequiredElementCountException
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / UnorderedMapModificationStrategy.java
index 54aad342bcf0ea7fe82a5d60b8eaf21fc9c8ec8b..0a4ce5a12040ac9a4d4dc606185d9889e7ecae13 100644 (file)
@@ -1,44 +1,70 @@
 /*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
- * This program and the accompanying materials are made available under the terms of the Eclipse
- * Public License v1.0 which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import static com.google.common.base.Preconditions.checkArgument;
+package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Optional;
+import java.util.Optional;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 
 final class UnorderedMapModificationStrategy extends AbstractNodeContainerModificationStrategy {
     private final Optional<ModificationApplyOperation> entryStrategy;
 
-    UnorderedMapModificationStrategy(final ListSchemaNode schema, final TreeType treeType) {
-        super(MapNode.class, treeType);
-        entryStrategy = Optional.<ModificationApplyOperation>of(new ListEntryModificationStrategy(schema, treeType));
+    UnorderedMapModificationStrategy(final ListSchemaNode schema, final DataTreeConfiguration treeConfig) {
+        super(MapNode.class, treeConfig);
+        entryStrategy = Optional.of(new ListEntryModificationStrategy(schema, treeConfig));
     }
 
     @SuppressWarnings("rawtypes")
     @Override
     protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
-        checkArgument(original instanceof MapNode);
-        return ImmutableMapNodeBuilder.create((MapNode) original);
+        // If the DataTree is rooted at a MapEntryNode the original value will be MapEntryNode
+        // so make sure we can handle this aswell
+        if (original instanceof MapNode) {
+            return ImmutableMapNodeBuilder.create((MapNode) original);
+        } else if (original instanceof MapEntryNode) {
+            return ImmutableMapEntryNodeBuilder.create((MapEntryNode) original);
+        }
+        throw new IllegalArgumentException("MapModification strategy can only handle MapNode or MapEntryNode's, "
+                + "offending node: " + original);
+    }
+
+    @Override
+    protected NormalizedNode<?, ?> createEmptyValue(final NormalizedNode<?, ?> original) {
+        if (original instanceof MapNode) {
+            return ImmutableMapNodeBuilder.create().withNodeIdentifier(((MapNode) original).getIdentifier()).build();
+        } else if (original instanceof MapEntryNode) {
+            return ImmutableMapEntryNodeBuilder.create().withNodeIdentifier(
+                ((MapEntryNode) original).getIdentifier()).build();
+        }
+        throw new IllegalArgumentException("MapModification strategy can only handle MapNode or MapEntryNode's, "
+                + "offending node: " + original);
     }
 
     @Override
     public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
         if (identifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
             return entryStrategy;
+        } else if (entryStrategy.isPresent()) {
+            // In case we already are in a MapEntry node(for example DataTree rooted at MapEntry)
+            // try to retrieve the child that the identifier should be pointing to from our entryStrategy
+            // if we have one. If the entryStrategy cannot find this child we just return the absent
+            // we get from it.
+            return entryStrategy.get().getChild(identifier);
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     @Override