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.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.verifyNoMoreInteractions;
18 import static org.mockito.Mockito.when;
19 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.apache.commons.io.FileUtils;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.opendaylight.mdsal.binding.api.DataBroker;
38 import org.opendaylight.mdsal.binding.api.WriteTransaction;
39 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs.Revision;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfigBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.Uint32;
54 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
55 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
56 import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
57 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
58 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
59 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
61 @RunWith(MockitoJUnitRunner.StrictStubs.class)
62 public class YangLibProviderTest {
63 private static final File CACHE_DIR = new File("target/yanglib");
66 private DataBroker dataBroker;
69 private WriteTransaction writeTransaction;
71 private YangLibProvider yangLibProvider;
74 public static void staticSetup() {
75 if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
76 throw new RuntimeException("Failed to create " + CACHE_DIR);
81 public static void staticCleanup() {
82 FileUtils.deleteQuietly(CACHE_DIR);
88 if (CACHE_DIR.exists()) {
89 FileUtils.cleanDirectory(CACHE_DIR);
91 } catch (IOException e) {
95 final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
96 .setBindingPort(Uint32.valueOf(300)).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
97 yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new DefaultYangParserFactory());
101 public void testSchemaSourceRegistered() {
102 yangLibProvider.init();
103 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
104 doNothing().when(writeTransaction)
105 .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
107 List<PotentialSchemaSource<?>> list = new ArrayList<>();
109 PotentialSchemaSource.create(new SourceIdentifier("no-revision"),
110 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
113 PotentialSchemaSource.create(new SourceIdentifier("with-revision", "2016-04-28"),
114 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
116 doReturn(emptyFluentFuture()).when(writeTransaction).commit();
117 yangLibProvider.schemaSourceRegistered(list);
119 Map<ModuleKey, Module> newModulesList = new HashMap<>();
121 Module newModule = new ModuleBuilder()
122 .setName(new YangIdentifier("no-revision"))
123 .setRevision(LegacyRevisionUtils.emptyRevision())
124 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
127 newModulesList.put(newModule.key(), newModule);
129 newModule = new ModuleBuilder()
130 .setName(new YangIdentifier("with-revision"))
131 .setRevision(new Revision(new RevisionIdentifier("2016-04-28")))
132 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
135 newModulesList.put(newModule.key(), newModule);
137 verify(dataBroker).newWriteOnlyTransaction();
138 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
139 eq(InstanceIdentifier.create(ModulesState.class)),
140 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
141 verify(writeTransaction).commit();
145 public void testFilteringNonYangSchemaSourceRegistered() {
146 yangLibProvider.init();
148 // test empty list of schema sources registered
149 List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
150 yangLibProvider.schemaSourceRegistered(potentialSources);
152 verifyNoMoreInteractions(dataBroker, writeTransaction);
154 // test list of non yang schema sources registered
155 // expected behavior is to do nothing
156 potentialSources = new ArrayList<>();
157 potentialSources.add(
158 PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
159 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
161 potentialSources.add(
162 PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"),
163 IRSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
165 yangLibProvider.schemaSourceRegistered(potentialSources);
166 verifyNoMoreInteractions(dataBroker, writeTransaction);
168 // add yang schema source to list
169 potentialSources.add(
170 PotentialSchemaSource.create(new SourceIdentifier("yang-schema-source"),
171 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
173 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
174 doReturn(emptyFluentFuture()).when(writeTransaction).commit();
175 yangLibProvider.schemaSourceRegistered(potentialSources);
176 verify(dataBroker).newWriteOnlyTransaction();
178 ArgumentCaptor<ModulesState> modulesStateCaptor = ArgumentCaptor.forClass(ModulesState.class);
179 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
180 eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
181 assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
182 verify(writeTransaction).commit();
186 public void testNonYangSchemaSourceUnregistered() {
187 yangLibProvider.init();
189 final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
190 PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
191 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
193 yangLibProvider.schemaSourceUnregistered(nonYangSource);
195 // expected behaviour is to do nothing if non yang based source is unregistered
196 verifyNoMoreInteractions(dataBroker, writeTransaction);
200 public void testSchemaSourceUnregistered() {
201 yangLibProvider.init();
203 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
204 doNothing().when(writeTransaction)
205 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
207 doReturn(emptyFluentFuture()).when(writeTransaction).commit();
209 PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
210 PotentialSchemaSource.create(new SourceIdentifier("unregistered-yang-schema-without-revision"),
211 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
213 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
215 verify(dataBroker).newWriteOnlyTransaction();
216 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
217 eq(InstanceIdentifier.create(ModulesState.class)
219 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
220 LegacyRevisionUtils.emptyRevision()))));
222 verify(writeTransaction).commit();
224 yangUnregistererSource =
225 PotentialSchemaSource.create(new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"),
226 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
228 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
230 verify(dataBroker, times(2)).newWriteOnlyTransaction();
231 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
232 eq(InstanceIdentifier.create(ModulesState.class)
234 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
235 new Revision(new RevisionIdentifier("2016-04-28"))))));
237 verify(writeTransaction, times(2)).commit();