X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fyang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fdata%2Fimpl%2FNodeFactory.java;fp=opendaylight%2Fsal%2Fyang-prototype%2Fyang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fdata%2Fimpl%2FNodeFactory.java;h=8e6e77498baec6a607957d07984df37339059e01;hp=8b3b7d45ae45bfead1017e302f3664b4ca3ba75c;hb=39b426fb075e8690911802f70aceb865cc83715d;hpb=9d0e5ef647eb242f7750e30482b403676f56c682 diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeFactory.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeFactory.java old mode 100755 new mode 100644 index 8b3b7d45ae..8e6e77498b --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeFactory.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeFactory.java @@ -10,6 +10,7 @@ package org.opendaylight.controller.yang.data.impl; import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Stack; @@ -20,6 +21,7 @@ import org.opendaylight.controller.yang.data.api.ModifyAction; import org.opendaylight.controller.yang.data.api.MutableCompositeNode; import org.opendaylight.controller.yang.data.api.MutableSimpleNode; import org.opendaylight.controller.yang.data.api.Node; +import org.opendaylight.controller.yang.data.api.NodeModification; import org.opendaylight.controller.yang.data.api.SimpleNode; /** @@ -34,47 +36,56 @@ public abstract class NodeFactory { * @param value * @return simple node modification, based on given qname, value and parent */ - public static SimpleNode createSimpleNode(QName qName, + public static SimpleNode createImmutableSimpleNode(QName qName, CompositeNode parent, T value) { - SimpleNodeTOImpl simpleNodeTOImpl = new SimpleNodeTOImpl(qName, parent, value); - return simpleNodeTOImpl; + return createImmutableSimpleNode(qName, parent, value, null); } /** * @param qName * @param parent * @param value + * @param modifyAction + * @param original originating node, if available * @return simple node modification, based on given qname, value and parent */ public static MutableSimpleNode createMutableSimpleNode(QName qName, - CompositeNode parent, T value) { + CompositeNode parent, Object value, ModifyAction modifyAction, SimpleNode original) { + @SuppressWarnings("unchecked") MutableSimpleNodeTOImpl simpleNodeTOImpl = - new MutableSimpleNodeTOImpl(qName, parent, value, null); + new MutableSimpleNodeTOImpl(qName, parent, (T) value, modifyAction); + simpleNodeTOImpl.setOriginal(original); return simpleNodeTOImpl; } - + /** * @param qName * @param parent * @param value * @return composite node modification, based on given qname, value (children), parent and modifyAction */ - public static CompositeNode createCompositeNode(QName qName, + public static CompositeNode createImmutableCompositeNode(QName qName, CompositeNode parent, List> value) { - CompositeNode compositeNodeTOImpl = new CompositeNodeTOImpl(qName, parent, value); - return compositeNodeTOImpl; + return createImmutableCompositeNode(qName, parent, value, null); } /** * @param qName * @param parent - * @param value - * @return composite node modification, based on given qname, value (children), parent and modifyAction + * @param valueArg + * @param modifyAction + * @param original originating node, if available + * @return composite node modification, based on given qName, value (children), parent and modifyAction */ public static MutableCompositeNode createMutableCompositeNode(QName qName, - CompositeNode parent, List> value) { + CompositeNode parent, List> valueArg, ModifyAction modifyAction, CompositeNode original) { + List> value = valueArg; + if (value == null) { + value = new ArrayList<>(); + } MutableCompositeNodeTOImpl compositeNodeTOImpl = - new MutableCompositeNodeTOImpl(qName, parent, value, null); + new MutableCompositeNodeTOImpl(qName, parent, value, modifyAction); + compositeNodeTOImpl.setOriginal(original); return compositeNodeTOImpl; } @@ -86,10 +97,10 @@ public abstract class NodeFactory { * @param modifyAction * @return simple node modification, based on given qname, value, parent and modifyAction */ - public static SimpleNodeModificationTOImpl createSimpleNodeModification(QName qName, + public static SimpleNode createImmutableSimpleNode(QName qName, CompositeNode parent, T value, ModifyAction modifyAction) { - SimpleNodeModificationTOImpl simpleNodeModTOImpl = - new SimpleNodeModificationTOImpl(qName, parent, value, modifyAction); + SimpleNodeTOImpl simpleNodeModTOImpl = + new SimpleNodeTOImpl(qName, parent, value, modifyAction); return simpleNodeModTOImpl; } @@ -100,10 +111,10 @@ public abstract class NodeFactory { * @param modifyAction * @return composite node modification, based on given qname, value (children), parent and modifyAction */ - public static CompositeNodeModificationTOImpl createCompositeNodeModification(QName qName, + public static CompositeNode createImmutableCompositeNode(QName qName, CompositeNode parent, List> value, ModifyAction modifyAction) { - CompositeNodeModificationTOImpl compositeNodeModTOImpl = - new CompositeNodeModificationTOImpl(qName, parent, value, modifyAction); + CompositeNodeTOImpl compositeNodeModTOImpl = + new CompositeNodeTOImpl(qName, parent, value, modifyAction); return compositeNodeModTOImpl; } @@ -113,7 +124,7 @@ public abstract class NodeFactory { * has no reference to this copy */ public static SimpleNode copyNode(SimpleNode node) { - SimpleNode twinNode = createSimpleNode( + SimpleNode twinNode = createImmutableSimpleNode( node.getNodeType(), node.getParent(), node.getValue()); return twinNode; } @@ -123,12 +134,13 @@ public abstract class NodeFactory { * @return copy of given node, parent and value are the same, but parent * has no reference to this copy */ - public static SimpleNode copyNodeAsMutable(SimpleNode node) { - SimpleNode twinNode = createMutableSimpleNode( - node.getNodeType(), node.getParent(), node.getValue()); + public static MutableSimpleNode copyNodeAsMutable(SimpleNode node) { + MutableSimpleNode twinNode = createMutableSimpleNode( + node.getNodeType(), node.getParent(), node.getValue(), + node.getModificationAction(), null); return twinNode; } - + /** * @param node * @param children @@ -136,8 +148,8 @@ public abstract class NodeFactory { * have no reference to this copy */ public static CompositeNode copyNode(CompositeNode node, Node... children) { - CompositeNode twinNode = createCompositeNode( - node.getNodeType(), node.getParent(), Arrays.asList(children)); + CompositeNode twinNode = createImmutableCompositeNode( + node.getNodeType(), node.getParent(), Arrays.asList(children), node.getModificationAction()); return twinNode; } @@ -152,53 +164,98 @@ public abstract class NodeFactory { /** * @param node root of original tree - * @param originalToMutable (optional) empty map, where binding between original and copy + * @param originalToCopyArg (optional) empty map, where binding between original and copy * will be stored - * @return copy of given node, parent and children are the same, but parent and children - * have no reference to this copy + * @return copy of given node and all subnodes recursively */ - public static MutableCompositeNode copyDeepNode(CompositeNode node, - Map, Node> originalToMutable) { - - MutableCompositeNode mutableRoot = - createMutableCompositeNode(node.getNodeType(), null, null); - Stack> jobQueue = new Stack<>(); - jobQueue.push(new SimpleEntry(node, mutableRoot)); - if (originalToMutable != null) { - originalToMutable.put(node, mutableRoot); - } - - while (!jobQueue.isEmpty()) { - SimpleEntry job = jobQueue.pop(); - CompositeNode originalNode = job.getKey(); - MutableCompositeNode mutableNode = job.getValue(); - mutableNode.setValue(new ArrayList>()); - - for (Node child : originalNode.getChildren()) { - Node mutableAscendant = null; - if (child instanceof CompositeNode) { - MutableCompositeNode newMutable = - createMutableCompositeNode(child.getNodeType(), mutableNode, null); - jobQueue.push(new SimpleEntry( - (CompositeNode) child, newMutable)); - mutableAscendant = newMutable; - } else if (child instanceof SimpleNode) { - mutableAscendant = - createMutableSimpleNode(child.getNodeType(), mutableNode, child.getValue()); - } else { - throw new IllegalStateException("Node class deep copy not supported: " - +child.getClass().getName()); - } - - mutableNode.getChildren().add(mutableAscendant); - if (originalToMutable != null) { - originalToMutable.put(child, mutableAscendant); - } - } - mutableNode.init(); - } + public static MutableCompositeNode copyDeepAsMutable(CompositeNode node, + Map, Node> originalToCopyArg) { + + Map, Node> originalToCopy = originalToCopyArg; + if (originalToCopy == null) { + originalToCopy = new HashMap<>(); + } + + MutableCompositeNode mutableRoot = createMutableCompositeNode(node.getNodeType(), null, null, + node.getModificationAction(), null); + Stack> jobQueue = new Stack<>(); + jobQueue.push(new SimpleEntry(node, mutableRoot)); + originalToCopy.put(node, mutableRoot); + + while (!jobQueue.isEmpty()) { + SimpleEntry job = jobQueue.pop(); + CompositeNode originalNode = job.getKey(); + MutableCompositeNode mutableNode = job.getValue(); + mutableNode.setValue(new ArrayList>()); + + for (Node child : originalNode.getChildren()) { + Node mutableAscendant = null; + if (child instanceof CompositeNode) { + MutableCompositeNode newMutable = + createMutableCompositeNode(child.getNodeType(), mutableNode, null, + ((NodeModification) child).getModificationAction(), null); + jobQueue.push(new SimpleEntry( + (CompositeNode) child, newMutable)); + mutableAscendant = newMutable; + } else if (child instanceof SimpleNode) { + mutableAscendant = + createMutableSimpleNode(child.getNodeType(), mutableNode, + child.getValue(), + ((NodeModification) child).getModificationAction(), null); + } else { + throw new IllegalStateException("Node class deep copy not supported: " + +child.getClass().getName()); + } + + mutableNode.getChildren().add(mutableAscendant); + originalToCopy.put(child, mutableAscendant); + } + mutableNode.init(); + } + + return mutableRoot; + } + + /** + * @param node root of original tree + * @param originalToCopyArg (optional) empty map, where binding between original and copy + * will be stored + * @return copy of given node and all subnodes recursively + */ + public static CompositeNode copyDeepAsImmutable(CompositeNode node, + Map, Node> originalToCopyArg) { + Stack jobQueue = new Stack<>(); + jobQueue.push(node); + + Map, Node> originalToCopy = originalToCopyArg; + if (originalToCopy == null) { + originalToCopy = new HashMap<>(); + } + + while (!jobQueue.isEmpty()) { + CompositeNode jobNode = jobQueue.peek(); + if (!originalToCopy.isEmpty() + && originalToCopy.keySet().containsAll(jobNode.getChildren())) { + jobQueue.pop(); + List> newChildren = NodeUtils.collectMapValues(jobNode.getChildren(), originalToCopy); + CompositeNode nodeCopy = createImmutableCompositeNode(jobNode.getNodeType(), null, + newChildren, jobNode.getModificationAction()); + NodeUtils.fixChildrenRelation(nodeCopy); + originalToCopy.put(jobNode, nodeCopy); + } else { + for (Node child : jobNode.getChildren()) { + if (child instanceof SimpleNode) { + originalToCopy.put(child, createImmutableSimpleNode( + child.getNodeType(), null, child.getValue(), + ((NodeModification) child).getModificationAction())); + } else if (child instanceof CompositeNode) { + jobQueue.push((CompositeNode) child); + } + } + } + } - return mutableRoot; + return (CompositeNode) originalToCopy.get(node); } }