示例#1
0
  @Override
  public void writeList() {
    int txSubmitted = 0;
    int writeCnt = 0;

    BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
    WriteTransaction tx = chain.newWriteOnlyTransaction();

    for (OuterList element : this.list) {
      InstanceIdentifier<OuterList> iid =
          InstanceIdentifier.create(TestExec.class).child(OuterList.class, element.getKey());
      if (oper == StartTestInput.Operation.PUT) {
        tx.put(LogicalDatastoreType.CONFIGURATION, iid, element);
      } else {
        tx.merge(LogicalDatastoreType.CONFIGURATION, iid, element);
      }

      writeCnt++;

      if (writeCnt == writesPerTx) {
        txSubmitted++;
        Futures.addCallback(
            tx.submit(),
            new FutureCallback<Void>() {
              @Override
              public void onSuccess(final Void result) {
                txOk++;
              }

              @Override
              public void onFailure(final Throwable t) {
                LOG.error("Transaction failed, {}", t);
                txError++;
              }
            });
        tx = chain.newWriteOnlyTransaction();
        writeCnt = 0;
      }
    }

    // *** Clean up and close the transaction chain ***
    // Submit the outstanding transaction even if it's empty and wait for it to finish
    // We need to empty the transaction chain before closing it
    try {
      txSubmitted++;
      tx.submit().checkedGet();
      txOk++;
    } catch (TransactionCommitFailedException e) {
      LOG.error("Transaction failed", e);
      txError++;
    }
    try {
      chain.close();
    } catch (IllegalStateException e) {
      LOG.error("Transaction close failed,", e);
    }
    LOG.info("Transactions: submitted {}, completed {}", txSubmitted, (txOk + txError));
  }
  @Before
  public void setUp() {
    final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture =
        Futures.immediateCheckedFuture(Optional.<Node>absent());
    Mockito.when(rTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent))
        .thenReturn(noExistNodeFuture);
    Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
    Mockito.when(dataBroker.createTransactionChain(Mockito.any(TransactionChainManager.class)))
        .thenReturn(txChainFactory);
    Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodeKeyIdent);

    final SettableFuture<RpcResult<GetAsyncReply>> settableFuture = SettableFuture.create();
    final SettableFuture<RpcResult<MultipartReply>> settableFutureMultiReply =
        SettableFuture.create();
    Mockito.when(requestContext.getFuture()).thenReturn(settableFuture);
    Mockito.doAnswer(
            new Answer<Object>() {
              @SuppressWarnings("unchecked")
              @Override
              public Object answer(final InvocationOnMock invocation) {
                settableFuture.set((RpcResult<GetAsyncReply>) invocation.getArguments()[0]);
                return null;
              }
            })
        .when(requestContext)
        .setResult(any(RpcResult.class));

    Mockito.when(requestContextMultiReply.getFuture()).thenReturn(settableFutureMultiReply);
    Mockito.doAnswer(
            new Answer<Object>() {
              @SuppressWarnings("unchecked")
              @Override
              public Object answer(final InvocationOnMock invocation) {
                settableFutureMultiReply.set(
                    (RpcResult<MultipartReply>) invocation.getArguments()[0]);
                return null;
              }
            })
        .when(requestContextMultiReply)
        .setResult(any(RpcResult.class));
    Mockito.when(txChainFactory.newWriteOnlyTransaction()).thenReturn(wTx);
    Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
    Mockito.when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
    Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);

    Mockito.when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
    Mockito.when(
            messageTranslatorPacketReceived.translate(
                any(Object.class), any(DeviceContext.class), any(Object.class)))
        .thenReturn(mock(PacketReceived.class));
    Mockito.when(
            messageTranslatorFlowCapableNodeConnector.translate(
                any(Object.class), any(DeviceContext.class), any(Object.class)))
        .thenReturn(mock(FlowCapableNodeConnector.class));
    Mockito.when(
            translatorLibrary.lookupTranslator(
                eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PacketIn.class.getName()))))
        .thenReturn(messageTranslatorPacketReceived);
    Mockito.when(
            translatorLibrary.lookupTranslator(
                eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PortGrouping.class.getName()))))
        .thenReturn(messageTranslatorFlowCapableNodeConnector);

    deviceContext =
        new DeviceContextImpl(
            connectionContext,
            deviceState,
            dataBroker,
            timer,
            messageIntelligenceAgency,
            outboundQueueProvider,
            translatorLibrary,
            txChainManager);

    xid = new Xid(atomicLong.incrementAndGet());
    xidMulti = new Xid(atomicLong.incrementAndGet());
  }