Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / test / java / org / opendaylight / controller / netconf / confignetconfconnector / NetconfMappingTest.java
index c78c80fe2bb849b97650061b87ab038f9d52321f..9a9f9c5de1bfa2ddee50f516eaa051ff6cd738e2 100644 (file)
@@ -19,12 +19,13 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
+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;
@@ -70,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;
@@ -87,8 +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.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;
@@ -97,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;
@@ -112,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;
@@ -146,7 +147,7 @@ public class NetconfMappingTest extends AbstractConfigTest {
     private TestImplModuleFactory factory4;
 
     @Mock
-    YangStoreContext yangStoreSnapshot;
+    YangStoreService yangStoreSnapshot;
     @Mock
     NetconfOperationRouter netconfOperationRouter;
     @Mock
@@ -156,6 +157,8 @@ public class NetconfMappingTest extends AbstractConfigTest {
 
     private TransactionProvider transactionProvider;
 
+    private ConfigSubsystemFacade configSubsystemFacade;
+
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
@@ -166,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() {
@@ -192,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 {
@@ -281,6 +287,21 @@ public class NetconfMappingTest extends AbstractConfigTest {
                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
                 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
 
+        edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
+        config = getConfigCandidate();
+        assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
+                "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
+                "ref_from_code_to_instance-from-code_1"));
+
+        try {
+            edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
+            fail("Should've failed, non-existing service instance");
+        } 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");
         config = getConfigCandidate();
         assertCorrectServiceNames(config, Collections.<String>emptySet());
@@ -430,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);
@@ -444,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);
@@ -514,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);
         }
     }
 
@@ -527,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");
@@ -540,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);
         }
     }
 
@@ -560,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);
@@ -580,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;
@@ -611,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();
@@ -619,11 +637,33 @@ public class NetconfMappingTest extends AbstractConfigTest {
 
     @Test
     public void testEx2() throws Exception {
+        //check abort before tx creation
         assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
+
+        //check abort after tx creation
+        edit("netconfMessages/editConfig.xml");
+        assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
+    }
+
+    @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();
+
+        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 (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");
     }
 
@@ -697,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);
@@ -758,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");
@@ -776,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");
     }
 
@@ -790,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);