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.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyZeroInteractions;
17 import static org.mockito.Mockito.when;
19 import com.google.common.util.concurrent.Futures;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesState;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesStateBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.OptionalRevision;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.RevisionIdentifier;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.Module;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleKey;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfigBuilder;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.Revision;
49 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
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.parser.repo.SharedSchemaRepository;
54 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
56 public class YangLibProviderTest {
57 private static final File CACHE_DIR = new File("target/yanglib");
60 private DataBroker dataBroker;
63 private WriteTransaction writeTransaction;
65 private YangLibProvider yangLibProvider;
68 public static void staticSetup() {
69 if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
70 throw new RuntimeException("Failed to create " + CACHE_DIR);
75 public static void staticCleanup() {
76 FileUtils.deleteQuietly(CACHE_DIR);
81 MockitoAnnotations.initMocks(this);
84 if (CACHE_DIR.exists()) {
85 FileUtils.cleanDirectory(CACHE_DIR);
87 } catch (IOException e) {
91 final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
92 .setBindingPort(300L).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
93 yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new SharedSchemaRepository("yang-library"));
97 public void testSchemaSourceRegistered() {
98 yangLibProvider.init();
99 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
100 doNothing().when(writeTransaction)
101 .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
103 List<PotentialSchemaSource<?>> list = new ArrayList<>();
105 PotentialSchemaSource.create(
106 RevisionSourceIdentifier.create("no-revision"),
107 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
110 PotentialSchemaSource.create(
111 RevisionSourceIdentifier.create("with-revision", Revision.of("2016-04-28")),
112 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
114 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
115 yangLibProvider.schemaSourceRegistered(list);
117 List<Module> newModulesList = new ArrayList<>();
119 Module newModule = new ModuleBuilder()
120 .setName(new YangIdentifier("no-revision"))
121 .setRevision(new OptionalRevision(""))
122 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
125 newModulesList.add(newModule);
127 newModule = new ModuleBuilder()
128 .setName(new YangIdentifier("with-revision"))
129 .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28")))
130 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
133 newModulesList.add(newModule);
135 verify(dataBroker).newWriteOnlyTransaction();
136 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
137 eq(InstanceIdentifier.create(ModulesState.class)),
138 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
139 verify(writeTransaction).submit();
143 public void testFilteringNonYangSchemaSourceRegistered() {
144 yangLibProvider.init();
146 // test empty list of schema sources registered
147 List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
148 yangLibProvider.schemaSourceRegistered(potentialSources);
150 verifyZeroInteractions(dataBroker, writeTransaction);
152 // test list of non yang schema sources registered
153 // expected behavior is to do nothing
154 potentialSources = new ArrayList<>();
155 potentialSources.add(
156 PotentialSchemaSource.create(
157 RevisionSourceIdentifier.create("yin-source-representation"),
158 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
160 potentialSources.add(
161 PotentialSchemaSource.create(
162 RevisionSourceIdentifier.create("asts-schema-source"),
163 ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
165 yangLibProvider.schemaSourceRegistered(potentialSources);
166 verifyZeroInteractions(dataBroker, writeTransaction);
168 // add yang schema source to list
169 potentialSources.add(
170 PotentialSchemaSource.create(
171 RevisionSourceIdentifier.create("yang-schema-source"),
172 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
174 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
175 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
176 yangLibProvider.schemaSourceRegistered(potentialSources);
177 verify(dataBroker).newWriteOnlyTransaction();
179 ArgumentCaptor<ModulesState> modulesStateCaptor = ArgumentCaptor.forClass(ModulesState.class);
180 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
181 eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
182 assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
183 verify(writeTransaction).submit();
187 public void testNonYangSchemaSourceUnregistered() {
188 yangLibProvider.init();
190 final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
191 PotentialSchemaSource.create(
192 RevisionSourceIdentifier.create("yin-source-representation"),
193 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
195 yangLibProvider.schemaSourceUnregistered(nonYangSource);
197 // expected behaviour is to do nothing if non yang based source is unregistered
198 verifyZeroInteractions(dataBroker, writeTransaction);
202 public void testSchemaSourceUnregistered() {
203 yangLibProvider.init();
205 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
206 doNothing().when(writeTransaction)
207 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
209 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
211 PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
212 PotentialSchemaSource.create(
213 RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
214 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
216 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
218 verify(dataBroker).newWriteOnlyTransaction();
219 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
220 eq(InstanceIdentifier.create(ModulesState.class)
222 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
223 new OptionalRevision("")))));
225 verify(writeTransaction).submit();
227 yangUnregistererSource =
228 PotentialSchemaSource.create(
229 RevisionSourceIdentifier.create("unregistered-yang-with-revision", Revision.of("2016-04-28")),
230 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
232 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
234 verify(dataBroker, times(2)).newWriteOnlyTransaction();
235 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
236 eq(InstanceIdentifier.create(ModulesState.class)
238 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
239 new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
241 verify(writeTransaction, times(2)).submit();