SfcGeniusTsaWriter { public CompletableFuture<Void> createTerminatingServiceAction(BigInteger dpnId) { short offset = NwConstants.SFC_TRANSPORT_INGRESS_TABLE; CreateTerminatingServiceActionsInput input = new CreateTerminatingServiceActionsInputBuilder() .setDpnId(dpnId) .setInstruction(Collections.singletonList(MDSALUtil.buildAndGetGotoTableInstruction(offset, 0))) .setServiceId(SfcGeniusConstants.SFC_VNID) .build(); return CompletableFuture.supplyAsync(() -> { try { RpcResult<Void> result = itmRpcService.createTerminatingServiceActions(input).get(); if (!result.isSuccessful()) { throw new SfcGeniusRuntimeException( new RuntimeException( "Could not add terminating service action on dara plane node " + dpnId, result.getErrors().stream().findFirst().map(RpcError::getCause).orElse(null))); } } catch (InterruptedException | ExecutionException e) { throw new SfcGeniusRuntimeException( new RuntimeException( "Could not add terminating service action on dara plane node " + dpnId, e)); } return null; }, executor); } SfcGeniusTsaWriter(ItmRpcService itmRpcService, Executor executor); CompletableFuture<Void> createTerminatingServiceAction(BigInteger dpnId); CompletableFuture<Void> removeTerminatingServiceAction(BigInteger dpnId);  }
@Test public void createTerminatingServiceAction() throws Exception { BigInteger dpnid = BigInteger.valueOf(8); when(itmRpcService.createTerminatingServiceActions(any())) .thenReturn(CompletableFuture.completedFuture(rpcResult)); when(rpcResult.isSuccessful()).thenReturn(true); SfcGeniusTsaWriter writer = new SfcGeniusTsaWriter(itmRpcService, executor); CompletableFuture<Void> completableFuture = writer.createTerminatingServiceAction(dpnid); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); verify(itmRpcService).createTerminatingServiceActions(createInputCaptor.capture()); assertThat(completableFuture.isDone(), is(true)); CreateTerminatingServiceActionsInput input = createInputCaptor.getValue(); assertThat(input, notNullValue()); assertThat(input.getDpnId(), is(dpnid)); assertThat(input.getServiceId(), is(SfcGeniusConstants.SFC_VNID)); assertThat(input.getInstruction().size(), is(1)); Instruction instruction = input.getInstruction().get(0); assertThat(instruction.getInstruction(), is(instanceOf(GoToTableCase.class))); GoToTableCase goToTable = (GoToTableCase) instruction.getInstruction(); assertThat(goToTable.getGoToTable().getTableId(), is(NwConstants.SFC_TRANSPORT_INGRESS_TABLE)); }
@Test public void createTerminatingServiceActionUnsuccessful() throws Exception { BigInteger dpnid = BigInteger.ZERO; Throwable throwable = new Throwable(); when(itmRpcService.createTerminatingServiceActions(any())) .thenReturn(CompletableFuture.completedFuture(rpcResult)); when(rpcResult.isSuccessful()).thenReturn(false); when(rpcResult.getErrors()).thenReturn(Collections.singletonList(rpcError)); when(rpcError.getCause()).thenReturn(throwable); SfcGeniusTsaWriter writer = new SfcGeniusTsaWriter(itmRpcService, executor); CompletableFuture<Void> completableFuture = writer.createTerminatingServiceAction(dpnid); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); assertThat(completableFuture.isCompletedExceptionally(), is(true)); try { completableFuture.join(); } catch (CompletionException e) { assertThat(e.getCause(), is(instanceOf(SfcGeniusRuntimeException.class))); assertThat(e.getCause().getCause(), is(instanceOf(RuntimeException.class))); assertThat(e.getCause().getCause().getCause(), sameInstance(throwable)); } }
@Test public void createTerminatingServiceActionException() throws Exception { BigInteger dpnid = BigInteger.ZERO; Throwable throwable = new Throwable(); CompletableFuture<RpcResult<Void>> future = new CompletableFuture<>(); future.completeExceptionally(throwable); when(itmRpcService.createTerminatingServiceActions(any())).thenReturn(future); SfcGeniusTsaWriter writer = new SfcGeniusTsaWriter(itmRpcService, executor); CompletableFuture<Void> completableFuture = writer.createTerminatingServiceAction(dpnid); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); assertThat(completableFuture.isCompletedExceptionally(), is(true)); try { completableFuture.join(); } catch (CompletionException e) { assertThat(e.getCause(), is(instanceOf(SfcGeniusRuntimeException.class))); assertThat(e.getCause().getCause(), is(instanceOf(RuntimeException.class))); assertThat(e.getCause().getCause().getCause(), is(instanceOf(ExecutionException.class))); assertThat(e.getCause().getCause().getCause().getCause(), sameInstance(throwable)); } }