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