/*
 * 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
 */

package org.opendaylight.yangtools.yang.data.impl.schema.tree;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.base.Optional;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.DataTreeConfiguration;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
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 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);
    }

    @Override
    protected NormalizedNode<?, ?> createEmptyValue(final NormalizedNode<?, ?> original) {
        checkArgument(original instanceof MapNode);
        return ImmutableMapNodeBuilder.create().withNodeIdentifier(((MapNode) original).getIdentifier()).build();
    }

    @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();
    }

    @Override
    public String toString() {
        return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
    }
}

@@ -370,75 +370,8 @@
 e;%0A%0A
-import static com.google.common.base.Preconditions.checkArgument;%0A%0A
 impo
@@ -474,24 +474,93 @@
 Identifier;%0A
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;%0A
 import org.o
@@ -857,32 +857,131 @@
 ntainerBuilder;%0A
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;%0A
 import org.opend
@@ -1699,52 +1699,318 @@
     
-checkArgument(original instanceof MapNode);%0A
+// If the DataTree is rooted at a MapEntryNode the original value will be MapEntryNode%0A        // so make sure we can handle this aswell%0A        if (original instanceof MapNode) %7B%0A            return ImmutableMapNodeBuilder.create((MapNode) original);%0A        %7D else if (original instanceof MapEntryNode) %7B%0A    
     
@@ -2024,32 +2024,37 @@
 urn ImmutableMap
+Entry
 NodeBuilder.crea
@@ -2052,32 +2052,37 @@
 lder.create((Map
+Entry
 Node) original);
@@ -2090,137 +2090,448 @@
     
-%7D%0A%0A    @Override%0A    protected NormalizedNode%3C?, ?%3E createEmptyValue(final NormalizedNode%3C?, ?%3E original) %7B%0A        checkArgument
+    %7D%0A        throw new IllegalArgumentException(%22MapModification strategy can only handle MapNode or MapEntryNode's, offending node: %22 + original);%0A    %7D%0A%0A    @Override%0A    protected NormalizedNode%3C?, ?%3E createEmptyValue(final NormalizedNode%3C?, ?%3E original) %7B%0A        if (original instanceof MapNode) %7B%0A            return ImmutableMapNodeBuilder.create().withNodeIdentifier(((MapNode) original).getIdentifier()).build();%0A        %7D else if 
 (ori
@@ -2554,15 +2554,25 @@
  Map
-Node);%0A
+EntryNode) %7B%0A    
     
@@ -2586,32 +2586,37 @@
 urn ImmutableMap
+Entry
 NodeBuilder.crea
@@ -2636,32 +2636,37 @@
 Identifier(((Map
+Entry
 Node) original).
@@ -2687,24 +2687,177 @@
 )).build();%0A
+        %7D%0A        throw new IllegalArgumentException(%22MapModification strategy can only handle MapNode or MapEntryNode's, offending node: %22 + original);%0A
     %7D%0A%0A    @

Fix list modification with DataTree rooted at MapEntryNode