440174efa6193f60110cf9bab6f6ae8544d9ab86
[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.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", Revision.of("2016-04-28")),
112                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
113
114         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
115         yangLibProvider.schemaSourceRegistered(list);
116
117         List<Module> newModulesList = new ArrayList<>();
118
119         Module newModule = new ModuleBuilder()
120                 .setName(new YangIdentifier("no-revision"))
121                 .setRevision(new OptionalRevision(""))
122                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
123                 .build();
124
125         newModulesList.add(newModule);
126
127         newModule = new ModuleBuilder()
128                 .setName(new YangIdentifier("with-revision"))
129                 .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28")))
130                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
131                 .build();
132
133         newModulesList.add(newModule);
134
135         verify(dataBroker).newWriteOnlyTransaction();
136         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
137                 eq(InstanceIdentifier.create(ModulesState.class)),
138                 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
139         verify(writeTransaction).submit();
140     }
141
142     @Test
143     public void testFilteringNonYangSchemaSourceRegistered() {
144         yangLibProvider.init();
145
146         // test empty list of schema sources registered
147         List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
148         yangLibProvider.schemaSourceRegistered(potentialSources);
149
150         verifyZeroInteractions(dataBroker, writeTransaction);
151
152         // test list of non yang schema sources registered
153         // expected behavior is to do nothing
154         potentialSources = new ArrayList<>();
155         potentialSources.add(
156                 PotentialSchemaSource.create(
157                         RevisionSourceIdentifier.create("yin-source-representation"),
158                         YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
159
160         potentialSources.add(
161                 PotentialSchemaSource.create(
162                         RevisionSourceIdentifier.create("asts-schema-source"),
163                         ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
164
165         yangLibProvider.schemaSourceRegistered(potentialSources);
166         verifyZeroInteractions(dataBroker, writeTransaction);
167
168         // add yang schema source to list
169         potentialSources.add(
170                 PotentialSchemaSource.create(
171                         RevisionSourceIdentifier.create("yang-schema-source"),
172                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
173
174         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
175         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
176         yangLibProvider.schemaSourceRegistered(potentialSources);
177         verify(dataBroker).newWriteOnlyTransaction();
178
179         ArgumentCaptor<ModulesState> modulesStateCaptor = ArgumentCaptor.forClass(ModulesState.class);
180         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
181                 eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
182         assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
183         verify(writeTransaction).submit();
184     }
185
186     @Test
187     public void testNonYangSchemaSourceUnregistered() {
188         yangLibProvider.init();
189
190         final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
191                 PotentialSchemaSource.create(
192                         RevisionSourceIdentifier.create("yin-source-representation"),
193                 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
194
195         yangLibProvider.schemaSourceUnregistered(nonYangSource);
196
197         // expected behaviour is to do nothing if non yang based source is unregistered
198         verifyZeroInteractions(dataBroker, writeTransaction);
199     }
200
201     @Test
202     public void testSchemaSourceUnregistered() {
203         yangLibProvider.init();
204
205         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
206         doNothing().when(writeTransaction)
207                 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
208
209         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
210
211         PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
212                 PotentialSchemaSource.create(
213                         RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
214                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
215
216         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
217
218         verify(dataBroker).newWriteOnlyTransaction();
219         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
220                 eq(InstanceIdentifier.create(ModulesState.class)
221                         .child(Module.class,
222                                 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
223                                         new OptionalRevision("")))));
224
225         verify(writeTransaction).submit();
226
227         yangUnregistererSource =
228                 PotentialSchemaSource.create(
229                         RevisionSourceIdentifier.create("unregistered-yang-with-revision", Revision.of("2016-04-28")),
230                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
231
232         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
233
234         verify(dataBroker, times(2)).newWriteOnlyTransaction();
235         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
236                 eq(InstanceIdentifier.create(ModulesState.class)
237                         .child(Module.class,
238                                 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
239                                         new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
240
241         verify(writeTransaction, times(2)).submit();
242     }
243 }