Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / test / java / org / opendaylight / controller / netconf / confignetconfconnector / NetconfMappingTest.java
index 388a131a5611642d58cb046d45b1b6da4fe9a967..9a9f9c5de1bfa2ddee50f516eaa051ff6cd738e2 100644 (file)
@@ -23,9 +23,9 @@ import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.opendaylight.controller.config.util.xml.XmlUtil.readXmlToElement;
 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElement;
 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
-import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToElement;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
@@ -71,9 +71,16 @@ import org.opendaylight.controller.config.api.ConflictingVersionException;
 import org.opendaylight.controller.config.api.ValidationException;
 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
+import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
+import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
+import org.opendaylight.controller.config.facade.xml.osgi.YangStoreService;
+import org.opendaylight.controller.config.facade.xml.transactions.TransactionProvider;
 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
+import org.opendaylight.controller.config.util.xml.DocumentedException;
+import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
 import org.opendaylight.controller.config.yang.test.impl.Deep;
@@ -88,11 +95,6 @@ import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMX
 import org.opendaylight.controller.config.yang.test.impl.Peers;
 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
-import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Lock;
@@ -101,10 +103,6 @@ import org.opendaylight.controller.netconf.confignetconfconnector.operations.edi
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
-import org.opendaylight.controller.netconf.confignetconfconnector.osgi.EnumResolver;
-import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreContext;
-import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreService;
-import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListener;
 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
@@ -116,7 +114,6 @@ import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedEx
 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
@@ -150,7 +147,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
     private TestImplModuleFactory factory4;
 
     @Mock
-    YangStoreContext yangStoreSnapshot;
+    YangStoreService yangStoreSnapshot;
     @Mock
     NetconfOperationRouter netconfOperationRouter;
     @Mock
@@ -160,6 +157,8 @@ public class NetconfMappingTest extends AbstractConfigTest {
 
     private TransactionProvider transactionProvider;
 
+    private ConfigSubsystemFacade configSubsystemFacade;
+
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
@@ -170,6 +169,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
         doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
         doReturn(new ServiceReference<?>[]{}).when(mockedContext).getServiceReferences(anyString(), anyString());
 
+        doReturn(yangStoreSnapshot).when(yangStoreSnapshot).getCurrentSnapshot();
         doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
         doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
         doReturn(new EnumResolver() {
@@ -196,6 +196,8 @@ public class NetconfMappingTest extends AbstractConfigTest {
                 this.factory3, factory4));
 
         transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
+
+        configSubsystemFacade = new ConfigSubsystemFacade(configRegistryClient, configRegistryClient, yangStoreSnapshot, "mapping-test");
     }
 
     private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
@@ -294,10 +296,10 @@ public class NetconfMappingTest extends AbstractConfigTest {
         try {
             edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
             fail("Should've failed, non-existing service instance");
-        } catch (NetconfDocumentedException e) {
-            assertEquals(e.getErrorSeverity(), ErrorSeverity.error);
-            assertEquals(e.getErrorTag(), ErrorTag.operation_failed);
-            assertEquals(e.getErrorType(), ErrorType.application);
+        } catch (DocumentedException e) {
+            assertEquals(e.getErrorSeverity(), DocumentedException.ErrorSeverity.error);
+            assertEquals(e.getErrorTag(), DocumentedException.ErrorTag.operation_failed);
+            assertEquals(e.getErrorType(), DocumentedException.ErrorType.application);
         }
 
         edit("netconfMessages/editConfig_replace_default.xml");
@@ -449,8 +451,8 @@ public class NetconfMappingTest extends AbstractConfigTest {
 
     }
 
-    private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
-            IOException {
+    private void closeSession() throws ParserConfigurationException, SAXException,
+            IOException, DocumentedException {
         final Channel channel = mock(Channel.class);
         doReturn("channel").when(channel).toString();
         final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
@@ -463,43 +465,40 @@ public class NetconfMappingTest extends AbstractConfigTest {
     }
 
     private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
-            NetconfDocumentedException {
-        EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
-                NETCONF_SESSION_ID);
+            DocumentedException {
+        EditConfig editOp = new EditConfig(configSubsystemFacade, NETCONF_SESSION_ID);
         executeOp(editOp, resource);
     }
 
-    private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
-        Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
+    private void commit() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
+        Commit commitOp = new Commit(configSubsystemFacade, NETCONF_SESSION_ID);
         executeOp(commitOp, "netconfMessages/commit.xml");
     }
 
-    private Document lockCandidate() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
+    private Document lockCandidate() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
         Lock commitOp = new Lock(NETCONF_SESSION_ID);
         return executeOp(commitOp, "netconfMessages/lock.xml");
     }
 
-    private Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
+    private Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
         UnLock commitOp = new UnLock(NETCONF_SESSION_ID);
         return executeOp(commitOp, "netconfMessages/unlock.xml");
     }
 
     private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
-            NetconfDocumentedException {
-        GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
-                configRegistryClient, NETCONF_SESSION_ID);
+            DocumentedException {
+        GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String> absent(), NETCONF_SESSION_ID);
         return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
     }
 
     private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
-            NetconfDocumentedException {
-        GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
-                configRegistryClient, NETCONF_SESSION_ID);
+            DocumentedException {
+        GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String> absent(), NETCONF_SESSION_ID);
         return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
     }
 
     @Ignore("second edit message corrupted")
