Convert anyxml nodes lazily
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / schema / mapping / NetconfMessageTransformerTest.java
index a91458077f24a0321b4742a8668039db7acca65f..42812abe38440e4d257f6282457b1743d76e0367 100644 (file)
@@ -33,6 +33,7 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
@@ -57,6 +58,7 @@ import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
+import org.opendaylight.netconf.util.NetconfUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
@@ -75,6 +77,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
@@ -89,6 +92,9 @@ public class NetconfMessageTransformerTest {
     private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
     private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
 
+    private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
+    private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
+
     private NetconfMessageTransformer actionNetconfMessageTransformer;
     private NetconfMessageTransformer netconfMessageTransformer;
     private SchemaContext schema;
@@ -223,9 +229,13 @@ public class NetconfMessageTransformerTest {
         final MapEntryNode schemaNode =
                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
 
-        final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult
+        final AnyXmlNode data = (AnyXmlNode) ((ContainerNode) compositeNodeRpcResult
                 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
-        final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
+
+        NormalizedNodeResult nodeResult =
+                NetconfUtil.transformDOMSourceToNormalizedNode(schema, data.getValue());
+        ContainerNode result = (ContainerNode) nodeResult.getResult();
+        final ContainerNode state = (ContainerNode) result.getChild(toId(NetconfState.QNAME)).get();
         final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
         final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
         assertEquals(1, Iterables.size(schemaParent.getValue()));
@@ -399,7 +409,9 @@ public class NetconfMessageTransformerTest {
         QName reset = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
         QName start = QName.create(reset, "start");
         QName open = QName.create(start, "open");
-        Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open));
+        QName enable = QName.create(open, "enable");
+        QName kill = QName.create(URN_EXAMPLE_SERVER_FARM_2, REVISION_EXAMPLE_SERVER_FARM_2, "kill");
+        Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open, enable, kill));
         Set<ActionDefinition> actions = actionNetconfMessageTransformer.getActions();
         assertTrue(!actions.isEmpty());
         for (ActionDefinition actionDefinition : actions) {
@@ -414,9 +426,9 @@ public class NetconfMessageTransformerTest {
         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
         QName nameQname = QName.create(qname, "name");
         QName actionResetQName = QName.create(qname, "reset");
-
-        Set<PathArgument> nodeIdentifiers =
-                Collections.singleton(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
+        List<PathArgument> nodeIdentifiers = new ArrayList<>();
+        nodeIdentifiers.add(new NodeIdentifier(qname));
+        nodeIdentifiers.add(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
 
         ContainerNode data = initInputAction(QName.create(qname, "reset-at"), "now");
@@ -443,7 +455,7 @@ public class NetconfMessageTransformerTest {
         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
         QName actionStartQName = QName.create(qname, "start");
 
-        Set<PathArgument> nodeIdentifiers = Collections.singleton(NodeIdentifier.create(qname));
+        List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(qname));
         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
 
         NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
@@ -464,7 +476,7 @@ public class NetconfMessageTransformerTest {
         QName boxInQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-in");
         QName actionOpenQName = QName.create(boxOutQName, "open");
 
-        Set<PathArgument> nodeIdentifiers = new HashSet<>();
+        List<PathArgument> nodeIdentifiers = new ArrayList<>();
         nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
         nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
 
@@ -486,6 +498,97 @@ public class NetconfMessageTransformerTest {
         checkNode(action, null, actionOpenQName.getLocalName(), null);
     }
 
+    @Test
+    public void toActionRequestListInContainerTest() {
+        QName qnameDevice = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
+        QName qnameInterface = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "interface");
+        QName nameQname = QName.create(qnameInterface, "name");
+        QName actionEnableQName = QName.create(qnameInterface, "enable");
+
+        List<PathArgument> nodeIdentifiers = new ArrayList<>();
+        nodeIdentifiers.add(NodeIdentifier.create(qnameDevice));
+        nodeIdentifiers.add(NodeIdentifier.create(qnameInterface));
+        nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameInterface, nameQname, "test"));
+
+        DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
+
+        NormalizedNode<?, ?> payload = initEmptyInputAction(qnameInterface);
+        NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
+                SchemaPath.create(true, actionEnableQName), domDataTreeIdentifier, payload);
+
+        Node childAction = checkBasePartOfActionRequest(actionRequest);
+
+        Node childDevice = childAction.getFirstChild();
+        checkNode(childDevice, "device", "device", qnameDevice.getNamespace().toString());
+
+        Node childInterface = childDevice.getFirstChild();
+        checkNode(childInterface, "interface", "interface", qnameInterface.getNamespace().toString());
+
+        Node childName = childInterface.getFirstChild();
+        checkNode(childName, "name", "name", nameQname.getNamespace().toString());
+
+        Node childTest = childName.getFirstChild();
+        assertEquals(childTest.getNodeValue(), "test");
+
+        Node action = childInterface.getLastChild();
+        checkNode(action, null, actionEnableQName.getLocalName(), null);
+    }
+
+    @Test
+    public void toActionRequestListInContainerAugmentedIntoListTest() {
+        QName qnameServer = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
+        QName serverNameQname = QName.create(qnameServer, "name");
+        QName qnameAppliactions = QName.create(URN_EXAMPLE_SERVER_FARM_2,
+                REVISION_EXAMPLE_SERVER_FARM_2, "applications");
+        QName qnameAppliaction = QName.create(URN_EXAMPLE_SERVER_FARM_2,
+                REVISION_EXAMPLE_SERVER_FARM_2, "application");
+        QName applicationNameQname = QName.create(qnameAppliaction, "name");
+        QName actionKillQName = QName.create(qnameAppliaction, "kill");
+
+        List<PathArgument> nodeIdentifiers = new ArrayList<>();
+        nodeIdentifiers.add(NodeIdentifier.create(qnameServer));
+        nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameServer, serverNameQname, "testServer"));
+        nodeIdentifiers.add(new YangInstanceIdentifier
+                .AugmentationIdentifier(Collections.singleton(qnameAppliactions)));
+        nodeIdentifiers.add(NodeIdentifier.create(qnameAppliactions));
+        nodeIdentifiers.add(NodeIdentifier.create(qnameAppliaction));
+        nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameAppliaction,
+                applicationNameQname, "testApplication"));
+
+        DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
+
+        NormalizedNode<?, ?> payload = initEmptyInputAction(qnameAppliaction);
+        NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
+                SchemaPath.create(true, actionKillQName), domDataTreeIdentifier, payload);
+
+        Node childAction = checkBasePartOfActionRequest(actionRequest);
+
+        Node childServer = childAction.getFirstChild();
+        checkNode(childServer, "server", "server", qnameServer.getNamespace().toString());
+
+        Node childServerName = childServer.getFirstChild();
+        checkNode(childServerName, "name", "name", serverNameQname.getNamespace().toString());
+
+        Node childServerNameTest = childServerName.getFirstChild();
+        assertEquals(childServerNameTest.getNodeValue(), "testServer");
+
+        Node childApplications = childServer.getLastChild();
+        checkNode(childApplications, "applications", "applications", qnameAppliactions.getNamespace().toString());
+
+        Node childApplication = childApplications.getFirstChild();
+        checkNode(childApplication, "application", "application", qnameAppliaction.getNamespace().toString());
+
+        Node childApplicationName = childApplication.getFirstChild();
+        checkNode(childApplicationName, "name", "name", applicationNameQname.getNamespace().toString());
+
+        Node childApplicationNameTest = childApplicationName.getFirstChild();
+        assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
+
+        Node childKillAction = childApplication.getLastChild();
+        checkNode(childApplication, "application", "application", qnameAppliaction.getNamespace().toString());
+        checkNode(childKillAction, null, actionKillQName.getLocalName(), null);
+    }
+
     @SuppressWarnings({ "rawtypes", "unchecked" })
     @Test
     public void toActionResultTest() throws Exception {
@@ -532,7 +635,7 @@ public class NetconfMessageTransformerTest {
         return childAction;
     }
 
-    private static DOMDataTreeIdentifier prepareDataTreeId(final Set<PathArgument> nodeIdentifiers) {
+    private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
         YangInstanceIdentifier yangInstanceIdentifier =
                 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
         DOMDataTreeIdentifier domDataTreeIdentifier =
@@ -550,6 +653,11 @@ public class NetconfMessageTransformerTest {
         return data;
     }
 
+    private static ContainerNode initEmptyInputAction(final QName qname) {
+        return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
+                QName.create(qname, "input"))).build();
+    }
+
     private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
             final String expectedNamespace) {
         assertNotNull(childServer);
@@ -559,7 +667,8 @@ public class NetconfMessageTransformerTest {
     }
 
     private static SchemaContext getActionSchema() {
-        return YangParserTestUtils.parseYangResource("/schemas/example-server-farm.yang");
+        return YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
+                "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang");
     }
 
     private static NetconfMessageTransformer getActionMessageTransformer() {