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.model.repo.api.RevisionSourceIdentifier;
49 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
50 import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
51 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
52 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
54 public class YangLibProviderTest {
55 private static final File CACHE_DIR = new File("target/yanglib");
58 private DataBroker dataBroker;
61 private WriteTransaction writeTransaction;
63 private YangLibProvider yangLibProvider;
66 public static void staticSetup() {
67 if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
68 throw new RuntimeException("Failed to create " + CACHE_DIR);
73 public static void staticCleanup() {
74 FileUtils.deleteQuietly(CACHE_DIR);
79 MockitoAnnotations.initMocks(this);
82 if (CACHE_DIR.exists()) {
83 FileUtils.cleanDirectory(CACHE_DIR);
85 } catch (IOException e) {
89 final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
90 .setBindingPort(300L).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
91 yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new YangLibServiceImpl());
95 public void testSchemaSourceRegistered() {
96 yangLibProvider.init();
97 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
98 doNothing().when(writeTransaction)
99 .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
101 List<PotentialSchemaSource<?>> list = new ArrayList<>();
103 PotentialSchemaSource.create(
104 RevisionSourceIdentifier.create("no-revision"),
105 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
108 PotentialSchemaSource.create(
109 RevisionSourceIdentifier.create("with-revision", "2016-04-28"),
110 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
112 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
113 yangLibProvider.schemaSourceRegistered(list);
115 List<Module> newModulesList = new ArrayList<>();
117 Module newModule = new ModuleBuilder()
118 .setName(new YangIdentifier("no-revision"))
119 .setRevision(new OptionalRevision(""))
120 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
123 newModulesList.add(newModule);
125 newModule = new ModuleBuilder()
126 .setName(new YangIdentifier("with-revision"))
127 .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28")))
128 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
131 newModulesList.add(newModule);
133 verify(dataBroker).newWriteOnlyTransaction();
134 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
135 eq(InstanceIdentifier.create(ModulesState.class)),
136 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
137 verify(writeTransaction).submit();
141 public void testFilteringNonYangSchemaSourceRegistered() {
142 yangLibProvider.init();
144 // test empty list of schema sources registered
145 List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
146 yangLibProvider.schemaSourceRegistered(potentialSources);
148 verifyZeroInteractions(dataBroker, writeTransaction);
150 // test list of non yang schema sources registered
151 // expected behavior is to do nothing
152 potentialSources = new ArrayList<>();
153 potentialSources.add(
154 PotentialSchemaSource.create(
155 RevisionSourceIdentifier.create("yin-source-representation"),
156 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
158 potentialSources.add(
159 PotentialSchemaSource.create(
160 RevisionSourceIdentifier.create("asts-schema-source"),
161 ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
163 yangLibProvider.schemaSourceRegistered(potentialSources);
164 verifyZeroInteractions(dataBroker, writeTransaction);
166 // add yang schema source to list
167 potentialSources.add(
168 PotentialSchemaSource.create(
169 RevisionSourceIdentifier.create("yang-schema-source"),
170 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
172 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
173 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
174 yangLibProvider.schemaSourceRegistered(potentialSources);
175 verify(dataBroker).newWriteOnlyTransaction();
177 ArgumentCaptor<ModulesState> modulesStateCaptor = ArgumentCaptor.forClass(ModulesState.class);
178 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
179 eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
180 assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
181 verify(writeTransaction).submit();
185 public void testNonYangSchemaSourceUnregistered() {
186 yangLibProvider.init();
188 final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
189 PotentialSchemaSource.create(
190 RevisionSourceIdentifier.create("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 verifyZeroInteractions(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 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
209 PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
210 PotentialSchemaSource.create(
211 RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
212 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
214 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
216 verify(dataBroker).newWriteOnlyTransaction();
217 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
218 eq(InstanceIdentifier.create(ModulesState.class)
220 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
221 new OptionalRevision("")))));
223 verify(writeTransaction).submit();
225 yangUnregistererSource =
226 PotentialSchemaSource.create(
227 RevisionSourceIdentifier.create("unregistered-yang-with-revision", "2016-04-28"),
228 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
230 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
232 verify(dataBroker, times(2)).newWriteOnlyTransaction();
233 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
234 eq(InstanceIdentifier.create(ModulesState.class)
236 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
237 new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
239 verify(writeTransaction, times(2)).submit();