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