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