Class CapabilitiesHelper
This class handles the fetching, merging, and processing of service capabilities from various sources including management tools and resource providers. It provides methods to combine activity states, format data for display, and create printable representations of service capabilities.
The class follows a reactive programming model using Mutiny's Uni for
asynchronous operations and provides comprehensive error handling for API calls.
- Since:
- 1.0
- Version:
- 1.0
- Author:
- Wanaku Team
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRecord representing a printable capability with all necessary display information.static final recordRecord representing a service configuration in printable format. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringStatus value indicating an active service.static final DurationDefault timeout duration for API calls.static final String[]Standard column names for capability display.static final StringDefault status value used when actual status is unavailable.static final StringStatus value indicating an inactive service.static final DateTimeFormatterFormatter for verbose timestamp display. -
Method Summary
Modifier and TypeMethodDescriptioncombineDataIntoCapabilities(List<?> responses) Combines fetched data from multiple sources into a list of printable capabilities.createPrintableCapability(ServiceTarget serviceTarget, Map<String, List<ActivityRecord>> activityStates) Creates a printable capability from a service target and activity states.static StringdetermineServiceStatus(ActivityRecord activityRecord) Determines the service status based on activity record.static <T> io.smallrye.mutiny.Uni<T> executeApiCall(Supplier<WanakuResponse<T>> apiCall, T defaultValue) Executes an API call with error handling and fallback to default value.static io.smallrye.mutiny.Uni<List<CapabilitiesHelper.PrintableCapability>> fetchAndMergeCapabilities(TargetsService targetsService) Fetches and merges capabilities from multiple sources asynchronously.static io.smallrye.mutiny.Uni<List<ServiceTarget>> fetchManagementTools(TargetsService targetsService) Fetches the list of management tools from the targets service.static io.smallrye.mutiny.Uni<List<ServiceTarget>> fetchResourceProviders(TargetsService targetsService) Fetches the list of resource providers from the targets service.static io.smallrye.mutiny.Uni<Map<String, List<ActivityRecord>>> fetchResourcesActivityState(TargetsService targetsService) Fetches the activity state for resource providers.static io.smallrye.mutiny.Uni<Map<String, List<ActivityRecord>>> fetchToolsActivityState(TargetsService targetsService) Fetches the activity state for management tools.static ActivityRecordfindActivityRecord(ServiceTarget serviceTarget, Map<String, List<ActivityRecord>> activityStates) Finds the activity record for a specific service target.static StringformatLastSeenTimestamp(ActivityRecord activityRecord) Formats the last seen timestamp from an activity record.static Map<String, List<ActivityRecord>> mergeActivityStates(Map<String, List<ActivityRecord>> toolsActivityState, Map<String, List<ActivityRecord>> resourcesActivityState) Merges two activity state maps into a single map.static voidprintCapabilities(List<CapabilitiesHelper.PrintableCapability> capabilities, WanakuPrinter printer) Prints a list of capabilities in table format.static voidprintCapability(CapabilitiesHelper.PrintableCapability capability, WanakuPrinter printer) Prints a single capability in map format.processServiceConfigurations(Map<String, String> configurations) Processes service configurations into printable format.
-
Field Details
-
API_TIMEOUT
Default timeout duration for API calls. -
DEFAULT_STATUS
Default status value used when actual status is unavailable.- See Also:
-
ACTIVE_STATUS
Status value indicating an active service.- See Also:
-
INACTIVE_STATUS
Status value indicating an inactive service.- See Also:
-
VERBOSE_TIMESTAMP_FORMATTER
Formatter for verbose timestamp display. Format: "EEE, MMM dd, yyyy 'at' HH:mm:ss" (e.g., "Mon, Jan 15, 2024 at 14:30:45") -
COLUMNS
Standard column names for capability display.This array defines the order and names of columns when displaying capabilities in table or map format. The order matches the fields in
CapabilitiesHelper.PrintableCapability.
-
-
Method Details
-
fetchAndMergeCapabilities
public static io.smallrye.mutiny.Uni<List<CapabilitiesHelper.PrintableCapability>> fetchAndMergeCapabilities(TargetsService targetsService) Fetches and merges capabilities from multiple sources asynchronously.This method combines data from management tools and resource providers, along with their respective activity states, to create a unified list of printable capabilities.
- Parameters:
targetsService- the service used to fetch target information- Returns:
- a
Uniemitting a list ofCapabilitiesHelper.PrintableCapabilityobjects - Throws:
NullPointerException- if targetsService is null- See Also:
-
combineDataIntoCapabilities
public static List<CapabilitiesHelper.PrintableCapability> combineDataIntoCapabilities(List<?> responses) Combines fetched data from multiple sources into a list of printable capabilities.This method expects exactly 4 responses in the following order:
- Management tools list
- Tools activity state map
- Resource providers list
- Resources activity state map
- Parameters:
responses- list containing exactly 4 responses from API calls- Returns:
- list of
CapabilitiesHelper.PrintableCapabilityobjects - Throws:
IndexOutOfBoundsException- if responses list doesn't contain exactly 4 elementsClassCastException- if response types don't match expected types
-
mergeActivityStates
public static Map<String,List<ActivityRecord>> mergeActivityStates(Map<String, List<ActivityRecord>> toolsActivityState, Map<String, List<ActivityRecord>> resourcesActivityState) Merges two activity state maps into a single map.When duplicate keys are found, the activity records from both maps are combined into a single list for that key.
- Parameters:
toolsActivityState- activity state map for management toolsresourcesActivityState- activity state map for resource providers- Returns:
- merged activity state map with immutable value lists
- Throws:
NullPointerException- if either parameter is null
-
findActivityRecord
public static ActivityRecord findActivityRecord(ServiceTarget serviceTarget, Map<String, List<ActivityRecord>> activityStates) Finds the activity record for a specific service target.Searches through the activity states map to find a matching activity record based on service name and target ID.
- Parameters:
serviceTarget- the service target to find activity record foractivityStates- map of activity states indexed by service name- Returns:
- the matching
ActivityRecordor null if not found - Throws:
NullPointerException- if either parameter is null
-
createPrintableCapability
public static CapabilitiesHelper.PrintableCapability createPrintableCapability(ServiceTarget serviceTarget, Map<String, List<ActivityRecord>> activityStates) Creates a printable capability from a service target and activity states.This method combines service target information with its corresponding activity record to create a formatted representation suitable for display.
- Parameters:
serviceTarget- the service target containing basic service informationactivityStates- map of activity states to find matching activity record- Returns:
- a
CapabilitiesHelper.PrintableCapabilitywith formatted service information - Throws:
NullPointerException- if either parameter is null
-
fetchManagementTools
public static io.smallrye.mutiny.Uni<List<ServiceTarget>> fetchManagementTools(TargetsService targetsService) Fetches the list of management tools from the targets service.- Parameters:
targetsService- the service to fetch management tools from- Returns:
- a
Uniemitting a list ofServiceTargetobjects - Throws:
NullPointerException- if targetsService is null
-
fetchToolsActivityState
public static io.smallrye.mutiny.Uni<Map<String,List<ActivityRecord>>> fetchToolsActivityState(TargetsService targetsService) Fetches the activity state for management tools.- Parameters:
targetsService- the service to fetch activity state from- Returns:
- a
Uniemitting a map of service names to activity records - Throws:
NullPointerException- if targetsService is null
-
fetchResourceProviders
public static io.smallrye.mutiny.Uni<List<ServiceTarget>> fetchResourceProviders(TargetsService targetsService) Fetches the list of resource providers from the targets service.- Parameters:
targetsService- the service to fetch resource providers from- Returns:
- a
Uniemitting a list ofServiceTargetobjects - Throws:
NullPointerException- if targetsService is null
-
fetchResourcesActivityState
public static io.smallrye.mutiny.Uni<Map<String,List<ActivityRecord>>> fetchResourcesActivityState(TargetsService targetsService) Fetches the activity state for resource providers.- Parameters:
targetsService- the service to fetch activity state from- Returns:
- a
Uniemitting a map of service names to activity records - Throws:
NullPointerException- if targetsService is null
-
executeApiCall
public static <T> io.smallrye.mutiny.Uni<T> executeApiCall(Supplier<WanakuResponse<T>> apiCall, T defaultValue) Executes an API call with error handling and fallback to default value.This method provides a consistent way to handle API calls that return
WanakuResponseobjects. If the response contains an error or if an exception occurs, the method returns the provided default value.- Type Parameters:
T- the type of data returned by the API call- Parameters:
apiCall- supplier that performs the API calldefaultValue- value to return in case of error or exception- Returns:
- a
Uniemitting either the API response data or the default value - Throws:
NullPointerException- if apiCall is nullWanakuException- if the API call fails with an exception
-
determineServiceStatus
Determines the service status based on activity record.- Parameters:
activityRecord- the activity record to check (may be null)- Returns:
ACTIVE_STATUS,INACTIVE_STATUS, orDEFAULT_STATUS
-
formatLastSeenTimestamp
Formats the last seen timestamp from an activity record.The timestamp is formatted using the system default timezone and the
VERBOSE_TIMESTAMP_FORMATTERpattern.- Parameters:
activityRecord- the activity record containing the timestamp (may be null)- Returns:
- formatted timestamp string or empty string if record is null or has no timestamp
-
processServiceConfigurations
public static List<CapabilitiesHelper.PrintableCapabilityConfiguration> processServiceConfigurations(Map<String, String> configurations) Processes service configurations into printable format.Note: This method currently returns an empty list as configuration processing requirements are not yet finalized.
- Parameters:
configurations- map of configuration key-value pairs- Returns:
- list of
CapabilitiesHelper.PrintableCapabilityConfigurationobjects (currently empty)
-
printCapabilities
public static void printCapabilities(List<CapabilitiesHelper.PrintableCapability> capabilities, WanakuPrinter printer) Prints a list of capabilities in table format.- Parameters:
capabilities- list of capabilities to printprinter- the printer to use for output- Throws:
NullPointerException- if either parameter is null
-
printCapability
public static void printCapability(CapabilitiesHelper.PrintableCapability capability, WanakuPrinter printer) Prints a single capability in map format.- Parameters:
capability- the capability to printprinter- the printer to use for output- Throws:
NullPointerException- if either parameter is null
-