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