Upgrade ietf-{inet,yang}-types to 2013-07-15
[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.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesState;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesStateBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.OptionalRevision;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.RevisionIdentifier;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.Module;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
43 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
44 import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
45 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
46 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
47 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
48
49 public class YangLibProviderTest {
50
51     @Mock
52     private BindingAwareBroker.ProviderContext context;
53
54     @Mock
55     private DataBroker dataBroker;
56
57     @Mock
58     private WriteTransaction writeTransaction;
59
60     private TestingYangLibProvider yangLibProvider;
61
62     @Before
63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65         yangLibProvider = new TestingYangLibProvider(new SharedSchemaRepository("test"), "www.fake.com", 300);
66         when(context.getSALService(eq(DataBroker.class))).thenReturn(dataBroker);
67     }
68
69     @Test
70     public void testSchemaSourceRegistered() {
71         yangLibProvider.onSessionInitiated(context);
72         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
73         doNothing().when(writeTransaction)
74                 .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
75
76         List<PotentialSchemaSource<?>> list = new ArrayList<>();
77         list.add(
78                 PotentialSchemaSource.create(
79                         RevisionSourceIdentifier.create("no-revision"),
80                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
81
82         list.add(
83                 PotentialSchemaSource.create(
84                         RevisionSourceIdentifier.create("with-revision", "2016-04-28"),
85                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
86
87         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
88         yangLibProvider.schemaSourceRegistered(list);
89
90         List<Module> newModulesList = new ArrayList<>();
91
92         Module newModule = new ModuleBuilder()
93                 .setName(new YangIdentifier("no-revision"))
94                 .setRevision(new OptionalRevision(""))
95                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
96                 .build();
97
98         newModulesList.add(newModule);
99
100         newModule = new ModuleBuilder()
101                 .setName(new YangIdentifier("with-revision"))
102                 .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28")))
103                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
104                 .build();
105
106         newModulesList.add(newModule);
107
108         verify(dataBroker).newWriteOnlyTransaction();
109         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
110                 eq(InstanceIdentifier.create(ModulesState.class)),
111                 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
112         verify(writeTransaction).submit();
113     }
114
115     @Test
116     public void testFilteringNonYangSchemaSourceRegistered() {
117         yangLibProvider.onSessionInitiated(context);
118
119         // test empty list of schema sources registered
120         List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
121         yangLibProvider.schemaSourceRegistered(potentialSources);
122
123         verifyZeroInteractions(dataBroker, writeTransaction);
124
125         // test list of non yang schema sources registered
126         // expected behavior is to do nothing
127         potentialSources = new ArrayList<>();
128         potentialSources.add(
129                 PotentialSchemaSource.create(
130                         RevisionSourceIdentifier.create("yin-source-representation"),
131                         YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
132
133         potentialSources.add(
134                 PotentialSchemaSource.create(
135                         RevisionSourceIdentifier.create("asts-schema-source"),
136                         ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
137
138         yangLibProvider.schemaSourceRegistered(potentialSources);
139         verifyZeroInteractions(dataBroker, writeTransaction);
140
141         // add yang schema source to list
142         potentialSources.add(
143                 PotentialSchemaSource.create(
144                         RevisionSourceIdentifier.create("yang-schema-source"),
145                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
146
147         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
148         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
149         yangLibProvider.schemaSourceRegistered(potentialSources);
150         verify(dataBroker).newWriteOnlyTransaction();
151
152         ArgumentCaptor<ModulesState> modulesStateCaptor = ArgumentCaptor.forClass(ModulesState.class);
153         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
154                 eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
155         assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
156         verify(writeTransaction).submit();
157     }
158
159     @Test
160     public void testNonYangSchemaSourceUnregistered() {
161         yangLibProvider.onSessionInitiated(context);
162
163         final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
164                 PotentialSchemaSource.create(
165                         RevisionSourceIdentifier.create("yin-source-representation"),
166                 YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
167
168         yangLibProvider.schemaSourceUnregistered(nonYangSource);
169
170         // expected behaviour is to do nothing if non yang based source is unregistered
171         verifyZeroInteractions(dataBroker, writeTransaction);
172     }
173
174     @Test
175     public void testSchemaSourceUnregistered() {
176         yangLibProvider.onSessionInitiated(context);
177
178         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
179         doNothing().when(writeTransaction)
180                 .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
181
182         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
183
184         PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
185                 PotentialSchemaSource.create(
186                         RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
187                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
188
189         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
190
191         verify(dataBroker).newWriteOnlyTransaction();
192         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
193                 eq(InstanceIdentifier.create(ModulesState.class)
194                         .child(Module.class,
195                                 new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
196                                         new OptionalRevision("")))));
197
198         verify(writeTransaction).submit();
199
200         yangUnregistererSource =
201                 PotentialSchemaSource.create(
202                         RevisionSourceIdentifier.create("unregistered-yang-with-revision", "2016-04-28"),
203                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
204
205         yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
206
207         verify(dataBroker, times(2)).newWriteOnlyTransaction();
208         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
209                 eq(InstanceIdentifier.create(ModulesState.class)
210                         .child(Module.class,
211                                 new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
212                                         new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
213
214         verify(writeTransaction, times(2)).submit();
215     }
216
217     private static class TestingYangLibProvider extends YangLibProvider {
218
219         public TestingYangLibProvider(SharedSchemaRepository schemaRepository, String bindingAddress, long bindingPort) {
220             super(schemaRepository, bindingAddress, bindingPort);
221         }
222
223         @Override
224         public void onSessionInitiated(BindingAwareBroker.ProviderContext providerContext) {
225             this.dataBroker = providerContext.getSALService(DataBroker.class);
226             schemaListenerRegistration = schemaRepository.registerSchemaSourceListener(this);
227         }
228     }
229
230
231 }