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.rfc7950.repo.ASTSchemaSource;
55 public class YangLibProviderTest {
56 private static final File CACHE_DIR = new File("target/yanglib");
59 private DataBroker dataBroker;
62 private WriteTransaction writeTransaction;
64 private YangLibProvider yangLibProvider;
67 public static void staticSetup() {
68 if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
69 throw new RuntimeException("Failed to create " + CACHE_DIR);
74 public static void staticCleanup() {
75 FileUtils.deleteQuietly(CACHE_DIR);
80 MockitoAnnotations.initMocks(this);
83 if (CACHE_DIR.exists()) {
84 FileUtils.cleanDirectory(CACHE_DIR);
86 } catch (IOException e) {
90 final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
91 .setBindingPort(300L).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
92 yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker);
96 public void testSchemaSourceRegistered() {
97 yangLibProvider.init();
98 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
99 doNothing().when(writeTransaction)
100 .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
102 List<PotentialSchemaSource<?>> list = new ArrayList<>();
104 PotentialSchemaSource.create(
105 RevisionSourceIdentifier.create("no-revision"),
106 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
109 PotentialSchemaSource.create(
110 RevisionSourceIdentifier.create("with-revision", Revision.of("2016-04-28")),
111 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
113 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
114 yangLibProvider.schemaSourceRegistered(list);
116 List<Module> newModulesList = new ArrayList<>();
118 Module newModule = new ModuleBuilder()
119 .setName(new YangIdentifier("no-revision"))
120 .setRevision(new OptionalRevision(""))
121 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
124 newModulesList.add(newModule);
126 newModule = new ModuleBuilder()
127 .setName(new YangIdentifier("with-revision"))
128 .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28")))
129 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
132 newModulesList.add(newModule);
134 verify(dataBroker).newWriteOnlyTransaction();
135 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
136 eq(InstanceIdentifier.create(ModulesState.class)),
137 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
138 verify(writeTransaction).submit();
142 public void testFilteringNonYangSchemaSourceRegistered() {
143 yangLibProvider.init();
145 // test empty list of schema sources registered
146 List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
147 yangLibProvider.schemaSourceRegistered(potentialSources);
149 verifyZeroInteractions(dataBroker, writeTransaction);
151 // test list of non yang schema sources registered
152 // expected behavior is to do nothing
153 potentialSources = new ArrayList<>();
154 potentialSources.add(
155 PotentialSchemaSource.create(
156 RevisionSourceIdentifier.create("yin-source-representation"),
157 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
159 potentialSources.add(
160 PotentialSchemaSource.create(
161 RevisionSourceIdentifier.create("asts-schema-source"),
162 ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
164 yangLibProvider.schemaSourceRegistered(potentialSources);
165 verifyZeroInteractions(dataBroker, writeTransaction);
167 // add yang schema source to list
168 potentialSources.add(
169 PotentialSchemaSource.create(
170 RevisionSourceIdentifier.create("yang-schema-source"),
171 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
173 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
174 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
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).submit();
186 public void testNonYangSchemaSourceUnregistered() {
187 yangLibProvider.init();
189 final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
190 PotentialSchemaSource.create(
191 RevisionSourceIdentifier.create("yin-source-representation"),
192 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
194 yangLibProvider.schemaSourceUnregistered(nonYangSource);
196 // expected behaviour is to do nothing if non yang based source is unregistered
197 verifyZeroInteractions(dataBroker, writeTransaction);
201 public void testSchemaSourceUnregistered() {
202 yangLibProvider.init();
204 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
205 doNothing().when(writeTransaction)
206 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
208 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
210 PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
211 PotentialSchemaSource.create(
212 RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
213 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
215 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
217 verify(dataBroker).newWriteOnlyTransaction();
218 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
219 eq(InstanceIdentifier.create(ModulesState.class)
221 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
222 new OptionalRevision("")))));
224 verify(writeTransaction).submit();
226 yangUnregistererSource =
227 PotentialSchemaSource.create(
228 RevisionSourceIdentifier.create("unregistered-yang-with-revision", Revision.of("2016-04-28")),
229 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
231 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
233 verify(dataBroker, times(2)).newWriteOnlyTransaction();
234 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
235 eq(InstanceIdentifier.create(ModulesState.class)
237 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
238 new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
240 verify(writeTransaction, times(2)).submit();