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;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesState;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesStateBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.OptionalRevision;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.RevisionIdentifier;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.Module;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
43 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
44 import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
45 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
46 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
47 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
49 public class YangLibProviderTest {
52 private BindingAwareBroker.ProviderContext context;
55 private DataBroker dataBroker;
58 private WriteTransaction writeTransaction;
60 private TestingYangLibProvider yangLibProvider;
64 MockitoAnnotations.initMocks(this);
65 yangLibProvider = new TestingYangLibProvider(new SharedSchemaRepository("test"), "www.fake.com", 300);
66 when(context.getSALService(eq(DataBroker.class))).thenReturn(dataBroker);
70 public void testSchemaSourceRegistered() {
71 yangLibProvider.onSessionInitiated(context);
72 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
73 doNothing().when(writeTransaction)
74 .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
76 List<PotentialSchemaSource<?>> list = new ArrayList<>();
78 PotentialSchemaSource.create(
79 RevisionSourceIdentifier.create("no-revision"),
80 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
83 PotentialSchemaSource.create(
84 RevisionSourceIdentifier.create("with-revision", "2016-04-28"),
85 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
87 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
88 yangLibProvider.schemaSourceRegistered(list);
90 List<Module> newModulesList = new ArrayList<>();
92 Module newModule = new ModuleBuilder()
93 .setName(new YangIdentifier("no-revision"))
94 .setRevision(new OptionalRevision(""))
95 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
98 newModulesList.add(newModule);
100 newModule = new ModuleBuilder()
101 .setName(new YangIdentifier("with-revision"))
102 .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28")))
103 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
106 newModulesList.add(newModule);
108 verify(dataBroker).newWriteOnlyTransaction();
109 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
110 eq(InstanceIdentifier.create(ModulesState.class)),
111 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
112 verify(writeTransaction).submit();
116 public void testFilteringNonYangSchemaSourceRegistered() {
117 yangLibProvider.onSessionInitiated(context);
119 // test empty list of schema sources registered
120 List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
121 yangLibProvider.schemaSourceRegistered(potentialSources);
123 verifyZeroInteractions(dataBroker, writeTransaction);
125 // test list of non yang schema sources registered
126 // expected behavior is to do nothing
127 potentialSources = new ArrayList<>();
128 potentialSources.add(
129 PotentialSchemaSource.create(
130 RevisionSourceIdentifier.create("yin-source-representation"),
131 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
133 potentialSources.add(
134 PotentialSchemaSource.create(
135 RevisionSourceIdentifier.create("asts-schema-source"),
136 ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
138 yangLibProvider.schemaSourceRegistered(potentialSources);
139 verifyZeroInteractions(dataBroker, writeTransaction);
141 // add yang schema source to list
142 potentialSources.add(
143 PotentialSchemaSource.create(
144 RevisionSourceIdentifier.create("yang-schema-source"),
145 YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
147 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
148 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
149 yangLibProvider.schemaSourceRegistered(potentialSources);
150 verify(dataBroker).newWriteOnlyTransaction();
152 ArgumentCaptor<ModulesState> modulesStateCaptor = ArgumentCaptor.forClass(ModulesState.class);
153 verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
154 eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
155 assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
156 verify(writeTransaction).submit();
160 public void testNonYangSchemaSourceUnregistered() {
161 yangLibProvider.onSessionInitiated(context);
163 final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
164 PotentialSchemaSource.create(
165 RevisionSourceIdentifier.create("yin-source-representation"),
166 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
168 yangLibProvider.schemaSourceUnregistered(nonYangSource);
170 // expected behaviour is to do nothing if non yang based source is unregistered
171 verifyZeroInteractions(dataBroker, writeTransaction);
175 public void testSchemaSourceUnregistered() {
176 yangLibProvider.onSessionInitiated(context);
178 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
179 doNothing().when(writeTransaction)
180 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
182 when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
184 PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
185 PotentialSchemaSource.create(
186 RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
187 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
189 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
191 verify(dataBroker).newWriteOnlyTransaction();
192 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
193 eq(InstanceIdentifier.create(ModulesState.class)
195 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
196 new OptionalRevision("")))));
198 verify(writeTransaction).submit();
200 yangUnregistererSource =
201 PotentialSchemaSource.create(
202 RevisionSourceIdentifier.create("unregistered-yang-with-revision", "2016-04-28"),
203 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
205 yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
207 verify(dataBroker, times(2)).newWriteOnlyTransaction();
208 verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
209 eq(InstanceIdentifier.create(ModulesState.class)
211 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
212 new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
214 verify(writeTransaction, times(2)).submit();
217 private static class TestingYangLibProvider extends YangLibProvider {
219 public TestingYangLibProvider(SharedSchemaRepository schemaRepository, String bindingAddress, long bindingPort) {
220 super(schemaRepository, bindingAddress, bindingPort);
224 public void onSessionInitiated(BindingAwareBroker.ProviderContext providerContext) {
225 this.dataBroker = providerContext.getSALService(DataBroker.class);
226 schemaListenerRegistration = schemaRepository.registerSchemaSourceListener(this);