f6c712976fc0981f1080ddfe3b456b1374bb2d1b
[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.rev160621.ModulesState;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesStateBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionIdentifier;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionUtils;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.CommonLeafs.Revision;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleKey;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfigBuilder;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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.repo.SharedSchemaRepository;
54 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
55
56 public class YangLibProviderTest {
57     private static final File CACHE_DIR = new File("target/yanglib");
58
59     @Mock
60     private DataBroker dataBroker;
61
62     @Mock
63     private WriteTransaction writeTransaction;
64
65     private YangLibProvider yangLibProvider;
66
67     @BeforeClass
68     public static void staticSetup() {
69         if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
70             throw new RuntimeException("Failed to create " + CACHE_DIR);
71         }
72     }
73
74     @AfterClass
75     public static void staticCleanup() {
76         FileUtils.deleteQuietly(CACHE_DIR);
77     }
78
79     @Before
80     public void setUp() {
81         MockitoAnnotations.initMocks(this);
82
83         try {
84             if (CACHE_DIR.exists()) {
85                 FileUtils.cleanDirectory(CACHE_DIR);
86             }
87         } catch (IOException e) {
88             // Ignore
89         }
90
91         final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
92                 .setBindingPort(300L).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
93         yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new SharedSchemaRepository("yang-library"));
94     }
95
96     @Test
97     public void testSchemaSourceRegistered() {
98         yangLibProvider.init();
99         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
100         doNothing().when(writeTransaction)
101                 .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
102
103         List<PotentialSchemaSource<?>> list = new ArrayList<>();
104         list.add(
105                 PotentialSchemaSource.create(
106                         RevisionSourceIdentifier.create("no-revision"),
107                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
108
109         list.add(
110                 PotentialSchemaSource.create(
111                         RevisionSourceIdentifier.create("with-revision",
112                             org.opendaylight.yangtools.yang.common.Revision.of("2016-04-28")),
113                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
114
115         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
116         yangLibProvider.schemaSourceRegistered(list);
117
118         List<Module> newModulesList = new ArrayList<>();
119
120         Module newModule = new ModuleBuilder()
121                 .setName(new YangIdentifier("no-revision"))
122                 .setRevision(RevisionUtils.emptyRevision())
123                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
124                 .build();
125
126         newModulesList.add(newModule);
127
128         newModule = new ModuleBuilder()
129                 .setName(new YangIdentifier("with-revision"))
130                 .setRevision(new Revision(new RevisionIdentifier("2016-04-28")))
131                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
132                 .build();
133
134         newModulesList.add(newModule);
135
136         verify(dataBroker).newWriteOnlyTransaction();
137         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
138                 eq(InstanceIdentifier.create(ModulesState.class)),
139                 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
140         verify(writeTransaction).submit();
141     }
142
143     @Test
144     public void testFilteringNonYangSchemaSourceRegistered() {
145         yangLibProvider.init();
146
147         // test empty list of schema sources registered
148         List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
149         yangLibProvider.schemaSourceRegistered(potentialSources);
150
151         verifyZeroInteractions(dataBroker, writeTransaction);
152
153         // test list of non yang schema sources registered
154         // expected behavior is to do nothing
155         potentialSources = new ArrayList<>();
156         potentialSources.add(
157                 PotentialSchemaSource.create(
158                         RevisionSourceIdentifier.create("yin-source-representation"),
159                         YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
160
161         potentialSources.add(
162                 PotentialSchemaSource.create(
163                         RevisionSourceIdentifier.create("asts-schema-source"),
164                         ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
165
166         yangLibProvider.schemaSourceRegistered(potentialSources);
167         verifyZeroInteractions(dataBroker, writeTransaction);
168
169         // add yang schema source to list
170         potentialSources.add(
171                 PotentialSchemaSource.create(
172                         RevisionSourceIdentifier.create("yang-schema-source"),
173                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
174
175         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
176         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
177         yangLibProvider.schemaSourceRegistered(potentialSources);
178         verify(dataBroker).newWriteOnlyTransaction();
179
180         ArgumentCaptor<ModulesState> modulesStateCaptor = ArgumentCaptor.forClass(ModulesState.class);
181         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
182                 eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
183         assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
184         verify(writeTransaction).submit();
185     }
186
187     @Test
188     public void testNonYangSchemaSourceUnregistered() {
189         yangLibProvider.init();
190
191         final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
192                 PotentialSchemaSource.create(
193                         RevisionSourceIdentifier.create("yin-source-representation"),
194                 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
195
196         yangLibProvider.schemaSourceUnregistered(nonYangSource);
197
198         // expected behaviour is to do nothing if non yang based source is unregistered
199         verifyZeroInteractions(dataBroker, writeTransaction);
200     }
201
202     @Test
203     public void testSchemaSourceUnregistered() {
204         yangLibProvider.init();
205
206         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
207         doNothing().when(writeTransaction)
208                 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
209
210         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
211
212         PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
213                 PotentialSchemaSource.create(
214                         RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
215                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
216
217         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
218
219         verify(dataBroker).newWriteOnlyTransaction();
220         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
221                 eq(InstanceIdentifier.create(ModulesState.class)
222                         .child(Module.class,
223                                 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
224                                         RevisionUtils.emptyRevision()))));
225
226         verify(writeTransaction).submit();
227
228         yangUnregistererSource =
229                 PotentialSchemaSource.create(
230                         RevisionSourceIdentifier.create("unregistered-yang-with-revision",
231                             org.opendaylight.yangtools.yang.common.Revision.of("2016-04-28")),
232                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
233
234         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
235
236         verify(dataBroker, times(2)).newWriteOnlyTransaction();
237         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
238                 eq(InstanceIdentifier.create(ModulesState.class)
239                         .child(Module.class,
240                                 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
241                                         new Revision(new RevisionIdentifier("2016-04-28"))))));
242
243         verify(writeTransaction, times(2)).submit();
244     }
245 }