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