Search in sources :

Example 1 with TransactionChainListener

use of org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener in project controller by opendaylight.

the class CrossBrokerMountPointTest method testMountPoint.

@Test
public void testMountPoint() throws ReadFailedException, TimeoutException {
    final Integer attrIntValue = 500;
    domMountPointService.createMountPoint(TLL_INSTANCE_ID_BI).addService(DOMDataBroker.class, new DOMDataBroker() {

        @Override
        public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store, final YangInstanceIdentifier path, final DOMDataChangeListener listener, final DataChangeScope triggeringScope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public DOMDataWriteTransaction newWriteOnlyTransaction() {
            throw new UnsupportedOperationException();
        }

        @Override
        public DOMDataReadWriteTransaction newReadWriteTransaction() {
            return new DOMDataReadWriteTransaction() {

                @Override
                public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
                    if (store == LogicalDatastoreType.OPERATIONAL && path.getLastPathArgument().equals(GROUP_STATISTICS_ID_BI.getLastPathArgument())) {
                        final ContainerNode data = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(AUG_CONT)).withChild(ImmutableNodes.leafNode(QName.create(AUG_CONT, "attr-int"), attrIntValue)).build();
                        return Futures.immediateCheckedFuture(Optional.<NormalizedNode<?, ?>>of(data));
                    }
                    return Futures.immediateFailedCheckedFuture(new ReadFailedException(TLL_NAME, new Exception()));
                }

                @Override
                public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Object getIdentifier() {
                    return this;
                }

                @Override
                public boolean cancel() {
                    return false;
                }

                @Override
                public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public CheckedFuture<Void, TransactionCommitFailedException> submit() {
                    throw new UnsupportedOperationException();
                }
            };
        }

        @Override
        public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
            throw new UnsupportedOperationException();
        }

        @Override
        public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
            return Collections.emptyMap();
        }
    }).register();
    final Optional<MountPoint> bindingMountPoint = bindingMountPointService.getMountPoint(TLL_INSTANCE_ID_BA);
    assertTrue(bindingMountPoint.isPresent());
    final Optional<DataBroker> dataBroker = bindingMountPoint.get().getService(DataBroker.class);
    assertTrue(dataBroker.isPresent());
    final Optional<Cont> data = dataBroker.get().newReadWriteTransaction().read(LogicalDatastoreType.OPERATIONAL, AUG_CONT_ID_BA).checkedGet(5, TimeUnit.SECONDS);
    assertTrue(data.isPresent());
    assertEquals(attrIntValue, data.get().getAttrInt());
}
Also used : DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) DOMDataBroker(org.opendaylight.controller.md.sal.dom.api.DOMDataBroker) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) DOMDataReadWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction) TransactionChainListener(org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) DOMDataBroker(org.opendaylight.controller.md.sal.dom.api.DOMDataBroker) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) Optional(com.google.common.base.Optional) DOMDataBrokerExtension(org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) TimeoutException(java.util.concurrent.TimeoutException) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) MountPoint(org.opendaylight.controller.md.sal.binding.api.MountPoint) Cont(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.top.top.level.list.list1.list1._1.Cont) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) DOMDataChangeListener(org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) Test(org.junit.Test)

Example 2 with TransactionChainListener

use of org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener in project controller by opendaylight.

the class LegacyDOMDataBrokerAdapter method createTransactionChain.

@Override
public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
    AtomicReference<DOMTransactionChain> legacyChain = new AtomicReference<>();
    org.opendaylight.mdsal.common.api.TransactionChainListener delegateListener = new org.opendaylight.mdsal.common.api.TransactionChainListener() {

        @SuppressWarnings("rawtypes")
        @Override
        public void onTransactionChainFailed(final org.opendaylight.mdsal.common.api.TransactionChain<?, ?> chain, final org.opendaylight.mdsal.common.api.AsyncTransaction<?, ?> transaction, final Throwable cause) {
            listener.onTransactionChainFailed(legacyChain.get(), (AsyncTransaction) () -> transaction.getIdentifier(), cause instanceof Exception ? SUBMIT_EX_MAPPER.apply((Exception) cause) : cause);
        }

        @Override
        public void onTransactionChainSuccessful(org.opendaylight.mdsal.common.api.TransactionChain<?, ?> chain) {
            listener.onTransactionChainSuccessful(legacyChain.get());
        }
    };
    final org.opendaylight.mdsal.dom.api.DOMTransactionChain delegateChain = delegate().createTransactionChain(delegateListener);
    legacyChain.set(new DOMTransactionChain() {

        @Override
        public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
            return new DOMDataReadOnlyTransactionAdapter(delegateChain.newReadOnlyTransaction());
        }

        @Override
        public DOMDataReadWriteTransaction newReadWriteTransaction() {
            return new DOMDataTransactionAdapter(delegateChain.newReadWriteTransaction());
        }

        @Override
        public DOMDataWriteTransaction newWriteOnlyTransaction() {
            return new DOMDataTransactionAdapter(delegateChain.newWriteOnlyTransaction());
        }

        @Override
        public void close() {
            delegateChain.close();
        }
    });
    return legacyChain.get();
}
Also used : DOMDataReadOnlyTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction) DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) AtomicReference(java.util.concurrent.atomic.AtomicReference) DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) DataStoreUnavailableException(org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException) DOMDataReadWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction) TransactionChainListener(org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener) AsyncTransaction(org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction) DOMDataWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)

