|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 PANTHEON.tech, s.r.o. and others. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v1.0 which accompanies this distribution, |
| 6 | + * and is available at https://www.eclipse.org/legal/epl-v10.html |
| 7 | + */ |
| 8 | +package io.lighty.modules.northbound.netty.restconf.community.impl.tests; |
| 9 | + |
| 10 | +import static org.testng.Assert.assertEquals; |
| 11 | +import static org.testng.Assert.assertNotNull; |
| 12 | +import static org.testng.AssertJUnit.assertTrue; |
| 13 | + |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; |
| 18 | +import org.opendaylight.netconf.databind.DatabindContext; |
| 19 | +import org.opendaylight.netconf.databind.DatabindPath; |
| 20 | +import org.opendaylight.restconf.mdsal.spi.data.MdsalRestconfStrategy; |
| 21 | +import org.opendaylight.restconf.server.api.DataYangPatchResult; |
| 22 | +import org.opendaylight.restconf.server.api.PatchContext; |
| 23 | +import org.opendaylight.restconf.server.api.PatchEntity; |
| 24 | +import org.opendaylight.restconf.server.api.testlib.CompletingServerRequest; |
| 25 | +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.yang.patch.Edit; |
| 26 | +import org.opendaylight.yangtools.yang.common.QName; |
| 27 | +import org.opendaylight.yangtools.yang.common.Revision; |
| 28 | +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; |
| 29 | +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; |
| 30 | +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; |
| 31 | +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; |
| 32 | +import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; |
| 33 | +import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; |
| 34 | +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; |
| 35 | +import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode; |
| 36 | +import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes; |
| 37 | +import org.testng.annotations.Test; |
| 38 | + |
| 39 | +public class YangPatchTest extends NettyRestConfTestBase { |
| 40 | + |
| 41 | + private static final QName BASE_Q_NAME = QName.create("instance:identifier:patch:module", |
| 42 | + "instance-identifier-patch-module", Revision.of("2015-11-21")).intern(); |
| 43 | + private static final QName EXAMPLE_LIST = QName.create(BASE_Q_NAME, "my-list1").intern(); |
| 44 | + private static final QName EXAMPLE_LIST_NAME = QName.create(BASE_Q_NAME, "name").intern(); |
| 45 | + private static final QName MY_LEAF_11 = QName.create(EXAMPLE_LIST, "my-leaf11").intern(); |
| 46 | + private static final QName MY_LEAF_12 = QName.create(EXAMPLE_LIST, "my-leaf12").intern(); |
| 47 | + private static final QName CONTAINER_ID = QName.create(EXAMPLE_LIST, "patch-cont").intern(); |
| 48 | + private static final String MY_LIST_1_A = "my-list1 - A"; |
| 49 | + private static final String I_AM_LEAF_11_0 = "I am leaf11-0"; |
| 50 | + private static final String I_AM_LEAF_12_1 = "I am leaf12-1"; |
| 51 | + |
| 52 | + @Test |
| 53 | + public void patchDataReplaceTest() throws Exception { |
| 54 | + assertNotNull(getLightyController()); |
| 55 | + assertNotNull(getNettyRestConf()); |
| 56 | + |
| 57 | + final ContainerNode patchContainerNode = getContainerWithData(); |
| 58 | + final YangInstanceIdentifier targetNodeMerge = YangInstanceIdentifier.builder() |
| 59 | + .node(CONTAINER_ID) |
| 60 | + .build(); |
| 61 | + |
| 62 | + final DatabindContext databindContext = DatabindContext.ofModel(getLightyController() |
| 63 | + .getServices().getDOMSchemaService().getGlobalContext()); |
| 64 | + |
| 65 | + final PatchEntity entityReplace = new PatchEntity( |
| 66 | + "edit1", Edit.Operation.Replace, getPath(targetNodeMerge, databindContext), patchContainerNode); |
| 67 | + final PatchContext patchContext = new PatchContext("test-patch", List.of(entityReplace)); |
| 68 | + |
| 69 | + final var strategy = new MdsalRestconfStrategy(DatabindContext.ofModel(getLightyController() |
| 70 | + .getServices().getDOMSchemaService().getGlobalContext()), getLightyController().getServices() |
| 71 | + .getClusteredDOMDataBroker()); |
| 72 | + |
| 73 | + final CompletingServerRequest<DataYangPatchResult> dataYangPatchRequest = new CompletingServerRequest<>(); |
| 74 | + |
| 75 | + strategy.patchData(dataYangPatchRequest, new DatabindPath.Data(DatabindContext.ofModel(getLightyController() |
| 76 | + .getServices().getDOMSchemaService().getGlobalContext())) ,patchContext); |
| 77 | + assertTrue(dataYangPatchRequest.getResult().status().ok()); |
| 78 | + |
| 79 | + final ContainerNode response = (ContainerNode) getLightyController().getServices().getClusteredDOMDataBroker() |
| 80 | + .newReadOnlyTransaction() |
| 81 | + .read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of()) |
| 82 | + .get(5000, TimeUnit.MILLISECONDS).orElseThrow(); |
| 83 | + final DataContainerChild bodyOfResponse = response.body().iterator().next(); |
| 84 | + assertEquals(bodyOfResponse, getContainerWithData()); |
| 85 | + } |
| 86 | + |
| 87 | + private static ContainerNode getContainerWithData() { |
| 88 | + final LeafNode<?> nameLeafA = ImmutableNodes.newLeafBuilder() |
| 89 | + .withNodeIdentifier(NodeIdentifier.create(EXAMPLE_LIST_NAME)) |
| 90 | + .withValue(MY_LIST_1_A) |
| 91 | + .build(); |
| 92 | + final LeafNode<?> buildLeaf1 = ImmutableNodes.newLeafBuilder() |
| 93 | + .withNodeIdentifier(NodeIdentifier.create(MY_LEAF_11)) |
| 94 | + .withValue(I_AM_LEAF_11_0) |
| 95 | + .build(); |
| 96 | + final LeafNode<?> buildLeaf2 = ImmutableNodes.newLeafBuilder() |
| 97 | + .withNodeIdentifier(NodeIdentifier.create(MY_LEAF_12)) |
| 98 | + .withValue(I_AM_LEAF_12_1) |
| 99 | + .build(); |
| 100 | + final MapEntryNode mapEntryNode = ImmutableNodes.newMapEntryBuilder() |
| 101 | + .withNodeIdentifier(NodeIdentifierWithPredicates.of(EXAMPLE_LIST)) |
| 102 | + .withValue(List.of(nameLeafA, buildLeaf1, buildLeaf2)) |
| 103 | + .build(); |
| 104 | + final SystemMapNode myList = ImmutableNodes.newSystemMapBuilder() |
| 105 | + .withNodeIdentifier(NodeIdentifier.create(EXAMPLE_LIST)) |
| 106 | + .withValue(Collections.singletonList(mapEntryNode)) |
| 107 | + .build(); |
| 108 | + return ImmutableNodes.newContainerBuilder() |
| 109 | + .withNodeIdentifier(NodeIdentifier.create(CONTAINER_ID)) |
| 110 | + .withValue(Collections.singletonList(myList)) |
| 111 | + .build(); |
| 112 | + } |
| 113 | + |
| 114 | + private DatabindPath.Data getPath(final YangInstanceIdentifier path, final DatabindContext databindContext) { |
| 115 | + final var childAndStack = new DatabindPath.Data( |
| 116 | + databindContext).databind().schemaTree().enterPath(path).orElseThrow(); |
| 117 | + return new DatabindPath.Data(databindContext, childAndStack.stack().toInference(), path, childAndStack.node()); |
| 118 | + } |
| 119 | +} |
0 commit comments