-    @Test(expected = NetconfDocumentedException.class)
+    @Test(expected = DocumentedException.class)
     public void testConfigNetconfReplaceDefaultEx() throws Exception {
 
         createModule(INSTANCE_NAME);
@@ -533,11 +532,11 @@ public class NetconfMappingTest extends AbstractConfigTest {
         try {
             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
             fail();
-        } catch (NetconfDocumentedException e) {
+        } catch (DocumentedException e) {
             String message = e.getMessage();
             assertContainsString(message, "Element simpleInt present multiple times with different namespaces");
             assertContainsString(message, TEST_NAMESPACE);
-            assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
+            assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
         }
     }
 
@@ -546,7 +545,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
         try {
             edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
             fail();
-        } catch (NetconfDocumentedException e) {
+        } catch (DocumentedException e) {
             String message = e.getMessage();
             assertContainsString(message, "Unrecognised elements");
             assertContainsString(message, "simple-int2");
@@ -559,11 +558,11 @@ public class NetconfMappingTest extends AbstractConfigTest {
         try {
             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
             fail();
-        } catch (NetconfDocumentedException e) {
+        } catch (DocumentedException e) {
             String message = e.getMessage();
             assertContainsString(message, "Element allow-user present multiple times with different namespaces");
             assertContainsString(message, TEST_NAMESPACE);
-            assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
+            assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
         }
     }
 
@@ -579,7 +578,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
     }
 
     // TODO add <modules operation="replace"> functionality
-    @Test(expected = NetconfDocumentedException.class)
+    @Test(expected = DocumentedException.class)
     public void testConfigNetconfReplaceModuleEx() throws Exception {
 
         createModule(INSTANCE_NAME);
@@ -599,7 +598,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
             LOG.info("Reading {}", file);
             try {
                 edit(file);
-            } catch (NetconfDocumentedException e) {
+            } catch (DocumentedException e) {
                 assertContainsString(e.getMessage(), "Unrecognised elements");
                 assertContainsString(e.getMessage(), "unknownAttribute");
                 continue;
@@ -630,7 +629,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
         assertEquals(3 + 3, afterReplace);
     }
 
-    @Test(expected = NetconfDocumentedException.class)
+    @Test(expected = DocumentedException.class)
     public void testEx() throws Exception {
 
         commit();
@@ -648,26 +647,23 @@ public class NetconfMappingTest extends AbstractConfigTest {
 
     @Test
     public void testFailedDiscardChangesAbort() throws Exception {
+        final ConfigSubsystemFacade facade = mock(ConfigSubsystemFacade.class);
+        doThrow(new RuntimeException("Mocked runtime exception, Abort has to fail")).when(facade).abortConfiguration();
 
-        TransactionProvider mockedTxProvider = mock(TransactionProvider.class);
-        doThrow(new RuntimeException("Mocked runtime exception, Abort has to fail")).when(mockedTxProvider).abortTransaction();
-        doReturn(Optional.of(ObjectName.getInstance("dummyDomain", "DummyKey", "DummyValue"))).when(mockedTxProvider).getTransaction();
-
-        DiscardChanges discardOp = new DiscardChanges(mockedTxProvider, configRegistryClient, NETCONF_SESSION_ID);
+        DiscardChanges discardOp = new DiscardChanges(facade, NETCONF_SESSION_ID);
 
         try {
             executeOp(discardOp, "netconfMessages/discardChanges.xml");
             fail("Should've failed, abort on mocked is supposed to throw RuntimeException");
-        } catch (NetconfDocumentedException e) {
-            assertTrue(e.getErrorTag() == ErrorTag.operation_failed);
-            assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
-            assertTrue(e.getErrorType() == ErrorType.application);
+        } catch (DocumentedException e) {
+            assertTrue(e.getErrorTag() == DocumentedException.ErrorTag.operation_failed);
+            assertTrue(e.getErrorSeverity() == DocumentedException.ErrorSeverity.error);
+            assertTrue(e.getErrorType() == DocumentedException.ErrorType.application);
         }
-
     }
 
-    private Document discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
-        DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
+    private Document discard() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
+        DiscardChanges discardOp = new DiscardChanges(configSubsystemFacade, NETCONF_SESSION_ID);
         return executeOp(discardOp, "netconfMessages/discardChanges.xml");
     }
 
@@ -741,7 +737,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
             public SchemaContext getSchemaContext() {
                 return schemaContext ;
             }
-        }, mockedContext);
+        });
         final BindingRuntimeContext bindingRuntimeContext = mock(BindingRuntimeContext.class);
         doReturn(getEnumMapping()).when(bindingRuntimeContext).getEnumMapping(any(Class.class));
         yangStoreService.refresh(bindingRuntimeContext);
@@ -802,7 +798,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
         assertEquals(8, getElementsSize(response, "deep4"));
         // TODO assert keys
 
-        RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
+        RuntimeRpc netconf = new RuntimeRpc(configSubsystemFacade, NETCONF_SESSION_ID);
 
         response = executeOp(netconf, "netconfMessages/rpc.xml");
         assertContainsElementWithText(response, "testarg1");
@@ -820,8 +816,8 @@ public class NetconfMappingTest extends AbstractConfigTest {
         assertContainsElementWithText(response, "2");
     }
 
-    private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
-        Get getOp = new Get(transactionProvider, yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
+    private Document get() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
+        Get getOp = new Get(configSubsystemFacade, NETCONF_SESSION_ID);
         return executeOp(getOp, "netconfMessages/get.xml");
     }
 
@@ -834,7 +830,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
     }
 
     private Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
-            SAXException, IOException, NetconfDocumentedException {
+            SAXException, IOException, DocumentedException {
 
         final Document request = XmlFileLoader.xmlFileToDocument(filename);