2 * Copyright (c) 2016 Cisco Systems, Inc. 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.yanglib.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.clearInvocations;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyNoMoreInteractions;
17 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs.Revision;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
42 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
43 import org.opendaylight.yangtools.yang.model.api.source.YinSourceRepresentation;
44 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
45 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
46 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSource;
47 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
49 @RunWith(MockitoJUnitRunner.StrictStubs.class)
50 public class YangLibProviderTest {
52 private DataBroker dataBroker;
54 private WriteTransaction writeTransaction;
56 private YangLibProvider yangLibProvider;
60 doReturn(emptyFluentFuture()).when(writeTransaction).commit();
61 doReturn(writeTransaction).when(dataBroker).newWriteOnlyTransaction();
63 yangLibProvider = new YangLibProvider(dataBroker, new DefaultYangParserFactory(),
64 YangLibProviderTest.class.getResource("/model").getPath(), "www.fake.com", Uint32.valueOf(300));
68 public void testSchemaSourceRegistered() {
69 // test that initial models are registered
70 verify(dataBroker).newWriteOnlyTransaction();
71 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
72 eq(InstanceIdentifier.create(ModulesState.class)),
73 eq(new ModulesStateBuilder()
74 .setModule(BindingMap.of(new ModuleBuilder()
75 .setName(new YangIdentifier("model1"))
76 .setRevision(new Revision(new RevisionIdentifier("2023-02-21")))
77 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/model1/2023-02-21"))
78 .build(), new ModuleBuilder()
79 .setName(new YangIdentifier("model2"))
80 .setRevision(LegacyRevisionUtils.emptyRevision())
81 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/model2"))
84 verify(writeTransaction).commit();
88 public void testFilteringEmptySchemaSourceRegistered() {
89 clearInvocations(dataBroker, writeTransaction);
91 // test empty list of schema sources registered
92 yangLibProvider.schemaSourceRegistered(List.of());
93 // expected behavior is to do nothing
94 verifyNoMoreInteractions(dataBroker, writeTransaction);
98 public void testFilteringNonYangSchemaSourceRegistered() {
99 clearInvocations(dataBroker, writeTransaction);
101 // test list of non yang schema sources registered
102 yangLibProvider.schemaSourceRegistered(List.of(
103 PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
104 YinSourceRepresentation.class, Costs.IMMEDIATE.getValue()),
105 PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"),
106 YangIRSource.class, Costs.IMMEDIATE.getValue())));
108 // expected behavior is to do nothing
109 verifyNoMoreInteractions(dataBroker, writeTransaction);
113 public void testSchemaSourceWithRevisionUnregistered() {
114 clearInvocations(dataBroker, writeTransaction);
116 // try to unregister YANG source with revision
117 final var schemaSourceWithRevision = PotentialSchemaSource.create(
118 new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"),
119 YangTextSource.class, Costs.LOCAL_IO.getValue());
120 yangLibProvider.schemaSourceUnregistered(schemaSourceWithRevision);
122 // source is unregistered
123 verify(dataBroker).newWriteOnlyTransaction();
124 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
125 eq(InstanceIdentifier.create(ModulesState.class)
127 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
128 new Revision(new RevisionIdentifier("2016-04-28"))))));
129 verify(writeTransaction).commit();
133 public void testSchemaSourceWithoutRevisionUnregistered() {
134 clearInvocations(dataBroker, writeTransaction);
136 // try to unregister YANG source without revision
137 final var schemaSourceWithoutRevision = PotentialSchemaSource.create(
138 new SourceIdentifier("unregistered-yang-schema-without-revision"), YangTextSource.class,
139 Costs.LOCAL_IO.getValue());
140 yangLibProvider.schemaSourceUnregistered(schemaSourceWithoutRevision);
142 // source is unregistered
143 verify(dataBroker).newWriteOnlyTransaction();
144 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
145 eq(InstanceIdentifier.create(ModulesState.class)
147 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
148 LegacyRevisionUtils.emptyRevision()))));
149 verify(writeTransaction).commit();
153 public void testNonYangSchemaSourceUnregistered() {
154 clearInvocations(dataBroker, writeTransaction);
156 // try to unregister non-YANG source
157 final var nonYangSources = PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
158 YinSourceRepresentation.class, Costs.IMMEDIATE.getValue());
159 yangLibProvider.schemaSourceUnregistered(nonYangSources);
161 // expected behaviour is to do nothing if non yang based source is unregistered
162 verifyNoMoreInteractions(dataBroker, writeTransaction);
166 public void testGetSchemaWithRevision() {
167 final var modelWithRevision = yangLibProvider.getSchema("model1", "2023-02-21");
168 assertNotNull(modelWithRevision);
171 namespace "model:with:revision";
174 revision 2023-02-21 {
185 """, modelWithRevision);
189 public void testGetSchemaWithoutRevision() {
190 final var modelWithoutRevision = yangLibProvider.getSchema("model2");
191 assertNotNull(modelWithoutRevision);
194 namespace "model:with:no:revision";
203 """, modelWithoutRevision);