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