+
+ @Test
+ public void getActionsTest() {
+ 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");
+ 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) {
+ QName qname = actionDefinition.getQName();
+ assertTrue(qnames.contains(qname));
+ qnames.remove(qname);
+ }
+ }
+
+ @Test
+ public void toActionRequestListTopLevelTest() {
+ 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");
+ 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");
+
+ NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
+ SchemaPath.create(true, actionResetQName), domDataTreeIdentifier, data);
+
+ Node childAction = checkBasePartOfActionRequest(actionRequest);
+
+ Node childServer = childAction.getFirstChild();
+ checkNode(childServer, "server", "server", qname.getNamespace().toString());
+
+ Node childName = childServer.getFirstChild();
+ checkNode(childName, "name", "name", qname.getNamespace().toString());
+
+ Node childTest = childName.getFirstChild();
+ assertEquals(childTest.getNodeValue(), "test");
+
+ checkAction(actionResetQName, childName.getNextSibling(), "reset-at", "reset-at", "now");
+ }
+
+ @Test
+ public void toActionRequestContainerTopLevelTest() {
+ QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
+ QName actionStartQName = QName.create(qname, "start");
+
+ List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(qname));
+ DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
+
+ NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
+ NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
+ SchemaPath.create(true, actionStartQName), domDataTreeIdentifier, payload);
+
+ Node childAction = checkBasePartOfActionRequest(actionRequest);
+
+ Node childDevice = childAction.getFirstChild();
+ checkNode(childDevice, "device", "device", qname.getNamespace().toString());
+
+ checkAction(actionStartQName, childDevice.getFirstChild(), "start-at", "start-at", "now");
+ }
+
+ @Test
+ public void toActionRequestContainerInContainerTest() {
+ QName boxOutQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
+ QName boxInQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-in");
+ QName actionOpenQName = QName.create(boxOutQName, "open");
+
+ List<PathArgument> nodeIdentifiers = new ArrayList<>();
+ nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
+ nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
+
+ DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
+
+ NormalizedNode<?, ?> payload = initInputAction(QName.create(boxOutQName, "start-at"), "now");
+ NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
+ SchemaPath.create(true, actionOpenQName), domDataTreeIdentifier, payload);
+
+ Node childAction = checkBasePartOfActionRequest(actionRequest);
+
+ Node childBoxOut = childAction.getFirstChild();
+ checkNode(childBoxOut, "box-out", "box-out", boxOutQName.getNamespace().toString());
+
+ Node childBoxIn = childBoxOut.getFirstChild();
+ checkNode(childBoxIn, "box-in", "box-in", boxOutQName.getNamespace().toString());
+
+ Node action = childBoxIn.getFirstChild();
+ 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 {
+ QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
+
+ NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
+ "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
+ + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
+ + "now"
+ + "</reset-finished-at>"
+ + "</rpc-reply>"));
+ DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(
+ SchemaPath.create(true, qname), message);
+ assertNotNull(actionResult);
+ ContainerNode containerNode = actionResult.getOutput().get();
+ assertNotNull(containerNode);
+ LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
+ assertEquals("now", leaf.getValue());
+ }
+
+ private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
+ final String inputNodeName, final String inputValue) {
+ checkNode(action, null, actionQname.getLocalName(), null);
+
+ Node childResetAt = action.getFirstChild();
+ checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
+
+ Node firstChild = childResetAt.getFirstChild();
+ assertEquals(firstChild.getNodeValue(), inputValue);
+ }
+
+ private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
+ Node baseRpc = actionRequest.getDocument().getFirstChild();
+ checkNode(baseRpc, "rpc", "rpc", NetconfMessageTransformUtil.NETCONF_QNAME.getNamespace().toString());
+ assertTrue(baseRpc.getLocalName().equals("rpc"));
+ assertTrue(baseRpc.getNodeName().equals("rpc"));
+
+ Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
+ assertNotNull(messageId);
+ assertTrue(messageId.getNodeValue().contains("m-"));
+
+ Node childAction = baseRpc.getFirstChild();
+ checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
+ return childAction;
+ }
+
+ private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
+ YangInstanceIdentifier yangInstanceIdentifier =
+ YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
+ DOMDataTreeIdentifier domDataTreeIdentifier =
+ new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
+ yangInstanceIdentifier);
+ return domDataTreeIdentifier;
+ }
+
+ private static ContainerNode initInputAction(final QName qname, final String value) {
+ ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
+ DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
+ NodeIdentifier.create(qname)).withValue(value).build();
+ ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
+ QName.create(qname, "input"))).withChild(build).build();
+ 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);
+ assertEquals(childServer.getLocalName(), expectedLocalName);
+ assertEquals(childServer.getNodeName(), expectedNodeName);
+ assertEquals(childServer.getNamespaceURI(), expectedNamespace);
+ }
+
+ private static SchemaContext getActionSchema() {
+ return YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
+ "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang");
+ }
+
+ private static NetconfMessageTransformer getActionMessageTransformer() {
+ return new NetconfMessageTransformer(getActionSchema(), true);
+ }