BUG-2801: Added filtering for get and getConfig in netconf mdsal northbound.
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / controller / netconf / mdsal / connector / ops / NetconfMDSalMappingTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.netconf.mdsal.connector.ops;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import com.google.common.base.Preconditions;
17 import com.google.common.io.ByteSource;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.EnumMap;
25 import java.util.List;
26 import java.util.concurrent.ExecutorService;
27 import javax.xml.parsers.ParserConfigurationException;
28 import org.custommonkey.xmlunit.DetailedDiff;
29 import org.custommonkey.xmlunit.Diff;
30 import org.custommonkey.xmlunit.XMLUnit;
31 import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.opendaylight.controller.cluster.datastore.ConcurrentDOMDataBroker;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
37 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
38 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
39 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
40 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
41 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
42 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
43 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
44 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
45 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
46 import org.opendaylight.controller.netconf.mdsal.connector.ops.get.Get;
47 import org.opendaylight.controller.netconf.mdsal.connector.ops.get.GetConfig;
48 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
49 import org.opendaylight.controller.netconf.util.xml.XmlElement;
50 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
51 import org.opendaylight.controller.sal.core.api.model.SchemaService;
52 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
53 import org.opendaylight.yangtools.concepts.ListenerRegistration;
54 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
55 import org.opendaylight.yangtools.yang.common.QName;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
58 import org.opendaylight.yangtools.yang.model.api.Module;
59 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
60 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
61 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
62 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
63 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66 import org.w3c.dom.Document;
67 import org.w3c.dom.Node;
68 import org.w3c.dom.NodeList;
69 import org.xml.sax.SAXException;
70
71 public class NetconfMDSalMappingTest {
72
73     private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
74
75     private static final String RPC_REPLY_ELEMENT = "rpc-reply";
76     private static final String DATA_ELEMENT = "data";
77     private static final String FILTER_NODE = "filter";
78     private static final String GET_CONFIG = "get-config";
79     private static final QName TOP = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "top");
80     private static final QName USERS = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "users");
81     private static final QName USER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "user");
82     private static final QName MODULES = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "modules");
83     private static final QName AUGMENTED_CONTAINER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "augmented-container");
84     private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "identifier");
85     private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "choice-node");
86     private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "augmented-case");
87     private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "choice-wrapper");
88     private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "inner-choice");
89     private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "text");
90
91     private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
92             YangInstanceIdentifier.builder().node(TOP).node(MODULES).build().node(new AugmentationIdentifier(Collections.singleton(AUGMENTED_CONTAINER)));
93
94     private static Document RPC_REPLY_OK = null;
95
96     static {
97         try {
98             RPC_REPLY_OK = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
99         } catch (Exception e) {
100             LOG.debug("unable to load rpc reply ok.", e);
101             RPC_REPLY_OK = XmlUtil.newDocument();
102         }
103     }
104
105     private CurrentSchemaContext currentSchemaContext = null;
106     private SchemaContext schemaContext = null;
107     private String sessionIdForReporting = "netconf-test-session1";
108
109     private TransactionProvider transactionProvider = null;
110
111     @Before
112     public void setUp() throws Exception {
113
114         XMLUnit.setIgnoreWhitespace(true);
115         XMLUnit.setIgnoreAttributeOrder(true);
116
117         this.schemaContext = parseSchemas(getYangSchemas());
118         schemaContext.getModules();
119         final SchemaService schemaService = createSchemaService();
120
121         final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
122         final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
123
124         final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
125         datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
126         datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
127
128         ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
129                 16, 16, "CommitFutures");
130
131         final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
132         this.transactionProvider = new TransactionProvider(cdb, sessionIdForReporting);
133         this.currentSchemaContext = new CurrentSchemaContext(schemaService);
134
135     }
136
137     @Test
138     public void testEmptyDatastore() throws Exception {
139         assertEmptyDatastore(get());
140         assertEmptyDatastore(getConfigCandidate());
141         assertEmptyDatastore(getConfigRunning());
142     }
143
144     @Test
145     public void testEditRunning() throws Exception {
146
147         try {
148             edit("messages/mapping/editConfigs/editConfig_running.xml");
149             fail("Should have failed - edit config on running datastore is not supported");
150         } catch (NetconfDocumentedException e) {
151             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
152             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
153             assertTrue(e.getErrorType() == ErrorType.protocol);
154         }
155
156     }
157
158     @Test
159     public void testCandidateTransaction() throws Exception {
160
161         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
162         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
163         assertEmptyDatastore(getConfigRunning());
164
165         verifyResponse(discardChanges(), RPC_REPLY_OK);
166         assertEmptyDatastore(getConfigCandidate());
167
168     }
169
170     @Test
171     public void testEditWithCommit() throws Exception {
172
173         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
174         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
175
176         verifyResponse(commit(), RPC_REPLY_OK);
177         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
178
179         deleteDatastore();
180
181     }
182
183     @Test
184     public void testMultipleEditsWithMerge() throws Exception {
185
186         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
187         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
188         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
189         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
190         assertEmptyDatastore(getConfigRunning());
191
192         verifyResponse(commit(), RPC_REPLY_OK);
193         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
194
195         deleteDatastore();
196
197     }
198
199     @Test
200     public void testMoreComplexEditConfigs() throws Exception {
201
202         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
203         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
204
205         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
206         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
207
208         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
209         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
210
211         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
212         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
213         verifyResponse(commit(), RPC_REPLY_OK);
214
215         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
216
217         verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
218         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
219         verifyResponse(commit(), RPC_REPLY_OK);
220
221         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
222
223         deleteDatastore();
224
225     }
226
227     @Test
228     public void testLock() throws Exception {
229
230         verifyResponse(lockCandidate(), RPC_REPLY_OK);
231
232         try {
233             lock();
234             fail("Should have failed - locking of running datastore is not supported");
235         } catch (NetconfDocumentedException e) {
236             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
237             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
238             assertTrue(e.getErrorType() == ErrorType.application);
239         }
240     }
241
242     @Test
243     public void testUnlock() throws Exception {
244
245         verifyResponse(unlockCandidate(), RPC_REPLY_OK);
246
247         try {
248             unlock();
249             fail("Should have failed - unlocking of running datastore is not supported");
250         } catch (NetconfDocumentedException e) {
251             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
252             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
253             assertTrue(e.getErrorType() == ErrorType.application);
254         }
255     }
256
257     @Test
258     public void testEditWithCreate() throws Exception {
259
260         verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
261         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_create_n1_control.xml"));
262
263
264         try {
265             edit("messages/mapping/editConfigs/editConfig_create.xml");
266             fail("Create should have failed - data already exists");
267         } catch (NetconfDocumentedException e) {
268             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
269             assertTrue(e.getErrorTag() == ErrorTag.data_exists);
270             assertTrue(e.getErrorType() == ErrorType.protocol);
271         }
272
273         verifyResponse(discardChanges(), RPC_REPLY_OK);
274
275     }
276
277     @Test
278     public void testDeleteNonExisting() throws Exception {
279
280         assertEmptyDatastore(getConfigCandidate());
281         assertEmptyDatastore(getConfigRunning());
282
283         try {
284             edit("messages/mapping/editConfigs/editConfig_delete-root.xml");
285             fail("Delete should have failed - data is missing");
286         } catch (NetconfDocumentedException e) {
287             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
288             assertTrue(e.getErrorTag() == ErrorTag.data_missing);
289             assertTrue(e.getErrorType() == ErrorType.protocol);
290         }
291
292     }
293
294     @Test
295     public void testEditMissingDefaultOperation() throws Exception {
296
297         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
298         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
299         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
300
301         verifyResponse(commit(), RPC_REPLY_OK);
302         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
303
304         deleteDatastore();
305     }
306
307     @Test
308     public void testFiltering() throws Exception {
309
310         assertEmptyDatastore(getConfigCandidate());
311         assertEmptyDatastore(getConfigRunning());
312
313         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
314         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
315         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
316                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
317
318         verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
319         verifyResponse(commit(), RPC_REPLY_OK);
320
321         verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
322                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
323         verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
324                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
325         verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
326                 YangInstanceIdentifier.builder().node(TOP).build());
327         verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
328                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
329         verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
330                 YangInstanceIdentifier.builder().node(TOP).build());
331         verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
332                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
333         verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
334                 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
335
336         YangInstanceIdentifier ident = YangInstanceIdentifier.
337                 builder(AUGMENTED_CONTAINER_IN_MODULES).
338                 node(AUGMENTED_CONTAINER).
339                 node(AUGMENTED_STRING_IN_CONT).build();
340
341         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
342         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
343                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
344
345         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
346                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
347         verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
348         verifyResponse(commit(), RPC_REPLY_OK);
349
350         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
351                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
352         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
353                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE).node(INNER_CHOICE_TEXT).build());
354
355         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
356                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
357         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
358                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
359         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
360                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
361
362         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
363         verifyResponse(commit(), RPC_REPLY_OK);
364
365     }
366
367     private void verifyFilterIdentifier(String resource, YangInstanceIdentifier identifier) throws Exception{
368         TestingGetConfig getConfig = new TestingGetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
369         Document request = XmlFileLoader.xmlFileToDocument(resource);
370         YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
371         assertTrue(iid.equals(identifier));
372     }
373
374     private class TestingGetConfig extends GetConfig{
375         public TestingGetConfig(String sessionId, CurrentSchemaContext schemaContext, TransactionProvider transactionProvider) {
376             super(sessionId, schemaContext, transactionProvider);
377         }
378
379         public YangInstanceIdentifier getInstanceIdentifierFromDocument(Document request) throws NetconfDocumentedException {
380             XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG).getOnlyChildElement(FILTER_NODE);
381             return getInstanceIdentifierFromFilter(filterElement);
382         }
383     }
384
385     private void deleteDatastore() throws Exception{
386         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
387         assertEmptyDatastore(getConfigCandidate());
388
389         verifyResponse(commit(), RPC_REPLY_OK);
390         assertEmptyDatastore(getConfigRunning());
391     }
392
393     private void verifyResponse(Document response, Document template){
394         DetailedDiff dd = new DetailedDiff(new Diff(response, template));
395         dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
396         assertTrue(dd.similar());
397     }
398
399     private void assertEmptyDatastore(Document response) {
400
401         NodeList nodes = response.getChildNodes();
402         assertTrue(nodes.getLength() == 1);
403
404         assertEquals(nodes.item(0).getLocalName(),RPC_REPLY_ELEMENT);
405
406         NodeList replyNodes = nodes.item(0).getChildNodes();
407         assertTrue(replyNodes.getLength() == 1);
408
409         Node dataNode = replyNodes.item(0);
410         assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
411         assertFalse(dataNode.hasChildNodes());
412
413     }
414
415     private Document commit() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
416         Commit commit = new Commit(sessionIdForReporting, transactionProvider);
417         return executeOperation(commit, "messages/mapping/commit.xml");
418     }
419
420     private Document discardChanges() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
421         DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
422         return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
423     }
424
425     private Document edit(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
426         EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
427         return executeOperation(editConfig, resource);
428     }
429
430     private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
431         Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
432         return executeOperation(get, "messages/mapping/get.xml");
433     }
434
435     private Document getConfigRunning() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
436         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
437         return executeOperation(getConfig, "messages/mapping/getConfig.xml");
438     }
439
440     private Document getConfigCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
441         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
442         return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
443     }
444
445     private Document getConfigWithFilter(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
446         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
447         return executeOperation(getConfig, resource);
448     }
449
450     private Document lock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
451         Lock lock = new Lock(sessionIdForReporting);
452         return executeOperation(lock, "messages/mapping/lock.xml");
453     }
454
455     private Document unlock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
456         Unlock unlock = new Unlock(sessionIdForReporting);
457         return executeOperation(unlock, "messages/mapping/unlock.xml");
458     }
459
460     private Document lockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
461         Lock lock = new Lock(sessionIdForReporting);
462         return executeOperation(lock, "messages/mapping/lock_candidate.xml");
463     }
464
465     private Document unlockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
466         Unlock unlock = new Unlock(sessionIdForReporting);
467         return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
468     }
469
470     private Document executeOperation(NetconfOperation op, String filename) throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
471         final Document request = XmlFileLoader.xmlFileToDocument(filename);
472
473         HandlingPriority priority = op.canHandle(request);
474         Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
475
476         final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
477
478         LOG.debug("Got response {}" , response);
479
480         return response;
481     }
482
483     private Collection<InputStream> getYangSchemas() {
484         final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
485         final List<InputStream> schemas = new ArrayList<>();
486
487         for (String schemaPath : schemaPaths) {
488             InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
489             schemas.add(resourceAsStream);
490         }
491
492         return schemas;
493     }
494
495     private SchemaContext parseSchemas(Collection<InputStream> schemas) throws IOException, YangSyntaxErrorException {
496         final YangParserImpl parser = new YangParserImpl();
497         Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(schemas);
498         return parser.parseSources(sources);
499     }
500
501     private SchemaService createSchemaService() {
502         return new SchemaService() {
503
504             @Override
505             public void addModule(Module module) {
506             }
507
508             @Override
509             public void removeModule(Module module) {
510
511             }
512
513             @Override
514             public SchemaContext getSessionContext() {
515                 return schemaContext;
516             }
517
518             @Override
519             public SchemaContext getGlobalContext() {
520                 return schemaContext;
521             }
522
523             @Override
524             public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
525                 listener.onGlobalContextUpdated(getGlobalContext());
526                 return new ListenerRegistration<SchemaContextListener>() {
527                     @Override
528                     public void close() {
529
530                     }
531
532                     @Override
533                     public SchemaContextListener getInstance() {
534                         return listener;
535                     }
536                 };
537             }
538         };
539     }
540 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.