Example 3 with TransactionChainListener

use of org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener in project controller by opendaylight.

the class LegacyDOMDataBrokerAdapterTest method testTransactionChain.

@SuppressWarnings("rawtypes")
@Test
public void testTransactionChain() throws Exception {
    TransactionChainListener mockListener = mock(TransactionChainListener.class);
    doNothing().when(mockListener).onTransactionChainSuccessful(anyObject());
    doNothing().when(mockListener).onTransactionChainFailed(anyObject(), anyObject(), anyObject());
    DOMTransactionChain chain = adapter.createTransactionChain(mockListener);
    // Test read-only tx
    DOMDataReadOnlyTransaction readTx = chain.newReadOnlyTransaction();
    CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture = readTx.read(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
    Optional<NormalizedNode<?, ?>> readOptional = readFuture.get();
    assertEquals("isPresent", true, readOptional.isPresent());
    assertEquals("NormalizedNode", dataNode, readOptional.get());
    // Test write-only tx
    DOMDataWriteTransaction writeTx = chain.newWriteOnlyTransaction();
    writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
    verify(mockWriteTx).write(TestModel.TEST_PATH, dataNode);
    CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
    submitFuture.get(5, TimeUnit.SECONDS);
    InOrder inOrder = inOrder(mockCommitCohort);
    inOrder.verify(mockCommitCohort).canCommit();
    inOrder.verify(mockCommitCohort).preCommit();
    inOrder.verify(mockCommitCohort).commit();
    // Test read-write tx
    DOMDataReadWriteTransaction readWriteTx = chain.newReadWriteTransaction();
    readFuture = readWriteTx.read(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
    readOptional = readFuture.get();
    assertEquals("isPresent", true, readOptional.isPresent());
    assertEquals("NormalizedNode", dataNode, readOptional.get());
    chain.close();
    verify(mockListener).onTransactionChainSuccessful(chain);
    // Test failed chain
    doReturn(Futures.immediateFailedFuture(new org.opendaylight.mdsal.common.api.TransactionCommitFailedException("mock", (Throwable) null))).when(mockCommitCohort).canCommit();
    chain = adapter.createTransactionChain(mockListener);
    writeTx = chain.newWriteOnlyTransaction();
    try {
        writeTx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
        writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
        fail("Expected TransactionCommitFailedException");
    } catch (TransactionCommitFailedException e) {
    // expected
    }
    ArgumentCaptor<AsyncTransaction> failedTx = ArgumentCaptor.forClass(AsyncTransaction.class);
    verify(mockListener).onTransactionChainFailed(eq(chain), failedTx.capture(), any(TransactionCommitFailedException.class));
}
Also used : ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) InOrder(org.mockito.InOrder) Optional(com.google.common.base.Optional) DOMDataReadOnlyTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction) DOMTransactionChain(org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) DOMDataReadWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction) TransactionChainListener(org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener) AsyncTransaction(org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) DOMDataWriteTransaction(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction) Test(org.junit.Test)

Example 4 with TransactionChainListener

use of org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener in project bgpcep by opendaylight.

