/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2016 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2016 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 * OpenNMS(R) Licensing <license@opennms.org>
 *      http://www.opennms.org/
 *      http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.bsm.service.internal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.opennms.core.criteria.CriteriaBuilder;
import org.opennms.netmgt.bsm.service.AlarmProvider;
import org.opennms.netmgt.bsm.service.model.AlarmWrapper;
import org.opennms.netmgt.dao.api.AlarmDao;
import org.opennms.netmgt.model.OnmsAlarm;
import org.springframework.beans.factory.annotation.Autowired;

public class AlarmProviderImpl implements AlarmProvider {

    @Autowired
    private AlarmDao alarmDao;

    @Override
    public Map<String, AlarmWrapper> lookup(Set<String> reductionKeys) {
        if (reductionKeys == null || reductionKeys.isEmpty()) {
            return new HashMap<>();
        }
        if (reductionKeys.size() <= 1000) {
            List<OnmsAlarm> alarms = alarmDao.findMatching(new CriteriaBuilder(OnmsAlarm.class).in("reductionKey", reductionKeys).toCriteria());
            return alarms.stream().collect(Collectors.toMap(alarm -> alarm.getReductionKey(), alarm -> new AlarmWrapperImpl(alarm)));
        } else {
            return alarmDao.findAll().stream()
                    .filter(a -> reductionKeys.contains(a.getReductionKey()))
                    .collect(Collectors.toMap(alarm -> alarm.getReductionKey(), alarm -> new AlarmWrapperImpl(alarm)));
        }
    }
}

@@ -1621,16 +1621,73 @@
 sAlarm;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -1801,16 +1801,321 @@
 ider %7B%0A%0A
+    private static final Logger LOG = LoggerFactory.getLogger(AlarmProviderImpl.class);%0A%0A    private static final long DEFAULT_THRESHOLD = 1000;%0A%0A    private static final String THRESHOLD_KEY = %22org.opennms.features.bsm.reductionKeyFullLoadThreshold%22;%0A%0A    private final long threshold = getThreshold();%0A%0A
     @Aut
@@ -2386,20 +2386,25 @@
 ze() %3C= 
-1000
+threshold
 ) %7B%0A    
@@ -2955,11 +2955,793 @@
  %7D%0A    %7D
+%0A%0A    private static long getThreshold() %7B%0A        String thresholdProperty = System.getProperty(THRESHOLD_KEY, Long.toString(DEFAULT_THRESHOLD));%0A        try %7B%0A            long threshold = Long.valueOf(thresholdProperty);%0A            if (threshold %3C= 0) %7B%0A                LOG.warn(%22Defined threshold must be greater than 0, but was %7B%7D. Falling back to default: %7B%7D%22, threshold, DEFAULT_THRESHOLD);%0A                return DEFAULT_THRESHOLD;%0A            %7D%0A            LOG.debug(%22Using threshold %7B%7D%22, threshold);%0A            return threshold;%0A        %7D catch (Exception ex) %7B%0A            LOG.warn(%22The defined threshold %7B%7D could not be interpreted as long value. Falling back to default: %7B%7D%22, thresholdProperty, DEFAULT_THRESHOLD);%0A            return DEFAULT_THRESHOLD;%0A        %7D%0A    %7D
 %0A%7D%0A

Make threshold configurable