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