import com.google.common.collect.Iterables;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.Duration;
import java.util.Properties;
private static final Logger logger = LoggerFactory.getLogger(DmaapMessageConsumer.class);
- private final static Duration TIME_BETWEEN_DMAAP_POLLS = Duration.ofSeconds(10);
+ private static final Duration TIME_BETWEEN_DMAAP_POLLS = Duration.ofSeconds(10);
private final ApplicationConfig applicationConfig;
thread.start();
}
- DmaapMessageConsumer(ApplicationConfig applicationConfig, boolean start) {
- this.applicationConfig = applicationConfig;
- }
-
private boolean isDmaapConfigured() {
Properties consumerCfg = applicationConfig.getDmaapConsumerConfig();
Properties producerCfg = applicationConfig.getDmaapPublisherConfig();
}
}
- MRConsumer getMessageRouterConsumer(Properties dmaapConsumerProperties) throws FileNotFoundException, IOException {
+ MRConsumer getMessageRouterConsumer(Properties dmaapConsumerProperties) throws IOException {
return MRClientFactory.createConsumer(dmaapConsumerProperties);
}
return new AsyncRestClient(agentBaseUrl);
}
- MRBatchingPublisher getMessageRouterPublisher(Properties dmaapPublisherProperties)
- throws FileNotFoundException, IOException {
+ MRBatchingPublisher getMessageRouterPublisher(Properties dmaapPublisherProperties) throws IOException {
return MRClientFactory.createBatchingPublisher(dmaapPublisherProperties);
}
}
try {
DmaapRequestMessage dmaapRequestMessage = gson.fromJson(msg, ImmutableDmaapRequestMessage.class);
return this.invokePolicyAgent(dmaapRequestMessage) //
- .onErrorResume(t -> handleAgentCallError(t, dmaapRequestMessage)) //
+ .onErrorResume(t -> handleAgentCallError(t, msg, dmaapRequestMessage)) //
.flatMap(
-
response -> sendDmaapResponse(response.getBody(), dmaapRequestMessage, response.getStatusCode()));
} catch (Exception e) {
logger.warn("Received unparsable message from DMAAP: {}", msg);
}
}
- private Mono<ResponseEntity<String>> handleAgentCallError(Throwable t, DmaapRequestMessage dmaapRequestMessage) {
+ private Mono<ResponseEntity<String>> handleAgentCallError(Throwable t, String originalMessage,
+ DmaapRequestMessage dmaapRequestMessage) {
logger.debug("Agent call failed: {}", t.getMessage());
HttpStatus status = HttpStatus.NOT_FOUND;
String errorMessage = t.getMessage();
WebClientResponseException exception = (WebClientResponseException) t;
status = exception.getStatusCode();
errorMessage = exception.getResponseBodyAsString();
+ } else if (t instanceof ServiceException) {
+ status = HttpStatus.BAD_REQUEST;
+ errorMessage = prepareBadOperationErrorMessage(t, originalMessage);
+
}
return sendDmaapResponse(errorMessage, dmaapRequestMessage, status) //
.flatMap(notUsed -> Mono.empty());
}
+ private String prepareBadOperationErrorMessage(Throwable t, String originalMessage) {
+ String operationParameterStart = "operation\":\"";
+ int indexOfOperationStart = originalMessage.indexOf(operationParameterStart) + operationParameterStart.length();
+ int indexOfOperationEnd = originalMessage.indexOf("\",\"", indexOfOperationStart);
+ String badOperation = originalMessage.substring(indexOfOperationStart, indexOfOperationEnd);
+ return t.getMessage().replace("null", badOperation);
+ }
+
private Mono<ResponseEntity<String>> invokePolicyAgent(DmaapRequestMessage dmaapRequestMessage) {
DmaapRequestMessage.Operation operation = dmaapRequestMessage.operation();
-
String uri = dmaapRequestMessage.url();
+
if (operation == Operation.DELETE) {
return agentClient.deleteForEntity(uri);
} else if (operation == Operation.GET) {
}
private Mono<String> handleResponseCallError(Throwable t) {
- logger.debug("Failed to respond: {}", t.getMessage());
+ logger.debug("Failed to send response to DMaaP: {}", t.getMessage());
return Mono.empty();
}
return Mono.error(new Exception("Syncronization started"));
}
- @SuppressWarnings("squid:S2629")
+ @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
private void onRicChecked(RicData ric) {
logger.debug("Ric: {} checked", ric.ric.name());
}
this.services = services;
}
- @SuppressWarnings("squid:S2629")
+ @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
public void run(Ric ric) {
logger.debug("Handling ric: {}", ric.getConfig().name());
return Flux.concat(recoverTypes, policiesDeletedInRic, policiesRecreatedInRic);
}
- @SuppressWarnings("squid:S2629")
+ @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
private void onSynchronizationComplete(Ric ric) {
logger.debug("Synchronization completed for: {}", ric.name());
ric.setState(RicState.IDLE);
}
}
- @SuppressWarnings("squid:S2629")
+ @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
private void onSynchronizationError(Ric ric, Throwable t) {
logger.warn("Synchronization failed for ric: {}, reason: {}", ric.name(), t.getMessage());
// If synchronization fails, try to remove all instances
() -> onSynchronizationComplete(ric));
}
- @SuppressWarnings("squid:S2629")
+ @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
private void onRecoveryError(Ric ric, Throwable t) {
logger.warn("Synchronization failure recovery failed for ric: {}, reason: {}", ric.name(), t.getMessage());
ric.setState(RicState.UNDEFINED);
createTask().subscribe(this::onPolicyDeleted, null, this::onComplete);
}
- @SuppressWarnings("squid:S2629")
+ @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
private void onPolicyDeleted(Policy policy) {
logger.debug("Policy deleted due to inactivity: {}, service: {}", policy.id(), policy.ownerServiceName());
}
.map(nothing -> policy));
}
- @SuppressWarnings("squid:S2629")
+ @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
private Mono<String> handleDeleteFromRicFailure(Policy policy, Throwable e) {
logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e);
return Mono.empty();
}
@Test
+ @SuppressWarnings("squid:S2699") // Tests should include assertions. This test is only for keeping the server alive,
+ // so it will only be confusing to add an assertion.
public void runMock() throws Exception {
keepServerAlive();
}
@Test
public void dmaapNotConfigured_thenDoNothing() {
- messageConsumerUnderTest = spy(new DmaapMessageConsumer(applicationConfigMock, false));
+ messageConsumerUnderTest = spy(new DmaapMessageConsumer(applicationConfigMock));
doReturn(true).when(messageConsumerUnderTest).sleep(any(Duration.class));
@Test
public void dmaapConfiguredAndNoMessages_thenPollOnce() throws Exception {
- messageConsumerUnderTest = spy(new DmaapMessageConsumer(applicationConfigMock, false));
+ messageConsumerUnderTest = spy(new DmaapMessageConsumer(applicationConfigMock));
doReturn(true, false).when(messageConsumerUnderTest).sleep(any(Duration.class));
@Test
public void dmaapConfiguredAndErrorGettingMessages_thenLogWarning() throws Exception {
- messageConsumerUnderTest = spy(new DmaapMessageConsumer(applicationConfigMock, false));
+ messageConsumerUnderTest = spy(new DmaapMessageConsumer(applicationConfigMock));
doReturn(true, false).when(messageConsumerUnderTest).sleep(any(Duration.class));
@Test
public void dmaapConfiguredAndOneMessage_thenPollOnceAndProcessMessage() throws Exception {
- messageConsumerUnderTest = spy(new DmaapMessageConsumer(applicationConfigMock, false));
+ messageConsumerUnderTest = spy(new DmaapMessageConsumer(applicationConfigMock));
doReturn(true, false).when(messageConsumerUnderTest).sleep(any(Duration.class));
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(dmaapClient).send(captor.capture());
String actualMessage = captor.getValue();
- assertThat(actualMessage.contains(HttpStatus.NOT_FOUND + "\",\"message\":\"Not implemented operation:"))
- .isTrue();
+ assertThat(actualMessage
+ .contains(HttpStatus.BAD_REQUEST + "\",\"message\":\"Not implemented operation: " + badOperation)).isTrue();
verify(dmaapClient).sendBatchWithResponse();
verifyNoMoreInteractions(dmaapClient);