Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / netconf / yanglib / src / test / java / org / opendaylight / yanglib / impl / YangLibProviderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yanglib.impl;
9
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;
18
19 import com.google.common.util.concurrent.Futures;
20 import java.io.File;
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;
54
55 public class YangLibProviderTest {
56     private static final File CACHE_DIR = new File("target/yanglib");
57
58     @Mock
59     private DataBroker dataBroker;
60
61     @Mock
62     private WriteTransaction writeTransaction;
63
64     private YangLibProvider yangLibProvider;
65
66     @BeforeClass
67     public static void staticSetup() {
68         if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
69             throw new RuntimeException("Failed to create " + CACHE_DIR);
70         }
71     }
72
73     @AfterClass
74     public static void staticCleanup() {
75         FileUtils.deleteQuietly(CACHE_DIR);
76     }
77
78     @Before
79     public void setUp() {
80         MockitoAnnotations.initMocks(this);
81
82         try {
83             if (CACHE_DIR.exists()) {
84                 FileUtils.cleanDirectory(CACHE_DIR);
85             }
86         } catch (IOException e) {
87             // Ignore
88         }
89
90         final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
91                 .setBindingPort(300L).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
92         yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new YangLibServiceImpl());
93     }
94
95     @Test
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());
101
102         List<PotentialSchemaSource<?>> list = new ArrayList<>();
103         list.add(
104                 PotentialSchemaSource.create(
105                         RevisionSourceIdentifier.create("no-revision"),
106                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
107
108         list.add(
109                 PotentialSchemaSource.create(
110                         RevisionSourceIdentifier.create("with-revision", Revision.of("2016-04-28")),
111                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
112
113         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
114         yangLibProvider.schemaSourceRegistered(list);
115
116         List<Module> newModulesList = new ArrayList<>();
117
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/"))
122                 .build();
123
124         newModulesList.add(newModule);
125
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"))
130                 .build();
131
132         newModulesList.add(newModule);
133
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();
139     }
140
141     @Test
142     public void testFilteringNonYangSchemaSourceRegistered() {
143         yangLibProvider.init();
144
145         // test empty list of schema sources registered
146         List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
147         yangLibProvider.schemaSourceRegistered(potentialSources);
148
149         verifyZeroInteractions(dataBroker, writeTransaction);
150
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()));
158
159         potentialSources.add(
160                 PotentialSchemaSource.create(
161                         RevisionSourceIdentifier.create("asts-schema-source"),
162                         ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
163
164         yangLibProvider.schemaSourceRegistered(potentialSources);
165         verifyZeroInteractions(dataBroker, writeTransaction);
166
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()));
172
173         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
174         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
175         yangLibProvider.schemaSourceRegistered(potentialSources);
176         verify(dataBroker).newWriteOnlyTransaction();
177
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();
183     }
184
185     @Test
186     public void testNonYangSchemaSourceUnregistered() {
187         yangLibProvider.init();
188
189         final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
190                 PotentialSchemaSource.create(
191                         RevisionSourceIdentifier.create("yin-source-representation"),
192                 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
193
194         yangLibProvider.schemaSourceUnregistered(nonYangSource);
195
196         // expected behaviour is to do nothing if non yang based source is unregistered
197         verifyZeroInteractions(dataBroker, writeTransaction);
198     }
199
200     @Test
201     public void testSchemaSourceUnregistered() {
202         yangLibProvider.init();
203
204         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
205         doNothing().when(writeTransaction)
206                 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
207
208         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
209
210         PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
211                 PotentialSchemaSource.create(
212                         RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
213                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
214
215         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
216
217         verify(dataBroker).newWriteOnlyTransaction();
218         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
219                 eq(InstanceIdentifier.create(ModulesState.class)
220                         .child(Module.class,
221                                 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
222                                         new OptionalRevision("")))));
223
224         verify(writeTransaction).submit();
225
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());
230
231         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
232
233         verify(dataBroker, times(2)).newWriteOnlyTransaction();
234         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
235                 eq(InstanceIdentifier.create(ModulesState.class)
236                         .child(Module.class,
237                                 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
238                                         new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
239
240         verify(writeTransaction, times(2)).submit();
241     }
242 }