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