6f9a62af1a4857f6c17d6d62c880248c49da42d0
[controller.git] / opendaylight / netconf / config-netconf-connector / src / test / java / org / opendaylight / controller / netconf / confignetconfconnector / NetconfMappingTest.java
1 /*
2  * Copyright (c) 2013 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.confignetconfconnector;
10
11 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyNoMoreInteractions;
21 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElement;
22 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
23 import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToElement;
24
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.ImmutableMap;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Maps;
30 import com.google.common.collect.Sets;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.math.BigInteger;
35 import java.net.URISyntaxException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Map.Entry;
44 import java.util.Set;
45 import javax.management.InstanceAlreadyExistsException;
46 import javax.management.InstanceNotFoundException;
47 import javax.management.ObjectName;
48 import javax.xml.parsers.ParserConfigurationException;
49 import org.custommonkey.xmlunit.AbstractNodeTester;
50 import org.custommonkey.xmlunit.NodeTest;
51 import org.custommonkey.xmlunit.NodeTestException;
52 import org.custommonkey.xmlunit.NodeTester;
53 import org.custommonkey.xmlunit.XMLAssert;
54 import org.custommonkey.xmlunit.XMLUnit;
55 import org.hamcrest.CoreMatchers;
56 import org.junit.Before;
57 import org.junit.Ignore;
58 import org.junit.Test;
59 import org.mockito.Mock;
60 import org.mockito.MockitoAnnotations;
61 import org.opendaylight.controller.config.api.ConflictingVersionException;
62 import org.opendaylight.controller.config.api.ValidationException;
63 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
64 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
65 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
66 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
67 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
68 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
69 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
70 import org.opendaylight.controller.config.yang.test.impl.Deep;
71 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
72 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
73 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
74 import org.opendaylight.controller.config.yang.test.impl.DtoC;
75 import org.opendaylight.controller.config.yang.test.impl.DtoD;
76 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
77 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
78 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
79 import org.opendaylight.controller.config.yang.test.impl.Peers;
80 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
81 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
82 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
83 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
84 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
85 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
86 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Lock;
87 import org.opendaylight.controller.netconf.confignetconfconnector.operations.UnLock;
88 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
89 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
90 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
91 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
92 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreServiceImpl;
93 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
94 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
95 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
96 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
97 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
98 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
99 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
100 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
101 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
102 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
103 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
106 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
107 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
108 import org.opendaylight.yangtools.yang.model.api.Module;
109 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
110 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
111 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
112 import org.slf4j.Logger;
113 import org.slf4j.LoggerFactory;
114 import org.w3c.dom.Document;
115 import org.w3c.dom.Element;
116 import org.w3c.dom.Node;
117 import org.w3c.dom.NodeList;
118 import org.w3c.dom.Text;
119 import org.w3c.dom.traversal.DocumentTraversal;
120 import org.xml.sax.SAXException;
121
122
123 public class NetconfMappingTest extends AbstractConfigTest {
124     private static final Logger LOG = LoggerFactory.getLogger(NetconfMappingTest.class);
125
126     private static final String INSTANCE_NAME = "instance-from-code";
127     private static final String NETCONF_SESSION_ID = "foo";
128     private static final String TEST_NAMESPACE= "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
129     private NetconfTestImplModuleFactory factory;
130     private DepTestImplModuleFactory factory2;
131     private IdentityTestModuleFactory factory3;
132     private TestImplModuleFactory factory4;
133
134     @Mock
135     YangStoreSnapshot yangStoreSnapshot;
136     @Mock
137     NetconfOperationRouter netconfOperationRouter;
138     @Mock
139     NetconfOperationServiceSnapshotImpl netconfOperationServiceSnapshot;
140
141     private TransactionProvider transactionProvider;
142
143     @Before
144     public void setUp() throws Exception {
145         MockitoAnnotations.initMocks(this);
146         doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
147         doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
148         doNothing().when(netconfOperationServiceSnapshot).close();
149
150         this.factory = new NetconfTestImplModuleFactory();
151         this.factory2 = new DepTestImplModuleFactory();
152         this.factory3 = new IdentityTestModuleFactory();
153         factory4 = new TestImplModuleFactory();
154         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory, this.factory2,
155                 this.factory3, factory4));
156
157         transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
158     }
159
160     private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
161         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
162
163         final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
164         final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
165         setModule(mxBean, transaction, instanceName + "_dep");
166
167         int i = 1;
168         for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
169             ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
170             transaction.saveServiceReference(
171                     transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
172                     on);
173
174         }
175         transaction.commit();
176         return on;
177     }
178
179     @Test
180     public void testIdentityRefs() throws Exception {
181         edit("netconfMessages/editConfig_identities.xml");
182
183         commit();
184         getConfigRunning();
185     }
186
187     @Override
188     protected CodecRegistry getCodecRegistry() {
189         IdentityCodec<?> idCodec = mock(IdentityCodec.class);
190         doReturn(TestIdentity1.class).when(idCodec).deserialize(TestIdentity1.QNAME);
191         doReturn(TestIdentity2.class).when(idCodec).deserialize(TestIdentity2.QNAME);
192
193         CodecRegistry codecReg = super.getCodecRegistry();
194         doReturn(idCodec).when(codecReg).getIdentityCodec();
195         return codecReg;
196     }
197
198     @Test
199     public void testServicePersistance() throws Exception {
200         createModule(INSTANCE_NAME);
201
202         edit("netconfMessages/editConfig.xml");
203         Document config = getConfigCandidate();
204         assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
205                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
206                 "ref_from_code_to_instance-from-code_1"));
207
208
209         edit("netconfMessages/editConfig_addServiceName.xml");
210         config = getConfigCandidate();
211         assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
212                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
213                 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
214
215         edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
216         config = getConfigCandidate();
217         assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
218                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
219                 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
220
221         commit();
222         config = getConfigRunning();
223         assertCorrectRefNamesForDependencies(config);
224         assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
225                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
226                 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
227
228         edit("netconfMessages/editConfig_replace_default.xml");
229         config = getConfigCandidate();
230         assertCorrectServiceNames(config, Collections.<String>emptySet());
231
232         edit("netconfMessages/editConfig_remove.xml");
233         config = getConfigCandidate();
234         assertCorrectServiceNames(config, Collections.<String>emptySet());
235
236         commit();
237         config = getConfigCandidate();
238         assertCorrectServiceNames(config, Collections.<String>emptySet());
239
240     }
241
242     @Test
243     public void testUnLock() throws Exception {
244         assertTrue(NetconfMessageUtil.isOKMessage(lockCandidate()));
245         assertTrue(NetconfMessageUtil.isOKMessage(unlockCandidate()));
246     }
247
248     private void assertCorrectRefNamesForDependencies(Document config) throws NodeTestException {
249         NodeList modulesList = config.getElementsByTagName("modules");
250         assertEquals(1, modulesList.getLength());
251
252         NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
253         NodeTester tester = new AbstractNodeTester() {
254             private int defaultRefNameCount = 0;
255             private int userRefNameCount = 0;
256
257             @Override
258             public void testText(Text text) throws NodeTestException {
259                 if(text.getData().equals("ref_dep2")) {
260                     defaultRefNameCount++;
261                 } else if(text.getData().equals("ref_dep_user_two")) {
262                     userRefNameCount++;
263                 }
264             }
265
266             @Override
267             public void noMoreNodes(NodeTest forTest) throws NodeTestException {
268                 assertEquals(0, defaultRefNameCount);
269                 assertEquals(2, userRefNameCount);
270             }
271         };
272         nt.performTest(tester, Node.TEXT_NODE);
273     }
274
275     private void assertCorrectServiceNames(Document configCandidate, Set<String> refNames) throws NodeTestException {
276         final Set<String> refNames2 = new HashSet<>(refNames);
277         NodeList servicesNodes = configCandidate.getElementsByTagName("services");
278         assertEquals(1, servicesNodes.getLength());
279
280         NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
281         NodeTester tester = new AbstractNodeTester() {
282
283             @Override
284             public void testElement(Element element) throws NodeTestException {
285                 if(element.getNodeName() != null) {
286                     if(element.getNodeName().equals("name")) {
287                         String elmText = element.getTextContent();
288                         if(refNames2.contains(elmText)) {
289                             refNames2.remove(elmText);
290                         } else {
291                             throw new NodeTestException("Unexpected services defined: " + elmText);
292                         }
293                     }
294                 }
295             }
296
297             @Override
298             public void noMoreNodes(NodeTest forTest) throws NodeTestException {
299                 assertEquals(Collections.<String>emptySet(), refNames2);
300                 assertTrue(refNames2.toString(), refNames2.isEmpty());
301             }
302         };
303         nt.performTest(tester, Node.ELEMENT_NODE);
304     }
305
306     @Test
307     public void testConfigNetconfUnionTypes() throws Exception {
308
309         createModule(INSTANCE_NAME);
310
311         edit("netconfMessages/editConfig.xml");
312         commit();
313         Document response = getConfigRunning();
314         Element ipElement = readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
315         assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>"));
316
317         assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
318
319
320         edit("netconfMessages/editConfig_setUnions.xml");
321         commit();
322         response = getConfigRunning();
323         assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
324         assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>"));
325
326     }
327
328     @Test
329     public void testConfigNetconf() throws Exception {
330
331         createModule(INSTANCE_NAME);
332
333         edit("netconfMessages/editConfig.xml");
334         Document configCandidate = getConfigCandidate();
335         checkBinaryLeafEdited(configCandidate);
336
337
338         // default-operation:none, should not affect binary leaf
339         edit("netconfMessages/editConfig_none.xml");
340         checkBinaryLeafEdited(getConfigCandidate());
341
342         // check after edit
343         commit();
344         Document response = getConfigRunning();
345
346         checkBinaryLeafEdited(response);
347         checkTypeConfigAttribute(response);
348         checkTypedefs(response);
349         checkTestingDeps(response);
350         checkEnum(response);
351         checkBigDecimal(response);
352
353         edit("netconfMessages/editConfig_remove.xml");
354
355         commit();
356         assertXMLEqual(getConfigCandidate(), getConfigRunning());
357
358         final Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
359         XMLUnit.setIgnoreWhitespace(true);
360         assertXMLEqual(expectedResult, getConfigRunning());
361         assertXMLEqual(expectedResult, getConfigCandidate());
362
363         edit("netconfMessages/editConfig_none.xml");
364         closeSession();
365         verify(netconfOperationServiceSnapshot).close();
366         verifyNoMoreInteractions(netconfOperationRouter);
367         verifyNoMoreInteractions(netconfOperationServiceSnapshot);
368     }
369
370     private void checkBigDecimal(Document response) throws NodeTestException, SAXException, IOException {
371         assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
372         // Default
373         assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
374
375     }
376
377     private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
378             IOException {
379         DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, netconfOperationServiceSnapshot);
380         executeOp(closeOp, "netconfMessages/closeSession.xml");
381     }
382
383     private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
384             NetconfDocumentedException {
385         EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
386                 NETCONF_SESSION_ID);
387         executeOp(editOp, resource);
388     }
389
390     private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
391         Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
392         executeOp(commitOp, "netconfMessages/commit.xml");
393     }
394
395     private Document lockCandidate() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
396         Lock commitOp = new Lock(NETCONF_SESSION_ID);
397         return executeOp(commitOp, "netconfMessages/lock.xml");
398     }
399
400     private Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
401         UnLock commitOp = new UnLock(NETCONF_SESSION_ID);
402         return executeOp(commitOp, "netconfMessages/unlock.xml");
403     }
404
405     private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
406             NetconfDocumentedException {
407         GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
408                 configRegistryClient, NETCONF_SESSION_ID);
409         return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
410     }
411
412     private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
413             NetconfDocumentedException {
414         GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
415                 configRegistryClient, NETCONF_SESSION_ID);
416         return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
417     }
418
419     @Ignore("second edit message corrupted")
420     @Test(expected = NetconfDocumentedException.class)
421     public void testConfigNetconfReplaceDefaultEx() throws Exception {
422
423         createModule(INSTANCE_NAME);
424
425         edit("netconfMessages/editConfig.xml");
426         edit("netconfMessages/editConfig_replace_default_ex.xml");
427     }
428
429     @Test
430     public void testConfigNetconfReplaceDefault() throws Exception {
431
432         createModule(INSTANCE_NAME);
433
434         edit("netconfMessages/editConfig.xml");
435         commit();
436         Document response = getConfigRunning();
437         final int allInstances = response.getElementsByTagName("module").getLength();
438
439         edit("netconfMessages/editConfig_replace_default.xml");
440
441         commit();
442         response = getConfigRunning();
443
444         final int afterReplace = response.getElementsByTagName("module").getLength();
445         assertEquals(4, allInstances);
446         assertEquals(2, afterReplace);
447     }
448
449     @Test
450     public void testSameAttrDifferentNamespaces() throws Exception {
451         try {
452             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
453             fail();
454         } catch (NetconfDocumentedException e) {
455             String message = e.getMessage();
456             assertContainsString(message, "Element simpleInt present multiple times with different namespaces");
457             assertContainsString(message, TEST_NAMESPACE);
458             assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
459         }
460     }
461
462     @Test
463     public void testDifferentNamespaceInTO() throws Exception {
464         try {
465             edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
466             fail();
467         } catch (NetconfDocumentedException e) {
468             String message = e.getMessage();
469             assertContainsString(message, "Unrecognised elements");
470             assertContainsString(message, "simple-int2");
471             assertContainsString(message, "dto_d");
472         }
473     }
474
475     @Test
476     public void testSameAttrDifferentNamespacesList() throws Exception {
477         try {
478             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
479             fail();
480         } catch (NetconfDocumentedException e) {
481             String message = e.getMessage();
482             assertContainsString(message, "Element allow-user present multiple times with different namespaces");
483             assertContainsString(message, TEST_NAMESPACE);
484             assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
485         }
486     }
487
488     @Test
489     public void testTypeNameConfigAttributeMatching() throws Exception {
490         edit("netconfMessages/editConfig.xml");
491         commit();
492         edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
493         commit();
494
495         Document response = getConfigRunning();
496         checkTypeConfigAttribute(response);
497     }
498
499     // TODO add <modules operation="replace"> functionality
500     @Test(expected = NetconfDocumentedException.class)
501     public void testConfigNetconfReplaceModuleEx() throws Exception {
502
503         createModule(INSTANCE_NAME);
504
505         edit("netconfMessages/editConfig.xml");
506         edit("netconfMessages/editConfig_replace_module_ex.xml");
507     }
508
509     @Test
510     public void testUnrecognisedConfigElements() throws Exception {
511
512         String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
513         final int TESTS_COUNT = 8;
514
515         for (int i = 0; i < TESTS_COUNT; i++) {
516             String file = String.format(format, i + 1);
517             LOG.info("Reading {}", file);
518             try {
519                 edit(file);
520             } catch (NetconfDocumentedException e) {
521                 assertContainsString(e.getMessage(), "Unrecognised elements");
522                 assertContainsString(e.getMessage(), "unknownAttribute");
523                 continue;
524             }
525             fail("Unrecognised test should throw exception " + file);
526         }
527     }
528
529     @Test
530     @Ignore
531     // FIXME
532     public void testConfigNetconfReplaceModule() throws Exception {
533
534         createModule(INSTANCE_NAME);
535
536         edit("netconfMessages/editConfig.xml");
537         commit();
538         Document response = getConfigRunning();
539         final int allInstances = response.getElementsByTagName("instance").getLength();
540
541         edit("netconfMessages/editConfig_replace_module.xml");
542
543         commit();
544         response = getConfigRunning();
545         final int afterReplace = response.getElementsByTagName("instance").getLength();
546
547         assertEquals(4 + 4 /* Instances from services */, allInstances);
548         assertEquals(3 + 3, afterReplace);
549     }
550
551     @Test(expected = NetconfDocumentedException.class)
552     public void testEx() throws Exception {
553
554         commit();
555     }
556
557     @Test(expected = NetconfDocumentedException.class)
558     public void testEx2() throws Exception {
559         discard();
560     }
561
562     private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
563         DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
564         executeOp(discardOp, "netconfMessages/discardChanges.xml");
565     }
566
567     private void checkBinaryLeafEdited(final Document response) throws NodeTestException, SAXException, IOException {
568         assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
569         assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
570     }
571
572     private void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
573
574         assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
575         // Default
576         assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
577
578         assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
579         // Default
580         assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
581
582         assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>"));
583         // Default
584         assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>"));
585     }
586
587     private void assertContainsString(String string, String substring) {
588         assertThat(string, CoreMatchers.containsString(substring));
589     }
590
591     private void checkEnum(final Document response) throws Exception {
592
593         String expectedEnumContent = "TWO";
594
595         XMLAssert.assertXpathEvaluatesTo(expectedEnumContent,
596                 getXpathForNetconfImplSubnode(INSTANCE_NAME,"extended-enum"),
597                 response);
598     }
599
600     private void checkTestingDeps(Document response) {
601         int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
602         assertEquals(2, testingDepsSize);
603     }
604
605     private String getXpathForNetconfImplSubnode(String instanceName, String subnode) {
606         return "/urn:ietf:params:xml:ns:netconf:base:1.0:rpc-reply" +
607                 "/urn:ietf:params:xml:ns:netconf:base:1.0:data" +
608                 "/urn:opendaylight:params:xml:ns:yang:controller:config:modules" +
609                 "/module[name='"+instanceName+"']" +
610                 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:impl-netconf" +
611                 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:"+subnode;
612     }
613
614     private void checkTypeConfigAttribute(Document response) throws Exception {
615
616         Map<String,String> namesToTypeValues = ImmutableMap.of("instance-from-code", "configAttributeType",
617                 "test2", "default-string");
618         for (Entry<String, String> nameToExpectedValue : namesToTypeValues.entrySet()) {
619             XMLAssert.assertXpathEvaluatesTo(nameToExpectedValue.getValue(),
620                     getXpathForNetconfImplSubnode(nameToExpectedValue.getKey(),"type"),
621                     response);
622         }
623     }
624
625     private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
626         final List<InputStream> yangDependencies = getYangs();
627
628         final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
629
630         YangParserImpl yangParser = new YangParserImpl();
631         final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
632         YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() {
633             @Override
634             public SchemaContext getSchemaContext() {
635                 return schemaContext ;
636             }
637         });
638         mBeanEntries.putAll(yangStoreService.getYangStoreSnapshot().getModuleMXBeanEntryMap());
639
640         return mBeanEntries;
641     }
642
643     private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
644         SchemaContext resolveSchemaContext = getSchemaContext();
645         return resolveSchemaContext.getModules();
646     }
647
648     private SchemaContext getSchemaContext() throws Exception {
649         final List<InputStream> yangDependencies = getYangs();
650         YangParserImpl parser = new YangParserImpl();
651
652         Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
653
654         return parser.resolveSchemaContext(Sets
655                 .newHashSet(allYangModules));
656     }
657
658     @Test
659     public void testConfigNetconfRuntime() throws Exception {
660
661         createModule(INSTANCE_NAME);
662
663         edit("netconfMessages/editConfig.xml");
664         checkBinaryLeafEdited(getConfigCandidate());
665
666         // check after edit
667         commit();
668         Document response = get();
669
670         assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
671         // data from state
672         assertEquals(2, getElementsSize(response, "asdf"));
673         // data from running config
674         assertEquals(2, getElementsSize(response, "simple-short"));
675
676         assertEquals(8, getElementsSize(response, "inner-running-data"));
677         assertEquals(8, getElementsSize(response, "deep2"));
678         assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
679         assertEquals(8 * 4, getElementsSize(response, "deep3"));
680         assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
681         assertEquals(8, getElementsSize(response, "inner-running-data-additional", "urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
682         assertEquals(8, getElementsSize(response, "deep4"));
683         // TODO assert keys
684
685         RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
686
687         response = executeOp(netconf, "netconfMessages/rpc.xml");
688         assertContainsElementWithText(response, "testarg1");
689
690         response = executeOp(netconf, "netconfMessages/rpcInner.xml");
691         Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
692         XMLUnit.setIgnoreWhitespace(true);
693         XMLAssert.assertXMLEqual(expectedReplyOk, response);
694
695         response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
696         assertContainsElementWithText(response, "true");
697
698         response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
699         assertContainsElementWithText(response, "1");
700         assertContainsElementWithText(response, "2");
701     }
702
703     private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
704         Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
705         return executeOp(getOp, "netconfMessages/get.xml");
706     }
707
708     private int getElementsSize(Document response, String elementName) {
709         return response.getElementsByTagName(elementName).getLength();
710     }
711
712     private int getElementsSize(Document response, String elementName, String namespace) {
713         return response.getElementsByTagNameNS(namespace, elementName).getLength();
714     }
715
716     private Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
717             SAXException, IOException, NetconfDocumentedException {
718
719         final Document request = XmlFileLoader.xmlFileToDocument(filename);
720
721         LOG.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
722         HandlingPriority priority = op.canHandle(request);
723
724         Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
725
726         final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
727         LOG.debug("Got response\n{}", XmlUtil.toString(response));
728         return response;
729     }
730
731     private List<InputStream> getYangs() throws FileNotFoundException {
732         List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
733                 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
734                 "/META-INF/yang/test-groups.yang", "/META-INF/yang/ietf-inet-types.yang");
735         final Collection<InputStream> yangDependencies = new ArrayList<>();
736         for (String path : paths) {
737             final InputStream is = Preconditions
738                     .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
739             yangDependencies.add(is);
740         }
741         return Lists.newArrayList(yangDependencies);
742     }
743
744     private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
745             throws InstanceAlreadyExistsException, InstanceNotFoundException {
746         mxBean.setSimpleInt((long) 44);
747         mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
748         final DtoD dtob = getDtoD();
749         mxBean.setDtoD(dtob);
750         //
751         final DtoC dtoa = getDtoC();
752         mxBean.setDtoC(dtoa);
753         mxBean.setSimpleBoolean(false);
754         //
755         final Peers p1 = new Peers();
756         p1.setCoreSize(44L);
757         p1.setPort("port1");
758         p1.setSimpleInt3(456);
759         final Peers p2 = new Peers();
760         p2.setCoreSize(44L);
761         p2.setPort("port23");
762         p2.setSimpleInt3(456);
763         mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
764         // //
765         mxBean.setSimpleLong(454545L);
766         mxBean.setSimpleLong2(44L);
767         mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
768         mxBean.setSimpleByte(new Byte((byte) 4));
769         mxBean.setSimpleShort(new Short((short) 4));
770         mxBean.setSimpleTest(545);
771
772         mxBean.setComplexList(Lists.<ComplexList> newArrayList());
773         mxBean.setSimpleList(Lists.<Integer> newArrayList());
774
775         final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
776         int i = 1;
777         for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
778             ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
779             transaction.saveServiceReference(
780                     transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
781                     testingDepOn);
782
783         }
784         mxBean.setTestingDep(testingDepOn);
785     }
786
787     private static DtoD getDtoD() {
788         final DtoD dtob = new DtoD();
789         dtob.setSimpleInt1((long) 444);
790         dtob.setSimpleInt2((long) 4444);
791         dtob.setSimpleInt3(454);
792         final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
793         final Deep deep = new Deep();
794         deep.setSimpleInt3(4);
795         dtobInner.setDeep(deep);
796         dtobInner.setSimpleInt3(44);
797         dtobInner.setSimpleList(Lists.newArrayList(4));
798         dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
799         dtob.setSimpleList(Lists.newArrayList(4));
800         return dtob;
801     }
802
803     private static DtoC getDtoC() {
804         final DtoC dtoa = new DtoC();
805         // dtoa.setSimpleArg((long) 55);
806         final DtoAInner dtoAInner = new DtoAInner();
807         final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
808         dtoAInnerInner.setSimpleArg(456L);
809         dtoAInner.setDtoAInnerInner(dtoAInnerInner);
810         dtoAInner.setSimpleArg(44L);
811         dtoa.setDtoAInner(dtoAInner);
812         return dtoa;
813     }
814
815 }