private boolean isSameEpg(RegisterEndpointInput epInput, AddressEndpoint input) { if (epInput == null || epInput.getAddressEndpointReg() == null || epInput.getAddressEndpointReg().isEmpty()) { return true; } final AddressEndpointReg epInputAddressEndpoint = epInput.getAddressEndpointReg().get(0); if (epInputAddressEndpoint.getEndpointGroup() == null || epInputAddressEndpoint.getEndpointGroup().isEmpty()) { return true; } if (input == null || input.getEndpointGroup() == null || input.getEndpointGroup().isEmpty()) { return true; } final EndpointGroupId addressEndpointGroupId = epInputAddressEndpoint.getEndpointGroup().get(0); final EndpointGroupId existingEndpointGroupId = input.getEndpointGroup().get(0); return addressEndpointGroupId.equals(existingEndpointGroupId); }
private ListenableFuture<RpcResult<Void>> chainL3EPServiceIfEpAbsent(final RegisterEndpointInput epInput) { AddressEndpointReg addressEndpoint = epInput.getAddressEndpointReg().get(0); CheckedFuture<Optional<AddressEndpoint>, ReadFailedException> existingEndpointFuture = findExistingEndPoint(addressEndpoint.getContextId(), addressEndpoint.getAddress()); return Futures.transform(existingEndpointFuture, new AsyncFunction<Optional<AddressEndpoint>, RpcResult<Void>>() { @Override public ListenableFuture<RpcResult<Void>> apply(final Optional<AddressEndpoint> input) throws Exception { final ListenableFuture<RpcResult<Void>> nextResult; if (input == null || !input.isPresent() || !isSameEpg(epInput, input.get())) { // invoke service return JdkFutureAdapters.listenInPoolThread(l3EndpointService.registerEndpoint(epInput)); } else { final String existingL3EpMsg = String.format("address-endpoint for given key already exists: %s | %s", addressEndpoint.getContextId(), addressEndpoint.getAddress() ); nextResult = Futures.immediateFailedFuture(new IllegalStateException(existingL3EpMsg)); } return nextResult; } }); }