package org.opendaylight.yanglib.impl;
import static org.junit.Assert.assertEquals;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
+import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
-import com.google.common.util.concurrent.Futures;
+import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import org.apache.commons.io.FileUtils;
+import org.junit.AfterClass;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesState;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesStateBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.OptionalRevision;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.RevisionIdentifier;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.Module;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleKey;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.WriteTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesStateBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionUtils;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.CommonLeafs.Revision;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleKey;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfigBuilder;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
-import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
-import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class YangLibProviderTest {
-
- @Mock
- private BindingAwareBroker.ProviderContext context;
+ private static final File CACHE_DIR = new File("target/yanglib");
@Mock
private DataBroker dataBroker;
@Mock
private WriteTransaction writeTransaction;
- private TestingYangLibProvider yangLibProvider;
+ private YangLibProvider yangLibProvider;
+
+ @BeforeClass
+ public static void staticSetup() {
+ if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
+ throw new RuntimeException("Failed to create " + CACHE_DIR);
+ }
+ }
+
+ @AfterClass
+ public static void staticCleanup() {
+ FileUtils.deleteQuietly(CACHE_DIR);
+ }
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
- yangLibProvider = new TestingYangLibProvider(new SharedSchemaRepository("test"), "www.fake.com", 300);
- when(context.getSALService(eq(DataBroker.class))).thenReturn(dataBroker);
+ try {
+ if (CACHE_DIR.exists()) {
+ FileUtils.cleanDirectory(CACHE_DIR);
+ }
+ } catch (IOException e) {
+ // Ignore
+ }
+
+ final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
+ .setBindingPort(Uint32.valueOf(300)).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
+ yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new YangParserFactoryImpl());
}
@Test
public void testSchemaSourceRegistered() {
- yangLibProvider.onSessionInitiated(context);
+ yangLibProvider.init();
when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
doNothing().when(writeTransaction)
.merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
List<PotentialSchemaSource<?>> list = new ArrayList<>();
list.add(
- PotentialSchemaSource.create(new SourceIdentifier("no-revision"),
+ PotentialSchemaSource.create(
+ RevisionSourceIdentifier.create("no-revision"),
YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
list.add(
- PotentialSchemaSource.create(new SourceIdentifier("with-revision", "2016-04-28"),
+ PotentialSchemaSource.create(
+ RevisionSourceIdentifier.create("with-revision",
+ org.opendaylight.yangtools.yang.common.Revision.of("2016-04-28")),
YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
- when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
+ doReturn(emptyFluentFuture()).when(writeTransaction).commit();
yangLibProvider.schemaSourceRegistered(list);
- List<Module> newModulesList = new ArrayList<>();
+ Map<ModuleKey, Module> newModulesList = new HashMap<>();
Module newModule = new ModuleBuilder()
.setName(new YangIdentifier("no-revision"))
- .setRevision(new OptionalRevision(""))
+ .setRevision(RevisionUtils.emptyRevision())
.setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
.build();
- newModulesList.add(newModule);
+ newModulesList.put(newModule.key(), newModule);
newModule = new ModuleBuilder()
.setName(new YangIdentifier("with-revision"))
- .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28")))
+ .setRevision(new Revision(new RevisionIdentifier("2016-04-28")))
.setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
.build();
- newModulesList.add(newModule);
+ newModulesList.put(newModule.key(), newModule);
verify(dataBroker).newWriteOnlyTransaction();
verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
eq(InstanceIdentifier.create(ModulesState.class)),
eq(new ModulesStateBuilder().setModule(newModulesList).build()));
- verify(writeTransaction).submit();
+ verify(writeTransaction).commit();
}
@Test
public void testFilteringNonYangSchemaSourceRegistered() {
- yangLibProvider.onSessionInitiated(context);
+ yangLibProvider.init();
// test empty list of schema sources registered
List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
yangLibProvider.schemaSourceRegistered(potentialSources);
- verifyZeroInteractions(dataBroker, writeTransaction);
+ verifyNoMoreInteractions(dataBroker, writeTransaction);
// test list of non yang schema sources registered
// expected behavior is to do nothing
potentialSources = new ArrayList<>();
potentialSources.add(
- PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
+ PotentialSchemaSource.create(
+ RevisionSourceIdentifier.create("yin-source-representation"),
YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
potentialSources.add(
- PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"),
+ PotentialSchemaSource.create(
+ RevisionSourceIdentifier.create("asts-schema-source"),
ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
yangLibProvider.schemaSourceRegistered(potentialSources);
- verifyZeroInteractions(dataBroker, writeTransaction);
+ verifyNoMoreInteractions(dataBroker, writeTransaction);
// add yang schema source to list
potentialSources.add(
- PotentialSchemaSource.create(new SourceIdentifier("yang-schema-source"),
+ PotentialSchemaSource.create(
+ RevisionSourceIdentifier.create("yang-schema-source"),
YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
- when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
+ doReturn(emptyFluentFuture()).when(writeTransaction).commit();
yangLibProvider.schemaSourceRegistered(potentialSources);
verify(dataBroker).newWriteOnlyTransaction();
verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
- verify(writeTransaction).submit();
+ verify(writeTransaction).commit();
}
@Test
public void testNonYangSchemaSourceUnregistered() {
- yangLibProvider.onSessionInitiated(context);
+ yangLibProvider.init();
final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
- PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
+ PotentialSchemaSource.create(
+ RevisionSourceIdentifier.create("yin-source-representation"),
YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
yangLibProvider.schemaSourceUnregistered(nonYangSource);
// expected behaviour is to do nothing if non yang based source is unregistered
- verifyZeroInteractions(dataBroker, writeTransaction);
+ verifyNoMoreInteractions(dataBroker, writeTransaction);
}
@Test
public void testSchemaSourceUnregistered() {
- yangLibProvider.onSessionInitiated(context);
+ yangLibProvider.init();
when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
doNothing().when(writeTransaction)
.delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
- when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
+ doReturn(emptyFluentFuture()).when(writeTransaction).commit();
PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
- PotentialSchemaSource.create(new SourceIdentifier("unregistered-yang-schema-without-revision"),
+ PotentialSchemaSource.create(
+ RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
eq(InstanceIdentifier.create(ModulesState.class)
.child(Module.class,
new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
- new OptionalRevision("")))));
+ RevisionUtils.emptyRevision()))));
- verify(writeTransaction).submit();
+ verify(writeTransaction).commit();
yangUnregistererSource =
- PotentialSchemaSource.create(new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"),
+ PotentialSchemaSource.create(
+ RevisionSourceIdentifier.create("unregistered-yang-with-revision",
+ org.opendaylight.yangtools.yang.common.Revision.of("2016-04-28")),
YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
eq(InstanceIdentifier.create(ModulesState.class)
.child(Module.class,
new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
- new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
+ new Revision(new RevisionIdentifier("2016-04-28"))))));
- verify(writeTransaction, times(2)).submit();
+ verify(writeTransaction, times(2)).commit();
}
-
- private static class TestingYangLibProvider extends YangLibProvider {
-
- public TestingYangLibProvider(SharedSchemaRepository schemaRepository, String bindingAddress, long bindingPort) {
- super(schemaRepository, bindingAddress, bindingPort);
- }
-
- @Override
- public void onSessionInitiated(BindingAwareBroker.ProviderContext providerContext) {
- this.dataBroker = providerContext.getSALService(DataBroker.class);
- schemaListenerRegistration = schemaRepository.registerSchemaSourceListener(this);
- }
- }
-
-
-}
\ No newline at end of file
+}