Bump upstream versions
[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.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.verifyNoMoreInteractions;
18 import static org.mockito.Mockito.when;
19 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import org.apache.commons.io.FileUtils;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.opendaylight.mdsal.binding.api.DataBroker;
38 import org.opendaylight.mdsal.binding.api.WriteTransaction;
39 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs.Revision;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfigBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.Uint32;
54 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
55 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
56 import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
57 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
58 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
59 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
60
61 @RunWith(MockitoJUnitRunner.StrictStubs.class)
62 public class YangLibProviderTest {
63     private static final File CACHE_DIR = new File("target/yanglib");
64
65     @Mock
66     private DataBroker dataBroker;
67
68     @Mock
69     private WriteTransaction writeTransaction;
70
71     private YangLibProvider yangLibProvider;
72
73     @BeforeClass
74     public static void staticSetup() {
75         if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
76             throw new RuntimeException("Failed to create " + CACHE_DIR);
77         }
78     }
79
80     @AfterClass
81     public static void staticCleanup() {
82         FileUtils.deleteQuietly(CACHE_DIR);
83     }
84
85     @Before
86     public void setUp() {
87         try {
88             if (CACHE_DIR.exists()) {
89                 FileUtils.cleanDirectory(CACHE_DIR);
90             }
91         } catch (IOException e) {
92             // Ignore
93         }
94
95         final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
96                 .setBindingPort(Uint32.valueOf(300)).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
97         yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new DefaultYangParserFactory());
98     }
99
100     @Test
101     public void testSchemaSourceRegistered() {
102         yangLibProvider.init();
103         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
104         doNothing().when(writeTransaction)
105                 .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
106
107         List<PotentialSchemaSource<?>> list = new ArrayList<>();
108         list.add(
109                 PotentialSchemaSource.create(new SourceIdentifier("no-revision"),
110                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
111
112         list.add(
113                 PotentialSchemaSource.create(new SourceIdentifier("with-revision", "2016-04-28"),
114                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
115
116         doReturn(emptyFluentFuture()).when(writeTransaction).commit();
117         yangLibProvider.schemaSourceRegistered(list);
118
119         Map<ModuleKey, Module> newModulesList = new HashMap<>();
120
121         Module newModule = new ModuleBuilder()
122                 .setName(new YangIdentifier("no-revision"))
123                 .setRevision(LegacyRevisionUtils.emptyRevision())
124                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
125                 .build();
126
127         newModulesList.put(newModule.key(), newModule);
128
129         newModule = new ModuleBuilder()
130                 .setName(new YangIdentifier("with-revision"))
131                 .setRevision(new Revision(new RevisionIdentifier("2016-04-28")))
132                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
133                 .build();
134
135         newModulesList.put(newModule.key(), newModule);
136
137         verify(dataBroker).newWriteOnlyTransaction();
138         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
139                 eq(InstanceIdentifier.create(ModulesState.class)),
140                 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
141         verify(writeTransaction).commit();
142     }
143
144     @Test
145     public void testFilteringNonYangSchemaSourceRegistered() {
146         yangLibProvider.init();
147
148         // test empty list of schema sources registered
149         List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
150         yangLibProvider.schemaSourceRegistered(potentialSources);
151
152         verifyNoMoreInteractions(dataBroker, writeTransaction);
153
154         // test list of non yang schema sources registered
155         // expected behavior is to do nothing
156         potentialSources = new ArrayList<>();
157         potentialSources.add(
158                 PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
159                         YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
160
161         potentialSources.add(
162                 PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"),
163                         IRSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
164
165         yangLibProvider.schemaSourceRegistered(potentialSources);
166         verifyNoMoreInteractions(dataBroker, writeTransaction);
167
168         // add yang schema source to list
169         potentialSources.add(
170                 PotentialSchemaSource.create(new SourceIdentifier("yang-schema-source"),
171                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
172
173         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
174         doReturn(emptyFluentFuture()).when(writeTransaction).commit();
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).commit();
183     }
184
185     @Test
186     public void testNonYangSchemaSourceUnregistered() {
187         yangLibProvider.init();
188
189         final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
190             PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
191                 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
192
193         yangLibProvider.schemaSourceUnregistered(nonYangSource);
194
195         // expected behaviour is to do nothing if non yang based source is unregistered
196         verifyNoMoreInteractions(dataBroker, writeTransaction);
197     }
198
199     @Test
200     public void testSchemaSourceUnregistered() {
201         yangLibProvider.init();
202
203         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
204         doNothing().when(writeTransaction)
205                 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
206
207         doReturn(emptyFluentFuture()).when(writeTransaction).commit();
208
209         PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
210             PotentialSchemaSource.create(new SourceIdentifier("unregistered-yang-schema-without-revision"),
211                 YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
212
213         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
214
215         verify(dataBroker).newWriteOnlyTransaction();
216         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
217                 eq(InstanceIdentifier.create(ModulesState.class)
218                         .child(Module.class,
219                                 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
220                                         LegacyRevisionUtils.emptyRevision()))));
221
222         verify(writeTransaction).commit();
223
224         yangUnregistererSource =
225                 PotentialSchemaSource.create(new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"),
226                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
227
228         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
229
230         verify(dataBroker, times(2)).newWriteOnlyTransaction();
231         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
232                 eq(InstanceIdentifier.create(ModulesState.class)
233                         .child(Module.class,
234                                 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
235                                         new Revision(new RevisionIdentifier("2016-04-28"))))));
236
237         verify(writeTransaction, times(2)).commit();
238     }
239 }