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.mockito.ArgumentMatchers.eq;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.verifyNoMoreInteractions;
14 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
23 import org.apache.commons.io.FileUtils;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.opendaylight.mdsal.binding.api.DataBroker;
32 import org.opendaylight.mdsal.binding.api.WriteTransaction;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs.Revision;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfigBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.Uint32;
48 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
49 import org.opendaylight.yangtools.yang.model.repo.api.YangIRSchemaSource;
50 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
51 import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
52 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
53 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
54 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
56 @RunWith(MockitoJUnitRunner.StrictStubs.class)
57 public class YangLibProviderTest {
58 private static final File CACHE_DIR = new File("target/yanglib");
61 private DataBroker dataBroker;
64 private WriteTransaction writeTransaction;
66 private YangLibProvider yangLibProvider;
69 public static void staticSetup() {
70 if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
71 throw new RuntimeException("Failed to create " + CACHE_DIR);
76 public static void staticCleanup() {
77 FileUtils.deleteQuietly(CACHE_DIR);
83 if (CACHE_DIR.exists()) {
84 FileUtils.cleanDirectory(CACHE_DIR);
86 } catch (IOException e) {
90 doReturn(emptyFluentFuture()).when(writeTransaction).commit();
91 doReturn(writeTransaction).when(dataBroker).newWriteOnlyTransaction();
93 final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
94 .setBindingPort(Uint32.valueOf(300)).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
95 yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new DefaultYangParserFactory());
99 public void testSchemaSourceRegistered() {
100 yangLibProvider.init();
102 List<PotentialSchemaSource<?>> list = new ArrayList<>();
104 PotentialSchemaSource.create(new SourceIdentifier("no-revision"),
105 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
108 PotentialSchemaSource.create(new SourceIdentifier("with-revision", "2016-04-28"),
109 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
111 yangLibProvider.schemaSourceRegistered(list);
113 Map<ModuleKey, Module> newModulesList = new HashMap<>();
115 Module newModule = new ModuleBuilder()
116 .setName(new YangIdentifier("no-revision"))
117 .setRevision(LegacyRevisionUtils.emptyRevision())
118 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
121 newModulesList.put(newModule.key(), newModule);
123 newModule = new ModuleBuilder()
124 .setName(new YangIdentifier("with-revision"))
125 .setRevision(new Revision(new RevisionIdentifier("2016-04-28")))
126 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
129 newModulesList.put(newModule.key(), newModule);
131 verify(dataBroker).newWriteOnlyTransaction();
132 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
133 eq(InstanceIdentifier.create(ModulesState.class)),
134 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
135 verify(writeTransaction).commit();
139 public void testFilteringEmptySchemaSourceRegistered() {
140 yangLibProvider.init();
142 // test empty list of schema sources registered
143 yangLibProvider.schemaSourceRegistered(Collections.emptyList());
144 // expected behavior is to do nothing
145 verifyNoMoreInteractions(dataBroker, writeTransaction);
149 public void testFilteringNonYangSchemaSourceRegistered() {
150 yangLibProvider.init();
152 // test list of non yang schema sources registered
153 final var nonYangSources = new ArrayList<PotentialSchemaSource<?>>();
154 nonYangSources.add(PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
155 YinSchemaSourceRepresentation.class, Costs.IMMEDIATE.getValue()));
156 nonYangSources.add(PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"),
157 YangIRSchemaSource.class, Costs.IMMEDIATE.getValue()));
158 yangLibProvider.schemaSourceRegistered(nonYangSources);
160 // expected behavior is to do nothing
161 verifyNoMoreInteractions(dataBroker, writeTransaction);
165 public void testSchemaSourceWithRevisionUnregistered() {
166 yangLibProvider.init();
168 // try to unregister YANG source with revision
169 final var schemaSourceWithRevision = PotentialSchemaSource.create(
170 new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"),
171 YangTextSchemaSource.class, Costs.LOCAL_IO.getValue());
172 yangLibProvider.schemaSourceUnregistered(schemaSourceWithRevision);
174 // source is unregistered
175 verify(dataBroker).newWriteOnlyTransaction();
176 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
177 eq(InstanceIdentifier.create(ModulesState.class)
179 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
180 new Revision(new RevisionIdentifier("2016-04-28"))))));
181 verify(writeTransaction).commit();
185 public void testSchemaSourceWithoutRevisionUnregistered() {
186 yangLibProvider.init();
188 // try to unregister YANG source without revision
189 final var schemaSourceWithoutRevision = PotentialSchemaSource.create(
190 new SourceIdentifier("unregistered-yang-schema-without-revision"), YangTextSchemaSource.class,
191 Costs.LOCAL_IO.getValue());
192 yangLibProvider.schemaSourceUnregistered(schemaSourceWithoutRevision);
194 // source is unregistered
195 verify(dataBroker).newWriteOnlyTransaction();
196 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
197 eq(InstanceIdentifier.create(ModulesState.class)
199 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
200 LegacyRevisionUtils.emptyRevision()))));
201 verify(writeTransaction).commit();
205 public void testNonYangSchemaSourceUnregistered() {
206 yangLibProvider.init();
208 // try to unregister non-YANG source
209 final var nonYangSources = PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
210 YinSchemaSourceRepresentation.class, Costs.IMMEDIATE.getValue());
211 yangLibProvider.schemaSourceUnregistered(nonYangSources);
213 // expected behaviour is to do nothing if non yang based source is unregistered
214 verifyNoMoreInteractions(dataBroker, writeTransaction);