From 39b426fb075e8690911802f70aceb865cc83715d Mon Sep 17 00:00:00 2001 From: Michal Rehak Date: Mon, 24 Jun 2013 08:31:00 +0200 Subject: [PATCH] groovy node-tree integration logging fixing groovy tree generating added params for surefire lazy node2node map fixing changed api according to dom usage added lazy implementation of dom added unit tests of this dom changed abstract implementation in yang-data-util according to new api improved groovy generated tree and testing removed large testing files added copyright comments removed maven-surefire-plugin jConsole argument (plugin fails to start, if property not defined) Change-Id: I00b9fcbc20ce5d120128940b7b569d7a14628a6e Signed-off-by: Michal Rehak --- .../yang/data/api/CompositeNode.java | 6 +- .../yang/data/api/MutableCompositeNode.java | 5 + .../yang/data/api/MutableSimpleNode.java | 5 +- .../controller/yang/data/api/SimpleNode.java | 6 +- .../yang/yang-data-impl/pom.xml | 18 +- .../yang/data/impl/AbstractNodeTO.java | 94 +++++- .../impl/CompositeNodeModificationTOImpl.java | 34 +-- .../yang/data/impl/CompositeNodeTOImpl.java | 41 ++- .../yang/data/impl/LazyNodeToNodeMap.java | 156 ++++++++++ .../data/impl/MutableCompositeNodeTOImpl.java | 24 +- .../data/impl/MutableSimpleNodeTOImpl.java | 22 +- .../yang/data/impl/NodeFactory.java | 197 ++++++++----- .../impl/NodeModificationBuilderImpl.java | 82 ++---- .../controller/yang/data/impl/NodeUtils.java | 73 ++++- .../impl/SimpleNodeModificationTOImpl.java | 33 +-- .../yang/data/impl/SimpleNodeTOImpl.java | 22 ++ .../impl/IgnoreWhiteCharsDiffListener.java | 44 +++ .../yang/data/impl/LazyNodeToNodeMapTest.java | 80 +++++ .../yang/data/impl/MemoryConsumption.java | 53 ++++ .../yang/data/impl/MyNodeBuilder.java | 190 ++++++++++++ .../yang/data/impl/NodeFactoryTest.java | 115 ++++---- .../controller/yang/data/impl/NodeHelper.java | 180 +++++++++--- .../impl/NodeModificationBuilderImplTest.java | 239 ++++++++++++++- .../yang/data/impl/NodeUtilsTest.java | 275 +++++++++++------- .../src/test/resources/MyXmlGenerator.groovy | 120 ++++++++ .../src/test/resources/config01.xml | 0 .../src/test/resources/config02.content | 73 ----- .../src/test/resources/config02.xml | 0 .../src/test/resources/config02g.xml | 73 ----- .../src/test/resources/controller-network.xsd | 0 .../test/resources/controller-network.yang | 0 .../src/test/resources/generateXml.groovy | 110 ------- .../resources/ietf-inet-types@2010-09-24.yang | 0 .../src/test/resources/log4j-test.xml | 24 ++ .../yang/data/impl/config02-shadow.xml | 73 +++++ .../controller/yang/data/impl/config02.groovy | 89 ++++++ .../yang/data/impl/config02g-shadow.xml | 72 +++++ .../yang/data/impl/mutableNodesConfig.xml | 13 + .../yang/data/util/AbstractNode.java | 11 + .../controller/yang/data/util/Nodes.java | 22 ++ 40 files changed, 1988 insertions(+), 686 deletions(-) mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/pom.xml mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/AbstractNodeTO.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeTOImpl.java create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/LazyNodeToNodeMap.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableCompositeNodeTOImpl.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableSimpleNodeTOImpl.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeFactory.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImpl.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeUtils.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeTOImpl.java create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/IgnoreWhiteCharsDiffListener.java create mode 100755 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/LazyNodeToNodeMapTest.java create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/MemoryConsumption.java create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/MyNodeBuilder.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeFactoryTest.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImplTest.java mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeUtilsTest.java create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/MyXmlGenerator.groovy mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config01.xml delete mode 100755 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02.content mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02.xml delete mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02g.xml mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/controller-network.xsd mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/controller-network.yang delete mode 100755 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/generateXml.groovy mode change 100755 => 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/ietf-inet-types@2010-09-24.yang create mode 100755 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/log4j-test.xml create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02-shadow.xml create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02.groovy create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02g-shadow.xml create mode 100644 opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/mutableNodesConfig.xml diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/CompositeNode.java b/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/CompositeNode.java index dfc790e0bd..e8ef406764 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/CompositeNode.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/CompositeNode.java @@ -29,7 +29,7 @@ import org.opendaylight.controller.yang.common.QName; * * */ -public interface CompositeNode extends Node>> { +public interface CompositeNode extends Node>>, NodeModification { List> getChildren(); @@ -45,4 +45,8 @@ public interface CompositeNode extends Node>> { SimpleNode getFirstSimpleByName(QName leaf); + /** + * @return cast self to mutable, if possible + */ + MutableCompositeNode asMutable(); } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/MutableCompositeNode.java b/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/MutableCompositeNode.java index b1baf309c5..4f11adbc17 100755 --- a/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/MutableCompositeNode.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/MutableCompositeNode.java @@ -20,4 +20,9 @@ public interface MutableCompositeNode extends MutableNode>>, Compos * update internal map */ public void init(); + + /** + * @return original node, if available + */ + CompositeNode getOriginal(); } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/MutableSimpleNode.java b/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/MutableSimpleNode.java index 9519fac792..ad25f8f380 100755 --- a/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/MutableSimpleNode.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/MutableSimpleNode.java @@ -15,6 +15,9 @@ package org.opendaylight.controller.yang.data.api; */ public interface MutableSimpleNode extends MutableNode, SimpleNode { - // nothing + /** + * @return original node, if available + */ + SimpleNode getOriginal(); } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/SimpleNode.java b/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/SimpleNode.java index 717c7224ee..3f75da5203 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/SimpleNode.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-api/src/main/java/org/opendaylight/controller/yang/data/api/SimpleNode.java @@ -21,6 +21,10 @@ package org.opendaylight.controller.yang.data.api; * * @param */ -public interface SimpleNode extends Node { +public interface SimpleNode extends Node, NodeModification { + /** + * @return cast self to mutable, if possible + */ + MutableSimpleNode asMutable(); } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/pom.xml b/opendaylight/sal/yang-prototype/yang/yang-data-impl/pom.xml old mode 100755 new mode 100644 index 41fd1139ae..7a1b756332 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/pom.xml +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/pom.xml @@ -10,16 +10,15 @@ yang-data-impl - 2.1.5 + 2.1.6 - org.apache.maven.plugins maven-surefire-plugin - -Dlog4j.configuration=log4j-test.xml + -Dlog4j.configuration=log4j-test.xml -Xmx1500m true @@ -39,12 +38,19 @@ org.opendaylight.controller yang-model-parser-impl + + + org.slf4j + slf4j-simple + + com.google.guava guava 14.0.1 + junit junit @@ -68,5 +74,11 @@ ${groovy.version} test + + xmlunit + xmlunit + 1.4 + test + diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/AbstractNodeTO.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/AbstractNodeTO.java old mode 100755 new mode 100644 index cc4dfee5a5..3385700c46 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/AbstractNodeTO.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/AbstractNodeTO.java @@ -9,7 +9,9 @@ package org.opendaylight.controller.yang.data.impl; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.opendaylight.controller.yang.data.api.ModifyAction; import org.opendaylight.controller.yang.data.api.Node; +import org.opendaylight.controller.yang.data.api.NodeModification; /** * @author michal.rehak @@ -17,12 +19,13 @@ import org.opendaylight.controller.yang.data.api.Node; * type of node value * */ -public abstract class AbstractNodeTO implements Node { +public abstract class AbstractNodeTO implements Node, NodeModification { private QName qName; private CompositeNode parent; private T value; - + private ModifyAction modifyAction; + /** * @param qname * @param parent @@ -33,6 +36,19 @@ public abstract class AbstractNodeTO implements Node { this.parent = parent; this.value = value; } + + /** + * @param qname + * @param parent + * @param value + * @param modifyAction + */ + public AbstractNodeTO(QName qname, CompositeNode parent, T value, ModifyAction modifyAction) { + this.qName = qname; + this.parent = parent; + this.value = value; + this.modifyAction = modifyAction; + } @Override public QName getNodeType() { @@ -42,7 +58,7 @@ public abstract class AbstractNodeTO implements Node { /** * @return the qName */ - protected QName getQName() { + public QName getQName() { return qName; } @@ -69,4 +85,76 @@ public abstract class AbstractNodeTO implements Node { public T getValue() { return value; } + + /** + * @return modification action + * @see org.opendaylight.controller.yang.data.impl.NodeModificationSupport#getModificationAction() + */ + @Override + public ModifyAction getModificationAction() { + return modifyAction; + } + + /** + * @param modifyAction + * the modifyAction to set + */ + protected void setModificationAction(ModifyAction modifyAction) { + this.modifyAction = modifyAction; + } + + @Override + public String toString() { + StringBuffer out = new StringBuffer(); + out.append(String.format("Node[%s], qName[%s], modify[%s]", + getClass().getSimpleName(), getQName().getLocalName(), + getModificationAction() == null ? "n/a" : getModificationAction())); + return out.toString(); + } + + /* */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((modifyAction == null) ? 0 : modifyAction.hashCode()); +// result = prime * result + ((parent == null) ? 0 : parent.hashCode()); + result = prime * result + ((qName == null) ? 0 : qName.hashCode()); + result = prime * result + ((value == null) ? 0 : value.hashCode()); + return result % 2; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + @SuppressWarnings("unchecked") + AbstractNodeTO other = (AbstractNodeTO) obj; + if (modifyAction != other.modifyAction) + return false; + if (parent == null) { + if (other.parent != null) + return false; + } else if (other.parent == null) { + return false; + } + if (qName == null) { + if (other.qName != null) + return false; + } else if (!qName.equals(other.qName)) + return false; + if (value == null) { + if (other.value != null) + return false; + } else if (!value.equals(other.value)) + return false; + return true; + } + /* */ + } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeModificationTOImpl.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeModificationTOImpl.java index 7981df2f20..44c8c852f5 100755 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeModificationTOImpl.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeModificationTOImpl.java @@ -1,5 +1,9 @@ -/** - * +/* + * Copyright (c) 2013 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.controller.yang.data.impl; @@ -9,16 +13,12 @@ import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; import org.opendaylight.controller.yang.data.api.ModifyAction; import org.opendaylight.controller.yang.data.api.Node; -import org.opendaylight.controller.yang.data.api.NodeModification; /** * @author michal.rehak * */ -public class CompositeNodeModificationTOImpl extends CompositeNodeTOImpl - implements NodeModification { - - private ModifyAction modifyAction; +public class CompositeNodeModificationTOImpl extends CompositeNodeTOImpl { /** * @param qname @@ -29,24 +29,6 @@ public class CompositeNodeModificationTOImpl extends CompositeNodeTOImpl public CompositeNodeModificationTOImpl(QName qname, CompositeNode parent, List> value, ModifyAction modifyAction) { super(qname, parent, value); - this.modifyAction = modifyAction; - } - - /** - * @return modification action - * @see org.opendaylight.controller.yang.data.impl.NodeModificationSupport#getModificationAction() - */ - @Override - public ModifyAction getModificationAction() { - return modifyAction; + super.setModificationAction(modifyAction); } - - /** - * @param modifyAction - * the modifyAction to set - */ - protected void setModificationAction(ModifyAction modifyAction) { - this.modifyAction = modifyAction; - } - } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeTOImpl.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeTOImpl.java old mode 100755 new mode 100644 index f94163aa89..605b99c296 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeTOImpl.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/CompositeNodeTOImpl.java @@ -13,6 +13,8 @@ import java.util.Map; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.opendaylight.controller.yang.data.api.ModifyAction; +import org.opendaylight.controller.yang.data.api.MutableCompositeNode; import org.opendaylight.controller.yang.data.api.Node; import org.opendaylight.controller.yang.data.api.SimpleNode; @@ -36,6 +38,19 @@ public class CompositeNodeTOImpl extends AbstractNodeTO>> if (value != null) { nodeMap = NodeUtils.buildNodeMap(getValue()); } + init(); + } + + /** + * @param qname + * @param parent use null to create top composite node (without parent) + * @param value + * @param modifyAction + */ + public CompositeNodeTOImpl(QName qname, CompositeNode parent, + List> value, ModifyAction modifyAction) { + super(qname, parent, value, modifyAction); + init(); } @@ -105,16 +120,34 @@ public class CompositeNodeTOImpl extends AbstractNodeTO>> @Override public List getCompositesByName(String children) { - return getCompositesByName(localQName(children)); + return getCompositesByName(new QName(getNodeType(), children)); } @Override public List> getSimpleNodesByName(String children) { - return getSimpleNodesByName(localQName(children)); + return getSimpleNodesByName(new QName(getNodeType(), children)); } - private QName localQName(String str) { - return new QName(getNodeType(), str); + /** + * @param value + */ + protected void init() { + if (getValue() != null) { + nodeMap = NodeUtils.buildNodeMap(getValue()); + } + } + + @Override + public MutableCompositeNode asMutable() { + throw new IllegalAccessError("cast to mutable is not supported - "+getClass().getSimpleName()); } + + @Override + public String toString() { + return super.toString() + ", children.size = " + + (getChildren() != null ? getChildren().size() : "n/a"); + } + + } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/LazyNodeToNodeMap.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/LazyNodeToNodeMap.java new file mode 100644 index 0000000000..81dd86e7e8 --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/LazyNodeToNodeMap.java @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2013 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.controller.yang.data.impl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.Stack; + +import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.opendaylight.controller.yang.data.api.MutableCompositeNode; +import org.opendaylight.controller.yang.data.api.MutableNode; +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; + +/** + * @author michal.rehak + * + */ +public class LazyNodeToNodeMap { + + private Map, Node> node2node = new HashMap<>(); + private CompositeNode originalRoot; + private MutableCompositeNode mutableRoot; + + /** + * @param originalNode + * @return mutable twin + */ + public Node getMutableEquivalent(Node originalNode) { + Node mutableNode = node2node.get(originalNode); + if (mutableNode == null) { + addPathMembers(originalNode); + mutableNode = node2node.get(originalNode); + } + + return mutableNode; + } + + /** + * @param originalNode + */ + private void addPathMembers(Node originalNode) { + Stack> jobQueue = new Stack<>(); + jobQueue.push(originalNode); + while (!jobQueue.isEmpty()) { + Node node2add = jobQueue.pop(); + boolean fixChildrenRefOnly = false; + if (node2node.containsKey(node2add)) { + if (node2add instanceof SimpleNode) { + continue; + } + fixChildrenRefOnly = true; + } + + CompositeNode nextParent = node2add.getParent(); + MutableNode mutableEquivalent = null; + + if (node2add instanceof SimpleNode) { + SimpleNode node2addSimple = (SimpleNode) node2add; + MutableSimpleNode nodeMutant = NodeFactory.createMutableSimpleNode( + node2add.getNodeType(), null, node2addSimple.getValue(), + node2addSimple.getModificationAction(), node2addSimple); + mutableEquivalent = nodeMutant; + } else if (node2add instanceof CompositeNode) { + MutableCompositeNode nodeMutant = null; + if (fixChildrenRefOnly) { + nodeMutant = (MutableCompositeNode) node2node.get(node2add); + } else { + CompositeNode node2addComposite = (CompositeNode) node2add; + nodeMutant = NodeFactory.createMutableCompositeNode(node2add.getNodeType(), + null, null, + ((NodeModification) node2add).getModificationAction(), node2addComposite); + } + + mutableEquivalent = nodeMutant; + + // tidy up children + if (nodeMutant.getChildren() == null) { + nodeMutant.setValue(new ArrayList>()); + } + for (Node originalChildNode : ((CompositeNode) node2add).getChildren()) { + MutableNode mutableChild = (MutableNode) node2node.get(originalChildNode); + fixChildrenRef(nodeMutant, mutableChild); + } + + if (nodeMutant.getChildren() != null && !nodeMutant.getChildren().isEmpty()) { + nodeMutant.init(); + } + + // store tree root, if occured + if (nextParent == null) { + if (originalRoot == null) { + originalRoot = (CompositeNode) node2add; + mutableRoot = nodeMutant; + } else { + if (!originalRoot.equals(node2add)) { + throw new IllegalStateException("Different tree root node obtained - " + + "perhaps nodes of different trees are getting mixed up."); + } + } + } + } + + // feed jobQueue + node2node.put(node2add, mutableEquivalent); + if (nextParent != null) { + jobQueue.push(nextParent); + } + } + } + + /** + * @param nodeMutant + * @param mutableChild + */ + private static void fixChildrenRef(MutableCompositeNode nodeMutant, + MutableNode mutableChild) { + if (mutableChild != null) { + if (!nodeMutant.getChildren().contains(mutableChild)) { + nodeMutant.getChildren().add(mutableChild); + } + CompositeNode parentOfChild = mutableChild.getParent(); + if (parentOfChild == null) { + mutableChild.setParent(nodeMutant); + } else { + if (!parentOfChild.equals(nodeMutant)) { + throw new IllegalStateException("Different parent node obtained - " + + "perhaps nodes of different trees are getting mixed up."); + } + } + } + } + + /** + * @return the mutableRoot + */ + public MutableCompositeNode getMutableRoot() { + return mutableRoot; + } + + /** + * @return set of original nodes, registered in map as keys + */ + public Set> getKeyNodes() { + return node2node.keySet(); + } +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableCompositeNodeTOImpl.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableCompositeNodeTOImpl.java old mode 100755 new mode 100644 index 997b5025a7..c29ecc4a0c --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableCompositeNodeTOImpl.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableCompositeNodeTOImpl.java @@ -20,10 +20,11 @@ import org.opendaylight.controller.yang.data.api.Node; * @author michal.rehak * */ -public class MutableCompositeNodeTOImpl extends CompositeNodeModificationTOImpl +public class MutableCompositeNodeTOImpl extends CompositeNodeTOImpl implements MutableCompositeNode { private Map>> nodeMap; + private CompositeNode original; /** * @param qname @@ -41,7 +42,9 @@ public class MutableCompositeNodeTOImpl extends CompositeNodeModificationTOImpl */ @Override public void init() { - nodeMap = NodeUtils.buildNodeMap(getChildren()); + if (!getChildren().isEmpty()) { + nodeMap = NodeUtils.buildNodeMap(getChildren()); + } } @Override @@ -58,4 +61,21 @@ public class MutableCompositeNodeTOImpl extends CompositeNodeModificationTOImpl protected Map>> getNodeMap() { return nodeMap; } + + @Override + public MutableCompositeNode asMutable() { + return this; + } + + @Override + public CompositeNode getOriginal() { + return original; + } + + /** + * @param original the original to set + */ + public void setOriginal(CompositeNode original) { + this.original = original; + } } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableSimpleNodeTOImpl.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableSimpleNodeTOImpl.java old mode 100755 new mode 100644 index 17cbb8dcad..a3b73b1c64 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableSimpleNodeTOImpl.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/MutableSimpleNodeTOImpl.java @@ -11,15 +11,18 @@ import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; import org.opendaylight.controller.yang.data.api.ModifyAction; import org.opendaylight.controller.yang.data.api.MutableSimpleNode; +import org.opendaylight.controller.yang.data.api.SimpleNode; /** * @author michal.rehak * @param type of simple node value * */ -public class MutableSimpleNodeTOImpl extends SimpleNodeModificationTOImpl +public class MutableSimpleNodeTOImpl extends SimpleNodeTOImpl implements MutableSimpleNode { + private SimpleNode original; + /** * @param qname * @param parent @@ -40,4 +43,21 @@ public class MutableSimpleNodeTOImpl extends SimpleNodeModificationTOImpl public void setModifyAction(ModifyAction action) { super.setModificationAction(action); } + + @Override + public MutableSimpleNode asMutable() { + return this; + } + + @Override + public SimpleNode getOriginal() { + return original; + } + + /** + * @param original the original to set + */ + public void setOriginal(SimpleNode original) { + this.original = original; + } } 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); } } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImpl.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImpl.java old mode 100755 new mode 100644 index cd717316d3..2ab2dd84b4 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImpl.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImpl.java @@ -1,14 +1,16 @@ -/** - * +/* + * Copyright (c) 2013 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.controller.yang.data.impl; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.Stack; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; @@ -30,9 +32,7 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder { private SchemaContext context; private Set> changeLog; - private Map, Node> originalToMutable; - - private MutableCompositeNode mutableRoot; + private LazyNodeToNodeMap originalToMutable; /** * @param originalTreeRootNode @@ -40,24 +40,10 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder { */ public NodeModificationBuilderImpl(CompositeNode originalTreeRootNode, SchemaContext context) { this.context = context; - originalToMutable = new HashMap<>(); - mutableRoot = NodeFactory.copyDeepNode(originalTreeRootNode, originalToMutable); + originalToMutable = new LazyNodeToNodeMap(); changeLog = new HashSet<>(); } - /** - * add given node to it's parent's list of children - * @param newNode - */ - private static void fixParentRelation(Node newNode) { - if (newNode.getParent() != null) { - List> siblings = newNode.getParent().getChildren(); - if (!siblings.contains(newNode)) { - siblings.add(newNode); - } - } - } - /** * @param modNode * @param action @@ -69,13 +55,13 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder { @Override public void addNode(MutableSimpleNode newNode) { - fixParentRelation(newNode); + NodeUtils.fixParentRelation(newNode); addModificationToLog(newNode, ModifyAction.CREATE); } @Override public void addNode(MutableCompositeNode newNode) { - fixParentRelation(newNode); + NodeUtils.fixParentRelation(newNode); addModificationToLog(newNode, ModifyAction.CREATE); } @@ -126,9 +112,8 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder { wanted.addAll(collectSelfAndAllParents(mutant)); } - // TODO:: walk wanted and add relevant keys + // walk wanted and add relevant keys Map mapOfLists = NodeUtils.buildMapOfListNodes(context); - Set> wantedKeys = new HashSet<>(); for (Node outlaw : wanted) { if (outlaw instanceof CompositeNode) { String path = NodeUtils.buildPath(outlaw); @@ -137,54 +122,27 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder { if (listSchema.getQName().equals(outlaw.getNodeType())) { // try to add key subnode to wanted list List supportedKeys = listSchema.getKeyDefinition(); - for (Node outlawChildren : ((CompositeNode) outlaw).getChildren()) { - if (supportedKeys.contains(outlawChildren.getNodeType())) { - wantedKeys.add(outlawChildren); + CompositeNode outlawOriginal = ((MutableCompositeNode) outlaw).getOriginal(); + for (Node outlawOriginalChild : outlawOriginal.getChildren()) { + if (supportedKeys.contains(outlawOriginalChild.getNodeType())) { + originalToMutable.getMutableEquivalent(outlawOriginalChild); } } } } } } - wanted.addAll(wantedKeys); - - // remove all unwanted nodes from tree - removeUnrelevantNodes(mutableRoot, wanted); - return mutableRoot; - } - - /** - * @param mutableRoot2 - * @param wanted - */ - private static void removeUnrelevantNodes(MutableCompositeNode mutRoot, - Set> wanted) { - Stack> jobQueue = new Stack<>(); - jobQueue.push(mutRoot); - while (!jobQueue.isEmpty()) { - MutableNode mutNode = jobQueue.pop(); - if (!wanted.contains(mutNode)) { - if (mutNode.getParent() != null) { - mutNode.getParent().getChildren().remove(mutNode); - } - } else { - if (mutNode instanceof MutableCompositeNode) { - for (Node mutChild : ((MutableCompositeNode) mutNode).getChildren()) { - jobQueue.push((MutableNode) mutChild); - } - } - } - } + return originalToMutable.getMutableRoot(); } /** - * @param focusedAncestor + * @param focusedDescendant * @return set of parents and focusedAncestor itself */ - private static Set> collectSelfAndAllParents(Node focusedAncestor) { + private static Set> collectSelfAndAllParents(Node focusedDescendant) { Set> family = new HashSet<>(); - Node tmpNode = focusedAncestor; + Node tmpNode = focusedDescendant; while (tmpNode != null) { family.add(tmpNode); tmpNode = tmpNode.getParent(); @@ -198,7 +156,7 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder { */ @Override public Node getMutableEquivalent(Node originalNode) { - return originalToMutable.get(originalNode); + return originalToMutable.getMutableEquivalent(originalNode); } } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeUtils.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeUtils.java old mode 100755 new mode 100644 index b3a7640775..513cf06a68 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeUtils.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/NodeUtils.java @@ -36,10 +36,10 @@ import org.opendaylight.controller.yang.model.api.ListSchemaNode; import org.opendaylight.controller.yang.model.api.SchemaContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; import org.w3c.dom.Element; import com.google.common.base.Joiner; +import com.google.common.collect.Lists; /** @@ -48,11 +48,12 @@ import com.google.common.base.Joiner; */ public abstract class NodeUtils { + private static final Logger LOG = LoggerFactory.getLogger(NodeUtils.class); + /** * */ private static final String USER_KEY_NODE = "node"; - private static final Logger LOG = LoggerFactory.getLogger(NodeUtils.class); /** * @param node @@ -75,9 +76,9 @@ public abstract class NodeUtils { * @return dom tree, containing same node structure, yang nodes are associated * to dom nodes as user data */ - public static Document buildShadowDomTree(CompositeNode treeRootNode) { + public static org.w3c.dom.Document buildShadowDomTree(CompositeNode treeRootNode) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - Document doc = null; + org.w3c.dom.Document doc = null; try { DocumentBuilder bob = dbf.newDocumentBuilder(); doc = bob.newDocument(); @@ -86,6 +87,7 @@ public abstract class NodeUtils { return null; } + Stack>> jobQueue = new Stack<>(); jobQueue.push(new SimpleEntry>(doc, treeRootNode)); @@ -93,12 +95,14 @@ public abstract class NodeUtils { SimpleEntry> job = jobQueue.pop(); org.w3c.dom.Node jointPlace = job.getKey(); Node item = job.getValue(); - Element itemEl = doc.createElement(item.getNodeType().getLocalName()); + QName nodeType = item.getNodeType(); + Element itemEl = doc.createElementNS(nodeType.getNamespace().toString(), + item.getNodeType().getLocalName()); itemEl.setUserData(USER_KEY_NODE, item, null); if (item instanceof SimpleNode) { Object value = ((SimpleNode) item).getValue(); itemEl.setTextContent(String.valueOf(value)); - itemEl.setAttribute("type", value.getClass().getSimpleName()); + //itemEl.setAttribute("type", value.getClass().getSimpleName()); } if (item instanceof NodeModification) { ModifyAction modificationAction = ((NodeModification) item).getModificationAction(); @@ -126,7 +130,7 @@ public abstract class NodeUtils { * @throws XPathExpressionException */ @SuppressWarnings("unchecked") - public static T findNodeByXpath(Document doc, String xpathEx) + public static T findNodeByXpath(org.w3c.dom.Document doc, String xpathEx) throws XPathExpressionException { T userNode = null; XPathFactory xPathfactory = XPathFactory.newInstance(); @@ -190,8 +194,7 @@ public abstract class NodeUtils { return mapOfLists; } - - + /** * @param path * @return @@ -203,4 +206,56 @@ public abstract class NodeUtils { } return Joiner.on(".").join(pathSeed); } + + /** + * add given node to it's parent's list of children + * @param newNode + */ + public static void fixParentRelation(Node newNode) { + if (newNode.getParent() != null) { + List> siblings = newNode.getParent().getChildren(); + if (!siblings.contains(newNode)) { + siblings.add(newNode); + } + } + } + + /** + * crawl all children of given node and assign it as their parent + * @param parentNode + */ + public static void fixChildrenRelation(CompositeNode parentNode) { + if (parentNode.getChildren() != null) { + for (Node child : parentNode.getChildren()) { + if (child instanceof AbstractNodeTO) { + ((AbstractNodeTO) child).setParent(parentNode); + } + } + } + } + + + /** + * @param keys + * @param dataMap + * @return list of values of map, found by given keys + */ + public static List collectMapValues(List keys, + Map dataMap) { + List valueSubList = new ArrayList<>(); + for (T key : keys) { + valueSubList.add(dataMap.get(key)); + } + + return valueSubList; + } + + /** + * @param nodes + * @return list of children in list of appropriate type + */ + public static List> buildChildrenList(Node...nodes) { + return Lists.newArrayList(nodes); + } + } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeModificationTOImpl.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeModificationTOImpl.java index 7268637377..eaee3730a7 100755 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeModificationTOImpl.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeModificationTOImpl.java @@ -1,22 +1,22 @@ -/** - * +/* + * Copyright (c) 2013 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.controller.yang.data.impl; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; import org.opendaylight.controller.yang.data.api.ModifyAction; -import org.opendaylight.controller.yang.data.api.NodeModification; /** * @author michal.rehak * @param type of node value * */ -public class SimpleNodeModificationTOImpl extends SimpleNodeTOImpl - implements NodeModification { - - private ModifyAction modifyAction; +public class SimpleNodeModificationTOImpl extends SimpleNodeTOImpl { /** * @param qname @@ -27,23 +27,6 @@ public class SimpleNodeModificationTOImpl extends SimpleNodeTOImpl public SimpleNodeModificationTOImpl(QName qname, CompositeNode parent, T value, ModifyAction modifyAction) { super(qname, parent, value); - this.modifyAction = modifyAction; - } - - /** - * @return modification action - * @see org.opendaylight.controller.yang.data.impl.NodeModificationSupport#getModificationAction() - */ - @Override - public ModifyAction getModificationAction() { - return modifyAction; - } - - /** - * @param modifyAction - * the modifyAction to set - */ - protected void setModificationAction(ModifyAction modifyAction) { - this.modifyAction = modifyAction; + setModificationAction(modifyAction); } } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeTOImpl.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeTOImpl.java old mode 100755 new mode 100644 index 5cfc03aa7b..466cea5303 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeTOImpl.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/main/java/org/opendaylight/controller/yang/data/impl/SimpleNodeTOImpl.java @@ -9,6 +9,8 @@ package org.opendaylight.controller.yang.data.impl; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.opendaylight.controller.yang.data.api.ModifyAction; +import org.opendaylight.controller.yang.data.api.MutableSimpleNode; import org.opendaylight.controller.yang.data.api.SimpleNode; /** @@ -27,5 +29,25 @@ public class SimpleNodeTOImpl extends AbstractNodeTO implements public SimpleNodeTOImpl(QName qname, CompositeNode parent, T value) { super(qname, parent, value); } + + /** + * @param qname + * @param parent + * @param value + * @param modifyAction + */ + public SimpleNodeTOImpl(QName qname, CompositeNode parent, T value, ModifyAction modifyAction) { + super(qname, parent, value, modifyAction); + } + + @Override + public MutableSimpleNode asMutable() { + throw new IllegalAccessError("cast to mutable is not supported - "+getClass().getSimpleName()); + } + + @Override + public String toString() { + return super.toString() + ", value = "+getValue(); + } } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/IgnoreWhiteCharsDiffListener.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/IgnoreWhiteCharsDiffListener.java new file mode 100644 index 0000000000..bf6b292c23 --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/IgnoreWhiteCharsDiffListener.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2013 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.controller.yang.data.impl; + +import org.custommonkey.xmlunit.Difference; +import org.custommonkey.xmlunit.DifferenceConstants; +import org.custommonkey.xmlunit.DifferenceListener; + +/** + * Implementatin of {@link DifferenceListener} ignoring white characters around text elements + * @author mirehak + * + */ +public class IgnoreWhiteCharsDiffListener implements DifferenceListener { + + @Override + public void skippedComparison(org.w3c.dom.Node control, + org.w3c.dom.Node test) { + // do nothing + } + + @Override + public int differenceFound(Difference diff) { + + if (diff.getId() == DifferenceConstants.TEXT_VALUE.getId()) { + + String control = diff.getControlNodeDetail().getValue(); + if (control != null) { + control = control.trim(); + if (diff.getTestNodeDetail().getValue() != null + && control.equals(diff.getTestNodeDetail().getValue().trim())) { + return + DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; + } + } + } + return RETURN_ACCEPT_DIFFERENCE; + } +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/LazyNodeToNodeMapTest.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/LazyNodeToNodeMapTest.java new file mode 100755 index 0000000000..c4dc672c12 --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/LazyNodeToNodeMapTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2013 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.controller.yang.data.impl; + +import java.net.URI; +import java.util.Date; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.opendaylight.controller.yang.data.api.MutableCompositeNode; +import org.opendaylight.controller.yang.data.api.Node; + +/** + * @author michal.rehak + * + */ +public class LazyNodeToNodeMapTest { + + private LazyNodeToNodeMap lazyN2N; + private CompositeNode tree; + + /** + * prepare test values + * @throws Exception + */ + @Before + public void setUp() throws Exception { + lazyN2N = new LazyNodeToNodeMap(); + + QName qName = new QName( + new URI("urn:ietf:params:xml:ns:netconf:base:1.0"), + new Date(42), "yang-data-impl-mutableTest"); + + tree = NodeHelper.buildTestConfigTree(qName); + } + + /** + * Test method for {@link org.opendaylight.controller.yang.data.impl.LazyNodeToNodeMap#getMutableEquivalent(org.opendaylight.controller.yang.data.api.Node)}. + */ + @Test + public void testGetMutableEquivalent() { + MutableCompositeNode mutableTree = (MutableCompositeNode) lazyN2N.getMutableEquivalent(tree); + + Assert.assertNull(mutableTree.getParent()); + Assert.assertEquals(tree.getNodeType(), mutableTree.getNodeType()); + Assert.assertEquals(1, lazyN2N.getKeyNodes().size()); + + Node subNode = tree.getCompositesByName("topologies").iterator().next(); + Node subMutant = lazyN2N.getMutableEquivalent(subNode); + + Assert.assertNotNull(subMutant.getParent()); + Assert.assertEquals(subNode.getNodeType(), subMutant.getNodeType()); + Assert.assertEquals(2, lazyN2N.getKeyNodes().size()); + + Assert.assertEquals(mutableTree, subMutant.getParent()); + Assert.assertEquals(mutableTree.getChildren().size(), 1); + Assert.assertEquals(mutableTree.getChildren().iterator().next(), subMutant); + } + + /** + * Test method for {@link org.opendaylight.controller.yang.data.impl.LazyNodeToNodeMap#getMutableRoot()}. + */ + @Test + public void testGetMutableRoot() { + Node subNode = tree.getCompositesByName("topologies").iterator().next(); + Node subMutant = lazyN2N.getMutableEquivalent(subNode); + + Assert.assertNotNull(subMutant.getParent()); + Assert.assertEquals(subMutant.getParent(), lazyN2N.getMutableRoot()); + } + +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/MemoryConsumption.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/MemoryConsumption.java new file mode 100644 index 0000000000..e53307d25f --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/MemoryConsumption.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2013 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.controller.yang.data.impl; + +/** + * Provides memory consumption and elapsed time between 2 points + * @author mirehak + */ +public class MemoryConsumption { + + private long memBegin; + private long tsBegin; + + /** + * record memory and timestamp + */ + public void startObserving() { + Runtime runtime = Runtime.getRuntime(); + // Run the garbage collector + runtime.gc(); + memBegin = getActualMemoryConsumption(); + tsBegin = System.currentTimeMillis(); + } + + + /** + * @return memory usage and time elapsed message + */ + public String finishObserving() { + long memEnd = getActualMemoryConsumption(); + long tsEnd = System.currentTimeMillis(); + return String.format("Used memory: %10d B; Elapsed time: %5d ms", (memEnd - memBegin), (tsEnd - tsBegin)); + } + + + /** + * @return actual memory usage + */ + public static long getActualMemoryConsumption() { + Runtime runtime = Runtime.getRuntime(); + // Calculate the used memory + long memory = runtime.totalMemory() - runtime.freeMemory(); + return memory; + } + + +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/MyNodeBuilder.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/MyNodeBuilder.java new file mode 100644 index 0000000000..26cee74514 --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/MyNodeBuilder.java @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2013 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.controller.yang.data.impl; + +import groovy.util.BuilderSupport; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Date; +import java.util.Map; +import java.util.Map.Entry; + +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.opendaylight.controller.yang.data.api.ModifyAction; +import org.opendaylight.controller.yang.data.api.MutableCompositeNode; +import org.opendaylight.controller.yang.data.api.Node; +import org.opendaylight.controller.yang.data.api.SimpleNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author michal.rehak + * + */ +public class MyNodeBuilder extends BuilderSupport { + + private static final Logger LOG = LoggerFactory + .getLogger(MyNodeBuilder.class); + + private URI qnNamespace; + private String qnPrefix; + private Date qnRevision; + + private CompositeNode rootNode; + + /** + * @param baseQName + */ + private MyNodeBuilder(QName baseQName) { + qnNamespace = baseQName.getNamespace(); + qnPrefix = baseQName.getPrefix(); + qnRevision = baseQName.getRevision(); + } + + /** + * @return initialized singleton instance + */ + public static MyNodeBuilder newInstance() { + QName qName = null; + try { + qName = new QName( + new URI("urn:opendaylight:controller:network"), + new Date(42), "yang-data-impl-groovyTest_", null); + } catch (URISyntaxException e) { + LOG.error(e.getMessage(), e); + } + return new MyNodeBuilder(qName); + } + + @Override + protected void setParent(Object parent, Object child) { + // do nothing + if (child instanceof AbstractNodeTO) { + ((AbstractNodeTO) child).setParent((CompositeNode) parent); + } else { + LOG.error("PARENTING FAILED: "+parent + " -> " + child); + } + } + + @Override + protected Object createNode(Object name) { + MutableCompositeNode newNode = NodeFactory.createMutableCompositeNode( + createQName(name), getCurrentNode(), null, null, null); + NodeUtils.fixParentRelation(newNode); + return newNode; + } + + @Override + protected Object createNode(Object name, @SuppressWarnings("rawtypes") Map attributes) { + ModifyAction modifyAction = processAttributes(attributes); + MutableCompositeNode newNode = NodeFactory.createMutableCompositeNode( + createQName(name), getCurrentNode(), null, modifyAction, null); + NodeUtils.fixParentRelation(newNode); + return newNode; + } + + + @Override + protected Object createNode(Object name, @SuppressWarnings("rawtypes") Map attributes, Object value) { + ModifyAction modifyAction = processAttributes(attributes); + SimpleNode newNode = NodeFactory.createImmutableSimpleNode( + createQName(name), (CompositeNode) getCurrent(), value, modifyAction); + NodeUtils.fixParentRelation(newNode); + return newNode; + } + + /** + * @param attributes + * @return + */ + private ModifyAction processAttributes(@SuppressWarnings("rawtypes") Map attributes) { + LOG.debug("attributes:" + attributes); + ModifyAction modAction = null; + + @SuppressWarnings("unchecked") + Map attributesSane = attributes; + for (Entry attr : attributesSane.entrySet()) { + switch (attr.getKey()) { + case "xmlns": + try { + qnNamespace = new URI(attr.getValue()); + } catch (URISyntaxException e) { + LOG.error(e.getMessage(), e); + } + break; + case "modifyAction": + modAction = ModifyAction.valueOf(attr.getValue()); + break; + + default: + throw new IllegalArgumentException("Attribute not supported: "+attr.getKey()); + } + } + return modAction; + } + + @Override + protected Object createNode(Object name, Object value) { + SimpleNode newNode = NodeFactory.createImmutableSimpleNode(createQName(name), (CompositeNode) getCurrent(), value); + NodeUtils.fixParentRelation(newNode); + return newNode; + } + + private QName createQName(Object localName) { + LOG.debug("qname for: "+localName); + return new QName(qnNamespace, qnRevision, qnPrefix, (String) localName); + } + + protected CompositeNode getCurrentNode() { + if (getCurrent() != null) { + if (getCurrent() instanceof CompositeNode) { + return (CompositeNode) getCurrent(); + + } else { + throw new IllegalAccessError("current node is not of type CompositeNode, but: " + +getCurrent().getClass().getSimpleName()); + } + } + + return null; + } + + @Override + protected Object postNodeCompletion(Object parent, Object node) { + Node nodeRevisited = (Node) node; + LOG.debug("postNodeCompletion at: \n "+ nodeRevisited+"\n "+parent); + if (nodeRevisited instanceof MutableCompositeNode) { + MutableCompositeNode mutant = (MutableCompositeNode) nodeRevisited; + if (mutant.getValue().isEmpty()) { + LOG.error("why is it having empty value? -- " + mutant); + } + nodeRevisited = NodeFactory.createImmutableCompositeNode( + mutant.getNodeType(), mutant.getParent(), mutant.getValue(), mutant.getModificationAction()); + NodeUtils.fixChildrenRelation((CompositeNode) nodeRevisited); + + if (parent == null) { + rootNode = (CompositeNode) nodeRevisited; + } else { + NodeUtils.fixParentRelation(nodeRevisited); + nodeRevisited.getParent().getChildren().remove(mutant); + } + } + + + return nodeRevisited; + } + + /** + * @return tree root + */ + public CompositeNode getRootNode() { + return rootNode; + } +} \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeFactoryTest.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeFactoryTest.java old mode 100755 new mode 100644 index 3d97cc83ca..cafbfa391d --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeFactoryTest.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeFactoryTest.java @@ -7,8 +7,6 @@ */ package org.opendaylight.controller.yang.data.impl; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; import java.net.URI; import java.util.ArrayList; import java.util.Date; @@ -17,6 +15,7 @@ import java.util.List; import java.util.Map; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; @@ -24,6 +23,7 @@ import org.opendaylight.controller.yang.data.api.ModifyAction; import org.opendaylight.controller.yang.data.api.MutableCompositeNode; import org.opendaylight.controller.yang.data.api.Node; import org.opendaylight.controller.yang.data.api.NodeModification; +import org.opendaylight.controller.yang.data.api.SimpleNode; import org.w3c.dom.Document; /** @@ -31,6 +31,27 @@ import org.w3c.dom.Document; * */ public class NodeFactoryTest { + + private QName qName; + private CompositeNode network; + + private String ns; + private Document networkShadow; + + + /** + * @throws Exception + */ + @Before + public void setUp() throws Exception { + ns = "urn:ietf:params:xml:ns:netconf:base:1.0"; + qName = new QName( + new URI(ns), + new Date(42), null); + network = NodeHelper.buildTestConfigTree(qName); + networkShadow = NodeUtils.buildShadowDomTree(network); + NodeHelper.compareXmlTree(networkShadow, "./config02-shadow.xml", getClass()); + } /** * Test method for methods creating immutable nodes in @@ -39,29 +60,19 @@ public class NodeFactoryTest { */ @Test public void testImmutableNodes() throws Exception { - QName qName = new QName( - new URI("urn:opendaylight:controller:network"), - new Date(42), "yang-data-impl-immutableTest_", null); - - CompositeNode network = NodeHelper.buildTestConfigTree(qName); - - - Assert.assertEquals(1, network.getChildren().size()); - Document domTree = NodeUtils.buildShadowDomTree(network); - NodeHelper.dumpDoc(domTree, System.out); - - CompositeNode tpList = NodeUtils.findNodeByXpath(domTree, - "//node[node-id/text()='nodeId_19']/termination-points"); + Assert.assertEquals(2, network.getChildren().size()); + CompositeNode tpList = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id/text()='nodeId_19']/{0}termination-points", ns)); Assert.assertEquals(2, tpList.getCompositesByName("termination-point").size()); -// Assert.assertEquals(1, topologies.getCompositesByName("topology").size()); -// Assert.assertEquals(2, destination.getChildren().size()); } /** - * Test method for methods creating immutable nodes in - * {@link org.opendaylight.controller.yang.data.impl.NodeFactory}. + * Test method for methods creating immutable and mutable nodes: + * {@link NodeFactory#createMutableCompositeNode(QName, CompositeNode, List, ModifyAction, CompositeNode)}, + * {@link NodeFactory#createMutableSimpleNode(QName, CompositeNode, Object, ModifyAction, SimpleNode)} * @throws Exception */ @Test @@ -79,69 +90,75 @@ public class NodeFactoryTest { // // - QName qName = new QName( - new URI("urn:ietf:params:xml:ns:netconf:base:1.0"), - new Date(42), "yang-data-impl-mutableTest"); List> value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "name"), null, "Ethernet0/0")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "mtu"), null, 1500)); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "name"), null, "Ethernet0/0")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "mtu"), null, 1500)); - CompositeNodeModificationTOImpl ifNode = NodeFactory.createCompositeNodeModification( - new QName(qName, "interface"), null, value, ModifyAction.DELETE); + MutableCompositeNode ifNode = NodeFactory.createMutableCompositeNode( + new QName(qName, "interface"), null, value, ModifyAction.DELETE, null); + ifNode.init(); NodeHelper.assignParentToChildren(ifNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "name"), null, "Ethernet1/0")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "mtu"), null, 1501)); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "name"), null, "Ethernet1/0")); + value.add(NodeFactory.createMutableSimpleNode(new QName(qName, "mtu"), null, 1501, ModifyAction.REMOVE, null)); - CompositeNode ifNode2 = NodeFactory.createCompositeNode(new QName(qName, "interface"), null, value); + CompositeNode ifNode2 = NodeFactory.createImmutableCompositeNode(new QName(qName, "interface"), null, value); NodeHelper.assignParentToChildren(ifNode2); value = new ArrayList>(); value.add(ifNode); value.add(ifNode2); - CompositeNode topNode = NodeFactory.createCompositeNode(new QName(qName, "top"), null, value); + CompositeNode topNode = NodeFactory.createImmutableCompositeNode(new QName(qName, "top"), null, value); NodeHelper.assignParentToChildren(topNode); value = new ArrayList>(); value.add(topNode); - CompositeNode root = NodeFactory.createCompositeNode(new QName(qName, "config"), null, value); - + CompositeNode root = NodeFactory.createImmutableCompositeNode(new QName(qName, "config"), null, value); + Document shadowConfig = NodeUtils.buildShadowDomTree(root); + NodeHelper.compareXmlTree(shadowConfig, "./mutableNodesConfig.xml", getClass()); Assert.assertEquals(1, root.getChildren().size()); Assert.assertEquals(1, ifNode.getSimpleNodesByName("name").size()); Assert.assertEquals(1, ifNode.getSimpleNodesByName("mtu").size()); Assert.assertEquals(2, topNode.getCompositesByName("interface").size()); - NodeModification interfaceMod = (NodeModification) - topNode.getCompositesByName("interface").get(0); + NodeModification interfaceMod = topNode.getCompositesByName("interface").get(0); Assert.assertEquals(ModifyAction.DELETE, interfaceMod.getModificationAction()); } /** - * test modifications builder + * test of {@link NodeFactory#copyDeepAsMutable(CompositeNode, Map)} * @throws Exception */ @Test - public void testCopyDeepNode() throws Exception { - QName qName = new QName( - new URI("urn:opendaylight:controller:network"), - new Date(42), "yang-data-impl-immutableTest_", null); - - CompositeNode network = NodeHelper.buildTestConfigTree(qName); + public void testCopyDeepAsMutable() throws Exception { Map, Node> mutableToOrig = new HashMap<>(); - MutableCompositeNode mutableNetwork = NodeFactory.copyDeepNode(network, mutableToOrig ); + CompositeNode mutableNetwork = NodeFactory.copyDeepAsMutable(network, mutableToOrig); - Document networkShadow = NodeUtils.buildShadowDomTree(network); - ByteArrayOutputStream expected = new ByteArrayOutputStream(); - NodeHelper.dumpDoc(networkShadow, new PrintStream(expected)); - Document mutableNetworkShadow = NodeUtils.buildShadowDomTree(mutableNetwork); - ByteArrayOutputStream actual = new ByteArrayOutputStream(); - NodeHelper.dumpDoc(mutableNetworkShadow, new PrintStream(actual)); - Assert.assertEquals(new String(expected.toByteArray()), new String(actual.toByteArray())); + NodeHelper.compareXmlTree(mutableNetworkShadow, "./config02-shadow.xml", getClass()); + + CompositeNode immutableNetwork = NodeFactory.copyDeepAsImmutable(mutableNetwork, null); + Assert.assertEquals(network, immutableNetwork); + } + + + /** + * test of {@link NodeFactory#copyDeepAsImmutable(CompositeNode, Map)} + * @throws Exception + */ + @Test + public void testCopyDeepAsImmutable() throws Exception { + Map, Node> mutableToOrig = new HashMap<>(); + CompositeNode immutableNetwork = NodeFactory.copyDeepAsImmutable(network, mutableToOrig); + + Document mutableNetworkShadow = NodeUtils.buildShadowDomTree(immutableNetwork); + NodeHelper.compareXmlTree(mutableNetworkShadow, "./config02-shadow.xml", getClass()); + + Assert.assertEquals(network, immutableNetwork); } } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java old mode 100755 new mode 100644 index 3b7c0d0fee..a7e7d2f74c --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeHelper.java @@ -7,9 +7,20 @@ */ package org.opendaylight.controller.yang.data.impl; +import groovy.lang.Binding; +import groovy.lang.GroovyShell; +import groovy.lang.Script; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.PrintStream; +import java.io.Reader; import java.io.StringWriter; +import java.lang.reflect.Method; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -20,6 +31,8 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.custommonkey.xmlunit.Diff; +import org.junit.Assert; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; import org.opendaylight.controller.yang.data.api.Node; @@ -27,7 +40,10 @@ import org.opendaylight.controller.yang.model.api.Module; import org.opendaylight.controller.yang.model.api.SchemaContext; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; import org.opendaylight.controller.yang.parser.impl.YangParserImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; +import org.xml.sax.SAXException; /** * @author michal.rehak @@ -35,6 +51,8 @@ import org.w3c.dom.Document; */ public abstract class NodeHelper { + private static final Logger LOG = LoggerFactory.getLogger(NodeHelper.class); + /** xml source of example network configuration */ public static final String NETWORK_XML = "\n" + @@ -127,145 +145,145 @@ public abstract class NodeHelper { String xmlString = result.getWriter().toString(); out.println(xmlString); } - + /** * @param qName * @return example tree, see {@link #NETWORK_XML} */ public static CompositeNode buildTestConfigTree(QName qName) { List> value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "element-id"), null, "ntElementId_09")); - CompositeNode ntElementNode1 = NodeFactory.createCompositeNode(new QName(qName, "network-element"), null, value); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "element-id"), null, "ntElementId_09")); + CompositeNode ntElementNode1 = NodeFactory.createImmutableCompositeNode(new QName(qName, "network-element"), null, value); assignParentToChildren(ntElementNode1); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "element-id"), null, "ntElementId_10")); - CompositeNode ntElementNode2 = NodeFactory.createCompositeNode(new QName(qName, "network-element"), null, value); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "element-id"), null, "ntElementId_10")); + CompositeNode ntElementNode2 = NodeFactory.createImmutableCompositeNode(new QName(qName, "network-element"), null, value); assignParentToChildren(ntElementNode2); value = new ArrayList>(); value.add(ntElementNode1); value.add(ntElementNode2); - CompositeNode ntElementsNode = NodeFactory.createCompositeNode( + CompositeNode ntElementsNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "network-elements"), null, value); assignParentToChildren(ntElementsNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "dest-node"), null, "nodeId_07")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "dest-tp"), null, "tpId_08")); - CompositeNode destination = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "dest-node"), null, "nodeId_07")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "dest-tp"), null, "tpId_08")); + CompositeNode destination = NodeFactory.createImmutableCompositeNode( new QName(qName, "destination"), null, value); assignParentToChildren(destination); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "source-node"), null, "nodeId_05")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "source-tp"), null, "tpId_06")); - CompositeNode source = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "source-node"), null, "nodeId_05")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "source-tp"), null, "tpId_06")); + CompositeNode source = NodeFactory.createImmutableCompositeNode( new QName(qName, "source"), null, value); assignParentToChildren(source); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "link-id"), null, "linkId_04")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "link-id"), null, "linkId_04")); value.add(source); value.add(destination); - CompositeNode link1 = NodeFactory.createCompositeNode( + CompositeNode link1 = NodeFactory.createImmutableCompositeNode( new QName(qName, "link"), null, value); assignParentToChildren(link1); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "dest-node"), null, "nodeId_14")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "dest-tp"), null, "tpId_15")); - destination = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "dest-node"), null, "nodeId_14")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "dest-tp"), null, "tpId_15")); + destination = NodeFactory.createImmutableCompositeNode( new QName(qName, "destination"), null, value); assignParentToChildren(destination); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "source-node"), null, "nodeId_12")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "source-tp"), null, "tpId_13")); - source = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "source-node"), null, "nodeId_12")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "source-tp"), null, "tpId_13")); + source = NodeFactory.createImmutableCompositeNode( new QName(qName, "source"), null, value); assignParentToChildren(source); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "link-id"), null, "linkId_11")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "link-id"), null, "linkId_11")); value.add(source); value.add(destination); - CompositeNode link2 = NodeFactory.createCompositeNode( + CompositeNode link2 = NodeFactory.createImmutableCompositeNode( new QName(qName, "link"), null, value); assignParentToChildren(link2); value = new ArrayList>(); value.add(link1); value.add(link2); - CompositeNode links = NodeFactory.createCompositeNode( + CompositeNode links = NodeFactory.createImmutableCompositeNode( new QName(qName, "links"), null, value); assignParentToChildren(links); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "tp-id"), null, "tpId_03")); - CompositeNode terminationPointNode1 = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "tp-id"), null, "tpId_03")); + CompositeNode terminationPointNode1 = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-point"), null, value); assignParentToChildren(terminationPointNode1); value = new ArrayList>(); value.add(terminationPointNode1); - CompositeNode terminationPointsNode = NodeFactory.createCompositeNode( + CompositeNode terminationPointsNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-points"), null, value); assignParentToChildren(terminationPointsNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "node-id"), null, "nodeId_02")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_02")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "node-id"), null, "nodeId_02")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_02")); value.add(terminationPointsNode); - CompositeNode node1Node = NodeFactory.createCompositeNode( + CompositeNode node1Node = NodeFactory.createImmutableCompositeNode( new QName(qName, "node"), null, value); assignParentToChildren(node1Node); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "tp-id"), null, "tpId_18")); - terminationPointNode1 = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "tp-id"), null, "tpId_18")); + terminationPointNode1 = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-point"), null, value); assignParentToChildren(terminationPointNode1); value = new ArrayList>(); value.add(terminationPointNode1); - terminationPointsNode = NodeFactory.createCompositeNode( + terminationPointsNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-points"), null, value); assignParentToChildren(terminationPointsNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "node-id"), null, "nodeId_16")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_17")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "node-id"), null, "nodeId_16")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_17")); value.add(terminationPointsNode); - CompositeNode node2Node = NodeFactory.createCompositeNode( + CompositeNode node2Node = NodeFactory.createImmutableCompositeNode( new QName(qName, "node"), null, value); assignParentToChildren(node2Node); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "tp-id"), null, "tpId_18")); - terminationPointNode1 = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "tp-id"), null, "tpId_18")); + terminationPointNode1 = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-point"), null, value); assignParentToChildren(terminationPointNode1); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "tp-id"), null, "tpId_19")); - CompositeNode terminationPointNode2 = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "tp-id"), null, "tpId_19")); + CompositeNode terminationPointNode2 = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-point"), null, value); assignParentToChildren(terminationPointNode2); value = new ArrayList>(); value.add(terminationPointNode1); value.add(terminationPointNode2); - terminationPointsNode = NodeFactory.createCompositeNode( + terminationPointsNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "termination-points"), null, value); assignParentToChildren(terminationPointsNode); value = new ArrayList>(); - value.add(NodeFactory.createSimpleNode(new QName(qName, "node-id"), null, "nodeId_19")); - value.add(NodeFactory.createSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_20")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "node-id"), null, "nodeId_19")); + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "supporting-ne"), null, "networkId_20")); value.add(terminationPointsNode); - CompositeNode node3Node = NodeFactory.createCompositeNode( + CompositeNode node3Node = NodeFactory.createImmutableCompositeNode( new QName(qName, "node"), null, value); assignParentToChildren(node3Node); @@ -273,27 +291,28 @@ public abstract class NodeHelper { value.add(node1Node); value.add(node2Node); value.add(node3Node); - CompositeNode nodesNode = NodeFactory.createCompositeNode( + CompositeNode nodesNode = NodeFactory.createImmutableCompositeNode( new QName(qName, "nodes"), null, value); assignParentToChildren(nodesNode); value = new ArrayList>(); value.add(links); value.add(nodesNode); - value.add(NodeFactory.createSimpleNode(new QName(qName, "topology-id"), null, "topId_01")); - CompositeNode topology = NodeFactory.createCompositeNode( + value.add(NodeFactory.createImmutableSimpleNode(new QName(qName, "topology-id"), null, "topId_01")); + CompositeNode topology = NodeFactory.createImmutableCompositeNode( new QName(qName, "topology"), null, value); assignParentToChildren(topology); value = new ArrayList>(); value.add(topology); - CompositeNode topologies = NodeFactory.createCompositeNode( + CompositeNode topologies = NodeFactory.createImmutableCompositeNode( new QName(qName, "topologies"), null, value); assignParentToChildren(topologies); value = new ArrayList>(); value.add(topologies); - CompositeNode network = NodeFactory.createCompositeNode( + value.add(ntElementsNode); + CompositeNode network = NodeFactory.createImmutableCompositeNode( new QName(qName, "network"), null, value); assignParentToChildren(network); @@ -323,5 +342,70 @@ public abstract class NodeHelper { .parseYangModelsFromStreams(yangInputStreams); return yParser.resolveSchemaContext(modules); } + + /** + * @param scriptName + * @return tree root + * @throws Exception + */ + public static CompositeNode loadConfigByGroovy(String scriptName) throws Exception { + InputStream configStream = NodeHelper.class.getResourceAsStream(scriptName); + Binding binding = new Binding(); + GroovyShell gShell = new GroovyShell(binding); + LOG.debug("groovy: starting script parse.. " + scriptName); + Script configScript = gShell.parse(new InputStreamReader(configStream)); + LOG.debug("groovy: starting script.. " + scriptName); + configScript.run(); + LOG.debug("groovy: digging result"); + Object xmlGen = binding.getVariable("xmlGen"); + LOG.debug("xmlGen = " + xmlGen); + Method getter = xmlGen.getClass().getDeclaredMethod("getBuilder", new Class[0]); + MyNodeBuilder builder = (MyNodeBuilder) getter.invoke(xmlGen, new Object[0]); + + return builder.getRootNode(); + } + + /** + * @param pattern , e.g.:
"//{0}:network/{1}:xx[text() = 'sss']"
+ * @param nsArg , e.g.:
{"uri:ns1", "uri:ns2"}
+ * @return pattern with namespaces:
//uri:ns1:network/uri:ns2:xx[text() = ''sss'']"
+ */ + public static String AddNamespaceToPattern(String pattern, Object... nsArg) { + Object[] ns = nsArg; + String patternNs = pattern.replaceAll("'", "''"); + if (ns == null) { + ns = new Object[]{""}; + } else { + // add ':' into pattern after placeholders + patternNs = patternNs.replaceAll("(\\{[0-9]+\\})", "$1:"); + } + + return MessageFormat.format(patternNs, ns); + } + + /** + * @param tree + * @param xmlFile + * @param clazz + * @throws Exception + * @throws SAXException + * @throws IOException + */ + public static void compareXmlTree(Document tree, String xmlFile, Class clazz) throws Exception, + SAXException, IOException { + ByteArrayOutputStream actualRaw = new ByteArrayOutputStream(); + dumpDoc(tree, new PrintStream(actualRaw)); + Reader actualReader = new InputStreamReader(new ByteArrayInputStream(actualRaw.toByteArray())); + + Reader expectedReader = new InputStreamReader(clazz.getResourceAsStream(xmlFile)); + Diff myDiff = new Diff(expectedReader, actualReader); + myDiff.overrideDifferenceListener(new IgnoreWhiteCharsDiffListener()); + + boolean similar = myDiff.similar(); + if (! similar) { + System.out.println(new String(actualRaw.toByteArray())); + } + Assert.assertEquals(myDiff.toString(), true, similar); + } } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImplTest.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImplTest.java old mode 100755 new mode 100644 index 2df397a9ed..5f4e482bca --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImplTest.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeModificationBuilderImplTest.java @@ -18,8 +18,11 @@ import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; 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.SimpleNode; import org.opendaylight.controller.yang.model.api.SchemaContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; /** @@ -27,12 +30,28 @@ import org.w3c.dom.Document; * */ public class NodeModificationBuilderImplTest { + + private static final Logger LOG = LoggerFactory + .getLogger(NodeModificationBuilderImplTest.class); private SchemaContext schemaCtx; private QName qName; private CompositeNode network; private NodeModificationBuilderImpl nodeModificationBuilder; + private String ns; + + /** + * @throws Exception + */ + private void dumpResult() throws Exception { + CompositeNode diffTree = nodeModificationBuilder.buildDiffTree(); + CompositeNode diffTreeImmutable = NodeFactory.copyDeepAsImmutable(diffTree, null); + + Document diffShadow = NodeUtils.buildShadowDomTree(diffTreeImmutable); + NodeHelper.dumpDoc(diffShadow, System.out); + } + /** * prepare schemaContext * @throws Exception @@ -41,14 +60,130 @@ public class NodeModificationBuilderImplTest { public void setUp() throws Exception { schemaCtx = NodeHelper.loadSchemaContext(); + ns = "urn:opendaylight:controller:network"; qName = new QName( - new URI("urn:opendaylight:controller:network"), + new URI(ns), new Date(1369000800000L), "topos"); network = NodeHelper.buildTestConfigTree(qName); nodeModificationBuilder = new NodeModificationBuilderImpl(network, schemaCtx); } + /** + * Test method for + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#getMutableEquivalent(org.opendaylight.controller.yang.data.api.Node)} + * . + */ + @Test + public void testGetMutableEquivalent() { + MutableCompositeNode rootMutable = (MutableCompositeNode) + nodeModificationBuilder.getMutableEquivalent(network); + + CompositeNode topologies = network.getCompositesByName("topologies").iterator().next(); + Node mutableEquivalent = nodeModificationBuilder.getMutableEquivalent(topologies); + CompositeNode topologiesMutable = rootMutable.getCompositesByName("topologies").iterator().next(); + + Assert.assertSame(topologiesMutable, mutableEquivalent); + } + + /** + * Test method for + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} + * . + * @throws Exception + */ + @Test + public void testBuildDiffTreeAddSimple() throws Exception { + LOG.debug("testBuildDiffTreeAddSimple"); + Document networkShadow = NodeUtils.buildShadowDomTree(network); + CompositeNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]", ns)); + + MutableCompositeNode mutableParent = (MutableCompositeNode) + nodeModificationBuilder.getMutableEquivalent(needle); + + MutableSimpleNode newMutable = NodeFactory.createMutableSimpleNode( + new QName(needle.getNodeType(), "anySubNode"), mutableParent, "42", null, null); + + nodeModificationBuilder.addNode(newMutable); + dumpResult(); + } + + /** + * Test method for + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} + * . + * @throws Exception + */ + @Test + public void testBuildDiffTreeAddComposite() throws Exception { + LOG.debug("testBuildDiffTreeAddComposite"); + Document networkShadow = NodeUtils.buildShadowDomTree(network); + CompositeNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]", ns)); + + MutableCompositeNode mutableParent = (MutableCompositeNode) + nodeModificationBuilder.getMutableEquivalent(needle); + + MutableSimpleNode newMutable = NodeFactory.createMutableSimpleNode( + new QName(needle.getNodeType(), "anySubNode"), null, "42", null, null); + + MutableCompositeNode newMutableCom = NodeFactory.createMutableCompositeNode( + new QName(needle.getNodeType(), "anySubNode"), mutableParent, + NodeUtils.buildChildrenList(newMutable), null, null); + NodeUtils.fixChildrenRelation(newMutableCom); + newMutableCom.init(); + + nodeModificationBuilder.addNode(newMutableCom); + dumpResult(); + } + + /** + * Test method for + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} + * . + * @throws Exception + */ + @Test + public void testBuildDiffTreeDeleteComposite() throws Exception { + LOG.debug("testBuildDiffTreeDeleteComposite"); + Document networkShadow = NodeUtils.buildShadowDomTree(network); + SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns)); + + @SuppressWarnings("unchecked") + MutableSimpleNode mutableNeedle = (MutableSimpleNode) + nodeModificationBuilder.getMutableEquivalent(needle); + + nodeModificationBuilder.deleteNode(mutableNeedle.getParent().asMutable()); + dumpResult(); + } + + /** + * Test method for + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} + * . + * @throws Exception + */ + @Test + public void testBuildDiffTreeDeleteSimple() throws Exception { + LOG.debug("testBuildDiffTreeDeleteSimple"); + Document networkShadow = NodeUtils.buildShadowDomTree(network); + SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns)); + + @SuppressWarnings("unchecked") + MutableSimpleNode mutableNeedle = (MutableSimpleNode) + nodeModificationBuilder.getMutableEquivalent(needle); + + nodeModificationBuilder.deleteNode(mutableNeedle); + dumpResult(); + } + /** * Test method for * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} @@ -56,37 +191,111 @@ public class NodeModificationBuilderImplTest { * @throws Exception */ @Test - public void testBuildDiffTree() throws Exception { + public void testBuildDiffTreeMerge() throws Exception { + LOG.debug("testBuildDiffTreeMerge"); Document networkShadow = NodeUtils.buildShadowDomTree(network); - SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, - "//node[node-id='nodeId_19']//termination-point[2]/tp-id"); + SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns)); @SuppressWarnings("unchecked") MutableSimpleNode mutableNeedle = (MutableSimpleNode) nodeModificationBuilder.getMutableEquivalent(needle); mutableNeedle.setValue("tpId_18x"); - nodeModificationBuilder.replaceNode(mutableNeedle); - CompositeNode diffTree = nodeModificationBuilder.buildDiffTree(); + nodeModificationBuilder.mergeNode(mutableNeedle.getParent().asMutable()); + dumpResult(); + } + + /** + * Test method for + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} + * . + * @throws Exception + */ + @Test + public void testBuildDiffTreeRemoveComposite() throws Exception { + LOG.debug("testBuildDiffTreeRemoveComposite"); + Document networkShadow = NodeUtils.buildShadowDomTree(network); + SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns)); - Document diffShadow = NodeUtils.buildShadowDomTree(diffTree); - NodeHelper.dumpDoc(diffShadow, System.out); + @SuppressWarnings("unchecked") + MutableSimpleNode mutableNeedle = (MutableSimpleNode) + nodeModificationBuilder.getMutableEquivalent(needle); + + nodeModificationBuilder.removeNode(mutableNeedle.getParent().asMutable()); + dumpResult(); } /** * Test method for - * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#getMutableEquivalent(org.opendaylight.controller.yang.data.api.Node)} + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} * . + * @throws Exception */ @Test - public void testGetMutableEquivalent() { - MutableCompositeNode rootMutable = (MutableCompositeNode) - nodeModificationBuilder.getMutableEquivalent(network); + public void testBuildDiffTreeRemoveSimple() throws Exception { + LOG.debug("testBuildDiffTreeRemoveSimple"); + Document networkShadow = NodeUtils.buildShadowDomTree(network); + SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns)); - CompositeNode topologies = network.getCompositesByName("topologies").iterator().next(); - CompositeNode topologiesMutable = rootMutable.getCompositesByName("topologies").iterator().next(); + @SuppressWarnings("unchecked") + MutableSimpleNode mutableNeedle = (MutableSimpleNode) + nodeModificationBuilder.getMutableEquivalent(needle); + + nodeModificationBuilder.removeNode(mutableNeedle); + dumpResult(); + } + + /** + * Test method for + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} + * . + * @throws Exception + */ + @Test + public void testBuildDiffTreeReplaceComposite() throws Exception { + LOG.debug("testBuildDiffTreeReplaceComposite"); + Document networkShadow = NodeUtils.buildShadowDomTree(network); + SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns)); + + @SuppressWarnings("unchecked") + MutableSimpleNode mutableNeedle = (MutableSimpleNode) + nodeModificationBuilder.getMutableEquivalent(needle); + + mutableNeedle.setValue("tpId_18x"); + nodeModificationBuilder.replaceNode(mutableNeedle.getParent().asMutable()); + dumpResult(); + } + + /** + * Test method for + * {@link org.opendaylight.controller.yang.data.impl.NodeModificationBuilderImpl#buildDiffTree()} + * . + * @throws Exception + */ + @Test + public void testBuildDiffTreeReplaceSimple() throws Exception { + LOG.debug("testBuildDiffTreeReplaceSimple"); + Document networkShadow = NodeUtils.buildShadowDomTree(network); + SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns)); - Assert.assertSame(topologiesMutable, nodeModificationBuilder.getMutableEquivalent(topologies)); + @SuppressWarnings("unchecked") + MutableSimpleNode mutableNeedle = (MutableSimpleNode) + nodeModificationBuilder.getMutableEquivalent(needle); + + mutableNeedle.setValue("tpId_18x"); + nodeModificationBuilder.replaceNode(mutableNeedle); + dumpResult(); } + } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeUtilsTest.java b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeUtilsTest.java old mode 100755 new mode 100644 index 53ffc2ea0b..16d736a35f --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeUtilsTest.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/java/org/opendaylight/controller/yang/data/impl/NodeUtilsTest.java @@ -1,110 +1,165 @@ -/* - * Copyright (c) 2013 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.controller.yang.data.impl; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.net.URI; -import java.util.Date; -import java.util.List; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.data.api.CompositeNode; -import org.opendaylight.controller.yang.data.api.Node; -import org.opendaylight.controller.yang.data.api.SimpleNode; -import org.opendaylight.controller.yang.model.api.ListSchemaNode; -import org.opendaylight.controller.yang.model.api.SchemaContext; -import org.w3c.dom.Document; - -/** - * @author michal.rehak - * - */ -public class NodeUtilsTest { - - private QName qName; - private CompositeNode network; - - /** - * @throws Exception - */ - @Before - public void setUp() throws Exception { - qName = new QName( - new URI("urn:ietf:params:xml:ns:netconf:base:1.0"), - new Date(42), "yang-data-impl-mutableTest"); - network = NodeHelper.buildTestConfigTree(qName); - } - - /** - * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildPath(org.opendaylight.controller.yang.data.api.Node)}. - * @throws Exception - */ - @Test - public void testBuildPath() throws Exception { - SimpleNode needle = network.getCompositesByName("topologies").iterator().next() - .getCompositesByName("topology").iterator().next() - .getSimpleNodesByName("topology-id").iterator().next(); - String breadCrumbs = NodeUtils.buildPath(needle); - - Assert.assertEquals("network.topologies.topology.topology-id", breadCrumbs); - } - - /** - * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildShadowDomTree(org.opendaylight.controller.yang.data.api.CompositeNode)}. - * @throws Exception - */ - @Test - public void testBuildShadowDomTree() throws Exception { - Document networkShadow = NodeUtils.buildShadowDomTree(network); - ByteArrayOutputStream actual = new ByteArrayOutputStream(); - NodeHelper.dumpDoc(networkShadow, new PrintStream(actual)); - - Assert.assertEquals(2760, new String(actual.toByteArray()).length()); - } - - /** - * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#findNodeByXpath(org.w3c.dom.Document, java.lang.String)}. - * @throws Exception - */ - @Test - public void testFindNodeByXpath() throws Exception { - Document networkShadow = NodeUtils.buildShadowDomTree(network); - SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, - "//node[node-id='nodeId_19']//termination-point[2]/tp-id"); - Assert.assertNotNull(needle); - Assert.assertEquals("tpId_18", needle.getValue()); - } - - /** - * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildNodeMap(java.util.List)}. - */ - @Test - public void testBuildNodeMap() { - CompositeNode topology = network.getCompositesByName("topologies").iterator().next() - .getCompositesByName("topology").iterator().next(); - - Map>> nodeMap = NodeUtils.buildNodeMap(topology.getChildren()); - Assert.assertEquals(3, nodeMap.size()); - } - - /** - * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildMapOfListNodes(org.opendaylight.controller.yang.model.api.SchemaContext)}. - */ - @Test - public void testBuildMapOfListNodes() { - SchemaContext schemaCtx = NodeHelper.loadSchemaContext(); - Map mapOfLists = NodeUtils.buildMapOfListNodes(schemaCtx); - Assert.assertEquals(5, mapOfLists.size()); - } - -} +/* + * Copyright (c) 2013 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.controller.yang.data.impl; + +import java.io.IOException; +import java.net.URI; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Stack; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.opendaylight.controller.yang.data.api.Node; +import org.opendaylight.controller.yang.data.api.SimpleNode; +import org.opendaylight.controller.yang.model.api.ListSchemaNode; +import org.opendaylight.controller.yang.model.api.SchemaContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; + +/** + * @author michal.rehak + * + */ +public class NodeUtilsTest { + + private static final Logger LOG = LoggerFactory + .getLogger(NodeUtilsTest.class); + + private QName qName; + private CompositeNode network; + + private String ns; + + + /** + * @throws Exception + */ + @Before + public void setUp() throws Exception { + ns = "urn:ietf:params:xml:ns:netconf:base:1.0"; + qName = new QName( + new URI(ns), + new Date(42), "yang-data-impl-mutableTest"); + network = NodeHelper.buildTestConfigTree(qName); + } + + /** + * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildPath(org.opendaylight.controller.yang.data.api.Node)}. + * @throws Exception + */ + @Test + public void testBuildPath() throws Exception { + SimpleNode needle = network.getCompositesByName("topologies").iterator().next() + .getCompositesByName("topology").iterator().next() + .getSimpleNodesByName("topology-id").iterator().next(); + String breadCrumbs = NodeUtils.buildPath(needle); + + Assert.assertEquals("network.topologies.topology.topology-id", breadCrumbs); + } + + /** + * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildShadowDomTree(org.opendaylight.controller.yang.data.api.CompositeNode)}. + * @throws Exception + */ + @Test + public void testBuildShadowDomTree() throws Exception { + MemoryConsumption mc = new MemoryConsumption(); + mc.startObserving(); + + Document networkShadow = NodeUtils.buildShadowDomTree(network); + + LOG.debug("After dom built: "+mc.finishObserving()); + NodeHelper.compareXmlTree(networkShadow, "./config02-shadow.xml", getClass()); + } + + /** + * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#findNodeByXpath(org.w3c.dom.Document, java.lang.String)}. + * @throws Exception + */ + @Test + public void testFindNodeByXpath() throws Exception { + Document networkShadow = NodeUtils.buildShadowDomTree(network); + MemoryConsumption mc = new MemoryConsumption(); + mc.startObserving(); + + SimpleNode needle = NodeUtils.findNodeByXpath(networkShadow, + NodeHelper.AddNamespaceToPattern( + "//{0}node[{0}node-id='nodeId_19']//{0}termination-point[2]/{0}tp-id", ns)); + + LOG.debug("After xpath executed: "+mc.finishObserving()); + + Assert.assertNotNull(needle); + Assert.assertEquals("tpId_18", needle.getValue()); + } + + /** + * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildNodeMap(java.util.List)}. + */ + @Test + public void testBuildNodeMap() { + CompositeNode topology = network.getCompositesByName("topologies").iterator().next() + .getCompositesByName("topology").iterator().next(); + + Map>> nodeMap = NodeUtils.buildNodeMap(topology.getChildren()); + Assert.assertEquals(3, nodeMap.size()); + } + + /** + * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildMapOfListNodes(org.opendaylight.controller.yang.model.api.SchemaContext)}. + */ + @Test + public void testBuildMapOfListNodes() { + SchemaContext schemaCtx = NodeHelper.loadSchemaContext(); + Map mapOfLists = NodeUtils.buildMapOfListNodes(schemaCtx); + Assert.assertEquals(5, mapOfLists.size()); + } + + /** + * Test method for {@link org.opendaylight.controller.yang.data.impl.NodeUtils#buildMapOfListNodes(org.opendaylight.controller.yang.model.api.SchemaContext)}. + * @throws Exception + * @throws IOException + */ + @Test + public void testLoadConfigByGroovy() throws IOException, Exception { + CompositeNode treeRoot = NodeHelper.loadConfigByGroovy("./config02.groovy"); + Document shadowTree = NodeUtils.buildShadowDomTree(treeRoot); + try { + checkFamilyBinding(treeRoot); + } catch (Exception e) { + LOG.error(e.getMessage()); + throw e; + } + + NodeHelper.compareXmlTree(shadowTree, "./config02g-shadow.xml", getClass()); + } + + private static void checkFamilyBinding(CompositeNode treeRoot) throws Exception { + Stack jobQueue = new Stack<>(); + jobQueue.push(treeRoot); + + while (!jobQueue.isEmpty()) { + CompositeNode job = jobQueue.pop(); + for (Node child : job.getChildren()) { + if (child instanceof CompositeNode) { + jobQueue.push((CompositeNode) child); + } + + if (job != child.getParent()) { + throw new Exception("binding mismatch occured: \nPARENT["+job+"]\n CHILD[" + child+"]\n +-> "+child.getParent()); + } + } + } + } + +} diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/MyXmlGenerator.groovy b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/MyXmlGenerator.groovy new file mode 100644 index 0000000000..f22da0c78a --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/MyXmlGenerator.groovy @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2013 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 + */ +import groovy.xml.MarkupBuilder +import org.opendaylight.controller.yang.data.impl.MyNodeBuilder + +/** + * wrapper class - applies hardcoded builder on given data closure + */ +class MyXmlGenerator { + + def myBuilder + + MyXmlGenerator() { + myBuilder = MyNodeBuilder.newInstance(); + } + + MyNodeBuilder getBuilder() { + return myBuilder; + } + + void buildTree(data) { + data.setDelegate(myBuilder) + data() + } + + /** + * tests builder execution + */ + static void main(args) { + println 'hello' + def data = { + network(xmlns: 'urn:opendaylight:controller:network') { + topologies { + topology { + 'topology-id'('topId_01') + + nodes { + node { + 'node-id'('nodeId_02') + 'supporting-ne'('networkId_03') + 'termination-points' { + 'termination-point' { + 'tp-id'('tpId_04') + } + } + } + node { + 'node-id'('nodeId_05') + 'supporting-ne'('networkId_06') + 'termination-points' { + 'termination-point' { + 'tp-id'('tpId_07') + } + } + } + node { + 'node-id'('nodeId_08') + 'supporting-ne'('networkId_09') + 'termination-points' { + 'termination-point' { + 'tp-id'('tpId_10') + } + 'termination-point' { + 'tp-id'('tpId_11') + } + } + } + } + links { + link { + 'link-id'('linkId_12') + source { + 'source-node'('nodeId_13') + 'source-tp'('tpId_13') + } + destination { + 'dest-node'('nodeId_14') + 'dest-tp'('tpId_14') + } + } + link { + 'link-id'('linkId_15') + source { + 'source-node'('nodeId_16') + 'source-tp'('tpId_16') + } + destination { + 'dest-node'('nodeId_17') + 'dest-tp'('tpId_17') + } + } + } + } + } + 'network-elements' { + 'network-element' { + 'element-id'('ntElementId_18') + } + 'network-element' { + 'element-id'('ntElementId_19') + } + } + } + + } + + def xmlGen = new MyXmlGenerator() + xmlGen.buildTree(data) + println xmlGen.getBuilder().getRootNode() + } + +} + + + diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config01.xml b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config01.xml old mode 100755 new mode 100644 diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02.content b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02.content deleted file mode 100755 index e9c05c9652..0000000000 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02.content +++ /dev/null @@ -1,73 +0,0 @@ -xmlDoc.network(xmlns: 'urn:opendaylight:controller:network') { - topologies { - topology { - 'topology-id'('topId_'+cnt.get()) - - types() - nodes { - node { - 'node-id'('nodeId_'+cnt.get()) - 'supporting-ne'('networkId_'+cnt.get()) - 'termination-points' { - 'termination-point' { - 'tp-id'('tpId_'+cnt.get()) - } - } - } - node { - 'node-id'('nodeId_'+cnt.get()) - 'supporting-ne'('networkId_'+cnt.get()) - 'termination-points' { - 'termination-point' { - 'tp-id'('tpId_'+cnt.get()) - } - } - } - node { - 'node-id'('nodeId_'+cnt.get()) - 'supporting-ne'('networkId_'+cnt.get()) - 'termination-points' { - 'termination-point' { - 'tp-id'('tpId_'+cnt.get()) - } - 'termination-point' { - 'tp-id'('tpId_'+cnt.get()) - } - } - } - } - links { - link { - 'link-id'('linkId_'+cnt.get()) - source { - 'source-node'('nodeId_'+cnt.get()) - 'source-tp'('tpId_'+cnt.get(false)) - } - destination { - 'dest-node'('nodeId_'+cnt.get()) - 'dest-tp'('tpId_'+cnt.get(false)) - } - } - link { - 'link-id'('linkId_'+cnt.get()) - source { - 'source-node'('nodeId_'+cnt.get()) - 'source-tp'('tpId_'+cnt.get(false)) - } - destination { - 'dest-node'('nodeId_'+cnt.get()) - 'dest-tp'('tpId_'+cnt.get(false)) - } - } - } - } - } - 'network-elements' { - 'network-element' { - 'element-id'('ntElementId_'+cnt.get()) - } - 'network-element' { - 'element-id'('ntElementId_'+cnt.get()) - } - } - } \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02.xml b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02.xml old mode 100755 new mode 100644 diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02g.xml b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02g.xml deleted file mode 100644 index d1ac68c014..0000000000 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/config02g.xml +++ /dev/null @@ -1,73 +0,0 @@ - - - - - topId_01 - - - - nodeId_02 - networkId_03 - - - tpId_04 - - - - - nodeId_05 - networkId_06 - - - tpId_07 - - - - - nodeId_08 - networkId_09 - - - tpId_10 - - - tpId_11 - - - - - - - linkId_12 - - nodeId_13 - tpId_13 - - - nodeId_14 - tpId_14 - - - - linkId_15 - - nodeId_16 - tpId_16 - - - nodeId_17 - tpId_17 - - - - - - - - ntElementId_18 - - - ntElementId_19 - - - diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/controller-network.xsd b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/controller-network.xsd old mode 100755 new mode 100644 diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/controller-network.yang b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/controller-network.yang old mode 100755 new mode 100644 diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/generateXml.groovy b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/generateXml.groovy deleted file mode 100755 index a778137beb..0000000000 --- a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/generateXml.groovy +++ /dev/null @@ -1,110 +0,0 @@ -//import groovy.xml.StreamingMarkupBuilder -import groovy.xml.MarkupBuilder -import groovy.xml.XmlUtil - -class Counter { - def counter = 0 - def get() { - return get(true) - } - def get(isInc) { - if (isInc) { - counter++ - } - return String.format('%02d', counter) - } -} - - -cnt = new Counter() -def writer = new StringWriter() -xmlDoc = new MarkupBuilder(writer) -xmlDoc.setDoubleQuotes(true) -xmlDoc.getMkp().xmlDeclaration(version:'1.0', encoding: 'UTF-8') - -//def data = { -// mkp.xmlDeclaration() -// network(xmlns: 'urn:opendaylight:controller:network') { -dataFile = new File(args[0]) -evaluate(dataFile) -// xmlDoc.network(xmlns: 'urn:opendaylight:controller:network') { - // topologies { - // topology { - // 'topology-id'('topId_'+cnt.get()) - // types() - // nodes { - // node { - // 'node-id'('nodeId_'+cnt.get()) - // 'supporting-ne'('networkId_'+cnt.get()) - // 'termination-points' { - // 'termination-point' { - // 'tp-id'('tpId_'+cnt.get()) - // } - // } - // } - // node { - // 'node-id'('nodeId_'+cnt.get()) - // 'supporting-ne'('networkId_'+cnt.get()) - // 'termination-points' { - // 'termination-point' { - // 'tp-id'('tpId_'+cnt.get()) - // } - // } - // } - // node { - // 'node-id'('nodeId_'+cnt.get()) - // 'supporting-ne'('networkId_'+cnt.get()) - // 'termination-points' { - // 'termination-point' { - // 'tp-id'('tpId_'+cnt.get()) - // } - // 'termination-point' { - // 'tp-id'('tpId_'+cnt.get()) - // } - // } - // } - // } - // links { - // link { - // 'link-id'('linkId_'+cnt.get()) - // source { - // 'source-node'('nodeId_'+cnt.get()) - // 'source-tp'('tpId_'+cnt.get(false)) - // } - // destination { - // 'dest-node'('nodeId_'+cnt.get()) - // 'dest-tp'('tpId_'+cnt.get(false)) - // } - // } - // link { - // 'link-id'('linkId_'+cnt.get()) - // source { - // 'source-node'('nodeId_'+cnt.get()) - // 'source-tp'('tpId_'+cnt.get(false)) - // } - // destination { - // 'dest-node'('nodeId_'+cnt.get()) - // 'dest-tp'('tpId_'+cnt.get(false)) - // } - // } - // } - // } - // } - // 'network-elements' { - // 'network-element' { - // 'element-id'('ntElementId_'+cnt.get()) - // } - // 'network-element' { - // 'element-id'('ntElementId_'+cnt.get()) - // } - // } - // } - -//} - - -// def xmlDoc = new StreamingMarkupBuilder() -// xmlDoc.encoding = 'UTF' -//println XmlUtil.serialize(xmlDoc.bind(data)) - -println writer diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/ietf-inet-types@2010-09-24.yang b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/ietf-inet-types@2010-09-24.yang old mode 100755 new mode 100644 diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/log4j-test.xml b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/log4j-test.xml new file mode 100755 index 0000000000..189236a783 --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/log4j-test.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02-shadow.xml b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02-shadow.xml new file mode 100644 index 0000000000..aaaf22dfc1 --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02-shadow.xml @@ -0,0 +1,73 @@ + + + + + + topId_01 + + + nodeId_19 + networkId_20 + + + tpId_19 + + + tpId_18 + + + + + nodeId_16 + networkId_17 + + + tpId_18 + + + + + nodeId_02 + networkId_02 + + + tpId_03 + + + + + + + + tpId_15 + nodeId_14 + + + tpId_13 + nodeId_12 + + linkId_11 + + + + tpId_08 + nodeId_07 + + + tpId_06 + nodeId_05 + + linkId_04 + + + + + + + ntElementId_10 + + + ntElementId_09 + + + diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02.groovy b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02.groovy new file mode 100644 index 0000000000..cfd8ab0743 --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02.groovy @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2013 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 + */ +def data = { + network(xmlns: 'urn:opendaylight:controller:network') { + topologies { + topology { + 'topology-id'('topId_01') + + //types() + nodes { + node { + 'node-id'('nodeId_02') + 'supporting-ne'('networkId_03') + 'termination-points' { + 'termination-point' { + 'tp-id'('tpId_04') + } + } + } + node { + 'node-id'('nodeId_05') + 'supporting-ne'('networkId_06') + 'termination-points' { + 'termination-point' { + 'tp-id'('tpId_07') + } + } + } + node { + 'node-id'('nodeId_08') + 'supporting-ne'('networkId_09') + 'termination-points' { + 'termination-point' { + 'tp-id'('tpId_10') + } + 'termination-point' { + 'tp-id'('tpId_11') + } + } + } + } + links { + link { + 'link-id'('linkId_12') + source { + 'source-node'('nodeId_13') + 'source-tp'('tpId_13') + } + destination { + 'dest-node'('nodeId_14') + 'dest-tp'('tpId_14') + } + } + link { + 'link-id'('linkId_15') + source { + 'source-node'('nodeId_16') + 'source-tp'('tpId_16') + } + destination { + 'dest-node'('nodeId_17') + 'dest-tp'('tpId_17') + } + } + } + } + } + 'network-elements' { + 'network-element' { + 'element-id'('ntElementId_18') + } + 'network-element' { + 'element-id'('ntElementId_19') + } + } + } +} + +System.err.println('data inited') + +import MyXmlGenerator + +xmlGen = new MyXmlGenerator() +xmlGen.buildTree(data) diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02g-shadow.xml b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02g-shadow.xml new file mode 100644 index 0000000000..9db934288b --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/config02g-shadow.xml @@ -0,0 +1,72 @@ + + + + + + + + tpId_17 + nodeId_17 + + + tpId_16 + nodeId_16 + + linkId_15 + + + + tpId_14 + nodeId_14 + + + tpId_13 + nodeId_13 + + linkId_12 + + + + + + + tpId_11 + + + tpId_10 + + + networkId_09 + nodeId_08 + + + + + tpId_07 + + + networkId_06 + nodeId_05 + + + + + tpId_04 + + + networkId_03 + nodeId_02 + + + topId_01 + + + + + ntElementId_19 + + + ntElementId_18 + + + \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/mutableNodesConfig.xml b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/mutableNodesConfig.xml new file mode 100644 index 0000000000..851022bb9f --- /dev/null +++ b/opendaylight/sal/yang-prototype/yang/yang-data-impl/src/test/resources/org/opendaylight/controller/yang/data/impl/mutableNodesConfig.xml @@ -0,0 +1,13 @@ + + + + + 1501 + Ethernet1/0 + + + 1500 + Ethernet0/0 + + + \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-util/src/main/java/org/opendaylight/controller/yang/data/util/AbstractNode.java b/opendaylight/sal/yang-prototype/yang/yang-data-util/src/main/java/org/opendaylight/controller/yang/data/util/AbstractNode.java index 250cd9d2a6..e6297f55b2 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-data-util/src/main/java/org/opendaylight/controller/yang/data/util/AbstractNode.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-util/src/main/java/org/opendaylight/controller/yang/data/util/AbstractNode.java @@ -9,6 +9,7 @@ package org.opendaylight.controller.yang.data.util; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.opendaylight.controller.yang.data.api.ModifyAction; import org.opendaylight.controller.yang.data.api.Node; public abstract class AbstractNode implements Node { @@ -28,4 +29,14 @@ public abstract class AbstractNode implements Node { public CompositeNode getParent() { return parent; } + + /* (non-Javadoc) + */ + /** + * @see org.opendaylight.controller.yang.data.api.NodeModification#getModificationAction() + */ + public ModifyAction getModificationAction() { + // TODO Auto-generated method stub + return null; + } } diff --git a/opendaylight/sal/yang-prototype/yang/yang-data-util/src/main/java/org/opendaylight/controller/yang/data/util/Nodes.java b/opendaylight/sal/yang-prototype/yang/yang-data-util/src/main/java/org/opendaylight/controller/yang/data/util/Nodes.java index 57feff1514..fca835f458 100644 --- a/opendaylight/sal/yang-prototype/yang/yang-data-util/src/main/java/org/opendaylight/controller/yang/data/util/Nodes.java +++ b/opendaylight/sal/yang-prototype/yang/yang-data-util/src/main/java/org/opendaylight/controller/yang/data/util/Nodes.java @@ -14,6 +14,9 @@ import java.util.Map; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; +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.SimpleNode; @@ -71,6 +74,16 @@ public class Nodes { return nodeMap; } + + /* (non-Javadoc) + * @see org.opendaylight.controller.yang.data.api.CompositeNode#asMutable() + */ + @Override + public MutableCompositeNode asMutable() { + // TODO Auto-generated method stub + return null; + } + } private static class SimpleNodeTO extends AbstractNode implements @@ -89,6 +102,15 @@ public class Nodes { return value; } + /* (non-Javadoc) + * @see org.opendaylight.controller.yang.data.api.SimpleNode#asMutable() + */ + @Override + public MutableSimpleNode asMutable() { + // TODO Auto-generated method stub + return null; + } + } } -- 2.36.6