use of org.opennms.netmgt.provision.DetectRequest in project opennms by OpenNMS.
the class DetectorRequestBuilderImpl method execute.
/**
* Builds the {@link DetectorRequestDTO} and executes the requested detector
* via the RPC client.
*/
@Override
public CompletableFuture<Boolean> execute() {
if (address == null) {
throw new IllegalArgumentException("Address is required.");
} else if (className == null) {
throw new IllegalArgumentException("Detector class name is required.");
}
// Retrieve the factory associated with the requested detector
final ServiceDetectorFactory<?> factory = client.getRegistry().getDetectorFactoryByClassName(className);
if (factory == null) {
// Fail immediately if no suitable factory was found
throw new IllegalArgumentException("No factory found for detector with class name '" + className + "'.");
}
// Store all of the request details in the DTO
final DetectorRequestDTO detectorRequestDTO = new DetectorRequestDTO();
detectorRequestDTO.setLocation(location);
detectorRequestDTO.setClassName(className);
detectorRequestDTO.setAddress(address);
detectorRequestDTO.addDetectorAttributes(attributes);
// Attempt to extract the port from the list of attributes
Integer port = null;
final String portString = attributes.get(PORT);
if (portString != null) {
try {
port = Integer.parseInt(portString);
} catch (NumberFormatException nfe) {
LOG.warn("Failed to parse port as integer from: ", portString);
}
}
// Build the DetectRequest and store the runtime attributes in the DTO
final DetectRequest request = factory.buildRequest(location, address, port, attributes);
detectorRequestDTO.addRuntimeAttributes(request.getRuntimeAttributes());
// Execute the request
return client.getDelegate().execute(detectorRequestDTO).thenApply(response -> {
try {
factory.afterDetect(request, response, nodeId);
} catch (Throwable t) {
LOG.error("Error while processing detect callback.", t);
}
return response.isDetected();
});
}
Aggregations