class AlarmService {

    public Alarm subscribeToAAlarm(final Alarm alarm) {
        LOGGER.debug("Search identical alarm to: " + alarm);
        Alarm identicalAlarm = this.searchAlarm(alarm);

        if (identicalAlarm != null) {
            // Alarm already exist, enable it if needed and manage subscription
            LOGGER.debug("Identical alarm found ({}).", identicalAlarm.getId());

            // Enable alarm if it's not
            activateAlarm(identicalAlarm);

            Subscription subscriptionToAdd = alarm.getSubscriptions().iterator().next();

            // Adding subscription
            try {
                subscriptionService.addSubscription(subscriptionToAdd, identicalAlarm.getId());
                return identicalAlarm;
            } catch (DuplicateSubscriptionException exception) {
                LOGGER.debug("Adding subscription not possible, identical subscription found for alarm {}.", identicalAlarm.getId());
                return identicalAlarm;
            }
        } else {
            // Error if alarm name already exists
            Alarm alarmByName = this.searchAlarmByName(alarm.getName());

            if (alarmByName != null) {
                throw new CerebroException(ErrorCode.ALARM_DUPLICATE_NAME, "A alarm with name '" + alarm.getName() + "' already exist");
            }

            seyrenRepository.addAlarm(alarm);
            return this.searchAlarmByName(alarm.getName());
        }
    }

}