2 * Copyright (c) 2023 PANTHEON.tech s.r.o. and others. All rights reserved.
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 http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.yanglib.writer;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.never;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.verifyNoInteractions;
18 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
19 import static org.opendaylight.netconf.yanglib.writer.YangLibraryContentBuilderUtil.DEFAULT_MODULE_SET_NAME;
20 import static org.opendaylight.netconf.yanglib.writer.YangLibraryContentBuilderUtil.DEFAULT_SCHEMA_NAME;
21 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module.ConformanceType.Implement;
22 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module.ConformanceType.Import;
23 import static org.opendaylight.yangtools.yang.test.util.YangParserTestUtils.parseYangResources;
26 import java.util.stream.Stream;
27 import org.junit.jupiter.api.DisplayName;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.junit.jupiter.params.ParameterizedTest;
31 import org.junit.jupiter.params.provider.Arguments;
32 import org.junit.jupiter.params.provider.MethodSource;
33 import org.junit.jupiter.params.provider.ValueSource;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Captor;
36 import org.mockito.Mock;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.opendaylight.mdsal.binding.api.DataBroker;
39 import org.opendaylight.mdsal.binding.api.TransactionChain;
40 import org.opendaylight.mdsal.binding.api.WriteTransaction;
41 import org.opendaylight.mdsal.common.api.CommitInfo;
42 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
43 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.datastores.rev180214.Operational;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionUtils;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryBuilder;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.DeviationBuilder;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.ModuleBuilder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.module.SubmoduleBuilder;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.DatastoreBuilder;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.ModuleSetBuilder;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.SchemaBuilder;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
61 import org.opendaylight.yangtools.concepts.Registration;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
64 import org.opendaylight.yangtools.yang.common.Empty;
66 @ExtendWith(MockitoExtension.class)
67 class YangLibraryWriterTest {
68 private static final YangLibrarySchemaSourceUrlProvider URL_PROVIDER = (moduleSetName, moduleName, revision) ->
69 Set.of(new Uri("/url/to/" + moduleName + (revision == null ? "" : "/" + revision)));
70 private static final InstanceIdentifier<YangLibrary> YANG_LIBRARY_PATH =
71 InstanceIdentifier.create(YangLibrary.class);
73 private static final InstanceIdentifier<ModulesState> MODULES_STATE_PATH =
74 InstanceIdentifier.create(ModulesState.class);
75 private static final boolean WITH_LEGACY = true;
76 private static final boolean NO_LEGACY = false;
77 private static final boolean WITH_URLS = true;
78 private static final boolean NO_URLS = false;
81 private DOMSchemaService schemaService;
83 private DataBroker dataBroker;
85 private TransactionChain transactionChain;
87 private WriteTransaction writeTransaction;
89 private Registration registration;
91 private ArgumentCaptor<YangLibrary> yangLibraryCaptor;
93 private ArgumentCaptor<ModulesState> modulesStateCaptor;
94 private YangLibraryWriter writer;
97 @DisplayName("No update bc context has no ietf-yang-library")
99 writer = new YangLibraryWriter(new FixedDOMSchemaService(parseYangResources(YangLibraryWriterTest.class,
100 "/test-module.yang", "/test-submodule.yang")), dataBroker, NO_LEGACY,
101 YangLibraryWriterSingleton.emptyProvider());
102 verifyNoInteractions(dataBroker);
105 @ParameterizedTest(name = "Write data -- with URLs: {0}, include legacy: {1}")
107 void writeContent(final boolean withUrls, final boolean writeLegacy, final YangLibrary expectedData,
108 final ModulesState expectedLegacyData) {
109 doReturn(transactionChain).when(dataBroker).createMergingTransactionChain();
110 doReturn(writeTransaction).when(transactionChain).newWriteOnlyTransaction();
111 doReturn(CommitInfo.emptyFluentFuture()).when(writeTransaction).commit();
113 writer = new YangLibraryWriter(new FixedDOMSchemaService(parseYangResources(YangLibraryWriterTest.class,
114 "/test-module.yang", "/test-submodule.yang", "/test-more.yang", "/ietf-yang-library@2019-01-04.yang",
115 "/ietf-datastores@2018-02-14.yang", "/ietf-yang-types.yang", "/ietf-inet-types.yang")), dataBroker,
116 writeLegacy, withUrls ? URL_PROVIDER : YangLibraryWriterSingleton.emptyProvider());
118 verify(writeTransaction).put(eq(OPERATIONAL), eq(YANG_LIBRARY_PATH), yangLibraryCaptor.capture());
119 assertEquals(expectedData, yangLibraryCaptor.getValue());
121 verify(writeTransaction).put(eq(OPERATIONAL), eq(MODULES_STATE_PATH), modulesStateCaptor.capture());
122 assertEquals(expectedLegacyData, modulesStateCaptor.getValue());
124 verify(writeTransaction, never()).put(eq(OPERATIONAL), eq(MODULES_STATE_PATH), any());
126 verify(writeTransaction).commit();
129 private static Stream<Arguments> writeContent() {
131 Arguments.of(NO_URLS, NO_LEGACY, buildYangLibrary(NO_URLS), null),
132 Arguments.of(NO_URLS, WITH_LEGACY, buildYangLibrary(NO_URLS), buildModulesState(NO_URLS)),
133 Arguments.of(WITH_URLS, NO_LEGACY, buildYangLibrary(WITH_URLS), null),
134 Arguments.of(WITH_URLS, WITH_LEGACY, buildYangLibrary(WITH_URLS), buildModulesState(WITH_URLS)));
137 @ParameterizedTest(name = "Clear data on close -- include legacy: {0}")
138 @ValueSource(booleans = { false, true })
139 void clearOnClose(final boolean writeLegacy) throws Exception {
140 doReturn(transactionChain).when(dataBroker).createMergingTransactionChain();
141 doReturn(writeTransaction).when(transactionChain).newWriteOnlyTransaction();
142 doReturn(Empty.immediateFuture()).when(transactionChain).future();
143 doReturn(CommitInfo.emptyFluentFuture()).when(writeTransaction).commit();
144 doReturn(registration).when(schemaService).registerSchemaContextListener(any());
146 assertNotNull(new YangLibraryWriter(schemaService, dataBroker, writeLegacy,
147 YangLibraryWriterSingleton.emptyProvider()).shutdown());
148 verify(writeTransaction).delete(OPERATIONAL, YANG_LIBRARY_PATH);
149 verify(writeTransaction).delete(OPERATIONAL, MODULES_STATE_PATH);
150 verify(writeTransaction).commit();
153 private static YangLibrary buildYangLibrary(final boolean withUrls) {
154 return new YangLibraryBuilder()
155 .setModuleSet(BindingMap.of(
156 new ModuleSetBuilder()
157 .setName(DEFAULT_MODULE_SET_NAME)
158 .setModule(BindingMap.of(
159 new ModuleBuilder().setName(new YangIdentifier("test-module_2013-07-22"))
160 .setNamespace(new Uri("test:namespace"))
161 .setRevision(new RevisionIdentifier("2013-07-22"))
162 .setLocation(withUrls ? Set.of(new Uri("/url/to/test-module/2013-07-22")) : null)
163 .setSubmodule(BindingMap.of(
164 new SubmoduleBuilder()
165 .setName(new YangIdentifier("test-submodule"))
166 .setRevision(RevisionUtils.emptyRevision().getRevisionIdentifier())
167 .setLocation(withUrls ? Set.of(new Uri("/url/to/test-submodule")) : null)
169 .setDeviation(Set.of(new YangIdentifier("test-more_2023-07-25")))
171 new ModuleBuilder().setName(new YangIdentifier("test-more_2023-07-25"))
172 .setNamespace(new Uri("test:more"))
173 .setRevision(new RevisionIdentifier("2023-07-25"))
174 .setLocation(withUrls ? Set.of(new Uri("/url/to/test-more/2023-07-25")) : null)
176 new YangIdentifier("first-feature"), new YangIdentifier("second-feature")))
178 new ModuleBuilder().setName(new YangIdentifier("ietf-yang-library_2019-01-04"))
179 .setNamespace(new Uri("urn:ietf:params:xml:ns:yang:ietf-yang-library"))
180 .setRevision(new RevisionIdentifier("2019-01-04"))
181 .setLocation(withUrls ? Set.of(new Uri("/url/to/ietf-yang-library/2019-01-04")) : null)
183 new ModuleBuilder().setName(new YangIdentifier("ietf-datastores_2018-02-14"))
184 .setNamespace(new Uri("urn:ietf:params:xml:ns:yang:ietf-datastores"))
185 .setRevision(new RevisionIdentifier("2018-02-14"))
186 .setLocation(withUrls ? Set.of(new Uri("/url/to/ietf-datastores/2018-02-14")) : null)
188 new ModuleBuilder().setName(new YangIdentifier("ietf-inet-types_2010-09-24"))
189 .setNamespace(new Uri("urn:ietf:params:xml:ns:yang:ietf-inet-types"))
190 .setRevision(new RevisionIdentifier("2010-09-24"))
191 .setLocation(withUrls ? Set.of(new Uri("/url/to/ietf-inet-types/2010-09-24")) : null)
193 new ModuleBuilder().setName(new YangIdentifier("ietf-yang-types_2010-09-24"))
194 .setNamespace(new Uri("urn:ietf:params:xml:ns:yang:ietf-yang-types"))
195 .setRevision(new RevisionIdentifier("2010-09-24"))
196 .setLocation(withUrls ? Set.of(new Uri("/url/to/ietf-yang-types/2010-09-24")) : null)
199 .setSchema(BindingMap.of(new SchemaBuilder()
200 .setName(DEFAULT_SCHEMA_NAME)
201 .setModuleSet(Set.of(DEFAULT_MODULE_SET_NAME))
203 .setDatastore(BindingMap.of(
204 new DatastoreBuilder().setName(Operational.VALUE)
205 .setSchema(DEFAULT_SCHEMA_NAME)
212 private static ModulesState buildModulesState(final boolean withUrls) {
213 return new ModulesStateBuilder()
214 .setModule(BindingMap.of(
215 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104
216 .module.list.ModuleBuilder()
217 .setName(new YangIdentifier("test-module_2013-07-22"))
218 .setNamespace(new Uri("test:namespace"))
219 .setRevision(new CommonLeafs.Revision(new RevisionIdentifier("2013-07-22")))
220 .setSchema(withUrls ? new Uri("/url/to/test-module/2013-07-22") : null)
221 .setSubmodule(BindingMap.of(
222 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104
223 .module.list.module.SubmoduleBuilder()
224 .setName(new YangIdentifier("test-submodule"))
225 .setRevision(LegacyRevisionUtils.emptyRevision())
226 .setSchema(withUrls ? new Uri("/url/to/test-submodule") : null)
228 .setDeviation(BindingMap.of(
229 new DeviationBuilder()
230 .setName(new YangIdentifier("test-more_2023-07-25"))
231 .setRevision(new CommonLeafs.Revision(new RevisionIdentifier("2023-07-25")))
234 .setConformanceType(Import)
236 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104
237 .module.list.ModuleBuilder()
238 .setName(new YangIdentifier("test-more_2023-07-25"))
239 .setNamespace(new Uri("test:more"))
240 .setRevision(new CommonLeafs.Revision(new RevisionIdentifier("2023-07-25")))
241 .setSchema(withUrls ? new Uri("/url/to/test-more/2023-07-25") : null)
243 new YangIdentifier("first-feature"), new YangIdentifier("second-feature")))
244 .setConformanceType(Implement)
246 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104
247 .module.list.ModuleBuilder()
248 .setName(new YangIdentifier("ietf-yang-library_2019-01-04"))
249 .setNamespace(new Uri("urn:ietf:params:xml:ns:yang:ietf-yang-library"))
250 .setRevision(new CommonLeafs.Revision(new RevisionIdentifier("2019-01-04")))
251 .setSchema(withUrls ? new Uri("/url/to/ietf-yang-library/2019-01-04") : null)
252 .setConformanceType(Import)
254 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104
255 .module.list.ModuleBuilder()
256 .setName(new YangIdentifier("ietf-datastores_2018-02-14"))
257 .setNamespace(new Uri("urn:ietf:params:xml:ns:yang:ietf-datastores"))
258 .setRevision(new CommonLeafs.Revision(new RevisionIdentifier("2018-02-14")))
259 .setSchema(withUrls ? new Uri("/url/to/ietf-datastores/2018-02-14") : null)
260 .setConformanceType(Import)
262 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104
263 .module.list.ModuleBuilder()
264 .setName(new YangIdentifier("ietf-inet-types_2010-09-24"))
265 .setNamespace(new Uri("urn:ietf:params:xml:ns:yang:ietf-inet-types"))
266 .setRevision(new CommonLeafs.Revision(new RevisionIdentifier("2010-09-24")))
267 .setSchema(withUrls ? new Uri("/url/to/ietf-inet-types/2010-09-24") : null)
268 .setConformanceType(Import)
270 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104
271 .module.list.ModuleBuilder()
272 .setName(new YangIdentifier("ietf-yang-types_2010-09-24"))
273 .setNamespace(new Uri("urn:ietf:params:xml:ns:yang:ietf-yang-types"))
274 .setRevision(new CommonLeafs.Revision(new RevisionIdentifier("2010-09-24")))
275 .setSchema(withUrls ? new Uri("/url/to/ietf-yang-types/2010-09-24") : null)
276 .setConformanceType(Import)