the class AbstractConfig method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    doReturn(InstanceIdentifier.create(BgpRib.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.bgp.rib.Rib.class, new RibKey(RIB_ID))).when(this.rib).getInstanceIdentifier();
    doReturn(this.domTx).when(this.rib).createPeerDOMChain(any(TransactionChainListener.class));
    doAnswer(invocation -> {
        final Object[] args = invocation.getArguments();
        return getDataBroker().createTransactionChain((TransactionChainListener) args[0]);
    }).when(this.rib).createPeerChain(any(TransactionChainListener.class));
    doReturn(getDataBroker()).when(this.rib).getDataBroker();
    doReturn(AS).when(this.rib).getLocalAs();
    doReturn(mock(RIBSupportContextRegistry.class)).when(this.rib).getRibSupportContext();
    doReturn(Collections.emptySet()).when(this.rib).getLocalTablesKeys();
    doNothing().when(this.domTx).close();
    doReturn(this.domDW).when(this.domTx).newWriteOnlyTransaction();
    doNothing().when(this.domDW).put(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class), any(MapEntryNode.class));
    doNothing().when(this.domDW).delete(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class));
    doNothing().when(this.domDW).merge(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class), any(NormalizedNode.class));
    final CheckedFuture<?, ?> checkedFuture = mock(CheckedFuture.class);
    doAnswer(invocation -> {
        final Runnable callback = (Runnable) invocation.getArguments()[0];
        callback.run();
        return null;
    }).when(checkedFuture).addListener(Mockito.any(Runnable.class), Mockito.any(Executor.class));
    doReturn(checkedFuture).when(this.domDW).submit();
    doReturn(null).when(checkedFuture).checkedGet();
    doReturn(null).when(checkedFuture).get();
    doReturn(true).when(checkedFuture).isDone();
    doReturn("checkedFuture").when(checkedFuture).toString();
    doReturn(YangInstanceIdentifier.of(Rib.QNAME)).when(this.rib).getYangRibId();
    doReturn(this.dataTreeChangeService).when(this.rib).getService();
    doReturn(this.listener).when(this.dataTreeChangeService).registerDataTreeChangeListener(any(), any());
    doReturn(new BgpId("127.0.0.1")).when(this.rib).getBgpIdentifier();
    doReturn(true).when(this.future).cancel(true);
    doReturn(this.future).when(this.dispatcher).createReconnectingClient(any(InetSocketAddress.class), anyInt(), any(KeyMapping.class));
    doReturn(this.dispatcher).when(this.rib).getDispatcher();
    doReturn(java.util.Optional.of(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class))).when(this.tableTypeRegistry).getTableType(any());
    doReturn(java.util.Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
    doReturn(Collections.singleton(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class))).when(this.rib).getLocalTables();
    doNothing().when(this.bgpPeerRegistry).addPeer(any(IpAddress.class), any(BGPSessionListener.class), any(BGPSessionPreferences.class));
    doNothing().when(this.bgpPeerRegistry).removePeer(any(IpAddress.class));
    doReturn("registry").when(this.bgpPeerRegistry).toString();
    doNothing().when(this.listener).close();
    doReturn(this.bgpPeerRegistry).when(this.dispatcher).getBGPPeerRegistry();
    doReturn(this.peerTracker).when(this.rib).getPeerTracker();
    doReturn(this.policies).when(this.rib).getRibPolicies();
    doReturn(null).when(this.peerGroupLoader).getPeerGroup(any(InstanceIdentifier.class), any(String.class));
}
Also used : BGPSessionListener(org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener) InetSocketAddress(java.net.InetSocketAddress) Executor(java.util.concurrent.Executor) TransactionChainListener(org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener) BGPSessionPreferences(org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences) RibKey(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.bgp.rib.RibKey) KeyMapping(org.opendaylight.protocol.concepts.KeyMapping) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) RIBSupportContextRegistry(org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) BgpRib(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.BgpRib) Rib(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Rib) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) BgpId(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId) IpAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress) BgpTableTypeImpl(org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl) Before(org.junit.Before)

Aggregations

TransactionChainListener (org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener)4 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)3 TransactionCommitFailedException (org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException)3 DOMDataReadWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction)3 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)3 Optional (com.google.common.base.Optional)2 Test (org.junit.Test)2 AsyncTransaction (org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction)2 DOMDataReadOnlyTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction)2 DOMDataWriteTransaction (org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction)2 DOMTransactionChain (org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain)2 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)2 InetSocketAddress (java.net.InetSocketAddress)1 Executor (java.util.concurrent.Executor)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Before (org.junit.Before)1 InOrder (org.mockito.InOrder)1 DataBroker (org.opendaylight.controller.md.sal.binding.api.DataBroker)1 MountPoint (org.opendaylight.controller.md.sal.binding.api.MountPoint)1