<commit_before>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2004-2014 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2014 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 <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.poller;

import static org.opennms.core.utils.InetAddressUtils.addr;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.opennms.core.criteria.Alias;
import org.opennms.core.criteria.Alias.JoinType;
import org.opennms.core.criteria.Criteria;
import org.opennms.core.criteria.restrictions.AnyRestriction;
import org.opennms.core.criteria.restrictions.EqRestriction;
import org.opennms.core.criteria.restrictions.NullRestriction;
import org.opennms.core.utils.InetAddressUtils;
import org.opennms.netmgt.dao.api.EventDao;
import org.opennms.netmgt.dao.api.IpInterfaceDao;
import org.opennms.netmgt.dao.api.MonitoredServiceDao;
import org.opennms.netmgt.dao.api.NodeDao;
import org.opennms.netmgt.dao.api.OutageDao;
import org.opennms.netmgt.dao.api.ServiceTypeDao;
import org.opennms.netmgt.events.api.EventConstants;
import org.opennms.netmgt.model.OnmsEvent;
import org.opennms.netmgt.model.OnmsMonitoredService;
import org.opennms.netmgt.model.OnmsOutage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>QueryManagerDaoImpl class.</p>
 *
 * @author brozow
 */
public class QueryManagerDaoImpl implements QueryManager {

    private static final Logger LOG = LoggerFactory.getLogger(QueryManagerDaoImpl.class);

    @Autowired
    private NodeDao m_nodeDao;

    @Autowired
    private IpInterfaceDao m_ipInterfaceDao;

    @Autowired
    private ServiceTypeDao m_serviceTypeDao;

    @Autowired
    private EventDao m_eventDao;

    @Autowired
    private OutageDao m_outageDao;

    @Autowired
    private MonitoredServiceDao m_monitoredServiceDao;

    /** {@inheritDoc} */
    @Override
    public String getNodeLabel(int nodeId) {
        return m_nodeDao.get(nodeId).getLabel();
    }

    /**
     * <p>convertEventTimeToTimeStamp</p>
     *
     * @param time a {@link java.lang.String} object.
     * @return a {@link java.sql.Timestamp} object.
     */
    private static Date convertEventTimeToTimeStamp(String time) {
        try {
            return EventConstants.parseToDate(time);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format: " + time, e);
        }
    }

    /** {@inheritDoc} */
    @Override
    public Integer openOutagePendingLostEventId(int nodeId, String ipAddr, String svcName, Date lostTime) {
        LOG.info("opening outage for {}:{}:{} @ {}", nodeId, ipAddr, svcName, lostTime);
        final OnmsMonitoredService service = m_monitoredServiceDao.get(nodeId, InetAddressUtils.addr(ipAddr), svcName);
        final OnmsOutage outage = new OnmsOutage(lostTime, service);
        m_outageDao.saveOrUpdate(outage);
        return outage.getId();
    }

    /** {@inheritDoc} */
    @Override
    public void updateOpenOutageWithEventId(int outageId, int lostEventId) {
        LOG.info("updating open outage {} with event id {}", outageId, lostEventId);

        final OnmsEvent event = m_eventDao.get(lostEventId);
        final OnmsOutage outage = m_outageDao.get(outageId);

        // Update the outage
        outage.setServiceLostEvent(event);
        m_outageDao.saveOrUpdate(outage);
    }

    /** {@inheritDoc} */
    @Override
    public Integer resolveOutagePendingRegainEventId(int nodeId, String ipAddr, String svcName, Date regainedTime) {
        LOG.info("resolving outage for {}:{}:{} @ {}", nodeId, ipAddr, svcName, regainedTime);
        final OnmsMonitoredService service = m_monitoredServiceDao.get(nodeId, InetAddressUtils.addr(ipAddr), svcName);

        final OnmsOutage outage = m_outageDao.currentOutageForService(service);
        if (outage == null) {
            return null;
        }

        // Update the outage
        outage.setIfRegainedService(new Timestamp(regainedTime.getTime()));
        m_outageDao.saveOrUpdate(outage);
        return outage.getId();
    }

    /** {@inheritDoc} */
    @Override
    public void updateResolvedOutageWithEventId(int outageId, int regainedEventId) {
        LOG.info("updating resolved outage {} with event id {}", outageId, regainedEventId);

        final OnmsEvent event = m_eventDao.get(regainedEventId);
        final OnmsOutage outage = m_outageDao.get(outageId);

        // Update the outage
        outage.setServiceRegainedEvent(event);
        m_outageDao.saveOrUpdate(outage);
    }

    /** {@inheritDoc} */
    @Override
    public void reparentOutages(String ipAddr, int oldNodeId, int newNodeId) {
        try {
            LOG.info("reparenting outages for {}:{} to new node {}", oldNodeId, ipAddr, newNodeId);

            Criteria criteria = new Criteria(OnmsOutage.class);
            criteria.setAliases(Arrays.asList(new Alias[] {
                new Alias("monitoredService.ipInterface", "ipInterface", JoinType.LEFT_JOIN),
                new Alias("ipInterface.node", "node", JoinType.LEFT_JOIN),
            }));
            criteria.addRestriction(new EqRestriction("node.id", oldNodeId));<commit_msg>Added defensive code for defered outage updates.<commit_after>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2004-2014 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2014 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 <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.poller;

import static org.opennms.core.utils.InetAddressUtils.addr;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.opennms.core.criteria.Alias;
import org.opennms.core.criteria.Alias.JoinType;
import org.opennms.core.criteria.Criteria;
import org.opennms.core.criteria.restrictions.AnyRestriction;
import org.opennms.core.criteria.restrictions.EqRestriction;
import org.opennms.core.criteria.restrictions.NullRestriction;
import org.opennms.core.utils.InetAddressUtils;
import org.opennms.netmgt.dao.api.EventDao;
import org.opennms.netmgt.dao.api.IpInterfaceDao;
import org.opennms.netmgt.dao.api.MonitoredServiceDao;
import org.opennms.netmgt.dao.api.NodeDao;
import org.opennms.netmgt.dao.api.OutageDao;
import org.opennms.netmgt.dao.api.ServiceTypeDao;
import org.opennms.netmgt.events.api.EventConstants;
import org.opennms.netmgt.model.OnmsEvent;
import org.opennms.netmgt.model.OnmsMonitoredService;
import org.opennms.netmgt.model.OnmsOutage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>QueryManagerDaoImpl class.</p>
 *
 * @author brozow
 */
public class QueryManagerDaoImpl implements QueryManager {

    private static final Logger LOG = LoggerFactory.getLogger(QueryManagerDaoImpl.class);

    @Autowired
    private NodeDao m_nodeDao;

    @Autowired
    private IpInterfaceDao m_ipInterfaceDao;

    @Autowired
    private ServiceTypeDao m_serviceTypeDao;

    @Autowired
    private EventDao m_eventDao;

    @Autowired
    private OutageDao m_outageDao;

    @Autowired
    private MonitoredServiceDao m_monitoredServiceDao;

    /** {@inheritDoc} */
    @Override
    public String getNodeLabel(int nodeId) {
        return m_nodeDao.get(nodeId).getLabel();
    }

    /**
     * <p>convertEventTimeToTimeStamp</p>
     *
     * @param time a {@link java.lang.String} object.
     * @return a {@link java.sql.Timestamp} object.
     */
    private static Date convertEventTimeToTimeStamp(String time) {
        try {
            return EventConstants.parseToDate(time);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format: " + time, e);
        }
    }

    /** {@inheritDoc} */
    @Override
    public Integer openOutagePendingLostEventId(int nodeId, String ipAddr, String svcName, Date lostTime) {
        LOG.info("opening outage for {}:{}:{} @ {}", nodeId, ipAddr, svcName, lostTime);
        final OnmsMonitoredService service = m_monitoredServiceDao.get(nodeId, InetAddressUtils.addr(ipAddr), svcName);
        final OnmsOutage outage = new OnmsOutage(lostTime, service);
        m_outageDao.saveOrUpdate(outage);
        return outage.getId();
    }

    /** {@inheritDoc} */
    @Override
    public void updateOpenOutageWithEventId(int outageId, int lostEventId) {
        LOG.info("updating open outage {} with event id {}", outageId, lostEventId);

        final OnmsEvent event = m_eventDao.get(lostEventId);
        final OnmsOutage outage = m_outageDao.get(outageId);
        if (outage == null) {
            LOG.warn("Failed to update outage {} with event id {}. The outage no longer exists.",
                    outageId, lostEventId);
            return;
        }

        // Update the outage
        outage.setServiceLostEvent(event);
        m_outageDao.saveOrUpdate(outage);
    }

    /** {@inheritDoc} */
    @Override
    public Integer resolveOutagePendingRegainEventId(int nodeId, String ipAddr, String svcName, Date regainedTime) {
        LOG.info("resolving outage for {}:{}:{} @ {}", nodeId, ipAddr, svcName, regainedTime);
        final OnmsMonitoredService service = m_monitoredServiceDao.get(nodeId, InetAddressUtils.addr(ipAddr), svcName);
        if (service == null) {
            LOG.warn("Failed to resolve the pending outage for {}:{}:{} @ {}. The service could not be found.",
                    nodeId, ipAddr, svcName, regainedTime);
            return null;
        }

        final OnmsOutage outage = m_outageDao.currentOutageForService(service);
        if (outage == null) {
            return null;
        }

        // Update the outage
        outage.setIfRegainedService(new Timestamp(regainedTime.getTime()));
        m_outageDao.saveOrUpdate(outage);
        return outage.getId();
    }

    /** {@inheritDoc} */
    @Override
    public void updateResolvedOutageWithEventId(int outageId, int regainedEventId) {
        LOG.info("updating resolved outage {} with event id {}", outageId, regainedEventId);

        final OnmsEvent event = m_eventDao.get(regainedEventId);
        final OnmsOutage outage = m_outageDao.get(outageId);
        if (outage == null) {
            LOG.warn("Failed to update outage {} with event id {}. The outage no longer exists.",
                    outageId, regainedEventId);
            return;
        }

        // Update the outage
        outage.setServiceRegainedEvent(event);
        m_outageDao.saveOrUpdate(outage);
    }

    /** {@inheritDoc} */
    @Override
    public void reparentOutages(String ipAddr, int oldNodeId, int newNodeId) {
        try {
            LOG.info("reparenting outages for {}:{} to new node {}", oldNodeId, ipAddr, newNodeId);

            Criteria criteria = new Criteria(OnmsOutage.class);
            criteria.setAliases(Arrays.asList(new Alias[] {
                new Alias("monitoredService.ipInterface", "ipInterface", JoinType.LEFT_JOIN),
                new Alias("ipInterface.node", "node", JoinType.LEFT_JOIN),
            }));
            criteria.addRestriction(new EqRestriction("node.id", oldNodeId));
{'fraction_non_alphanumeric': 0.12532462714253914, 'fraction_numerical': 0.0028196186094828226, 'mean_word_length': 3.4146740910579756, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5399423', 'n_tokens_mistral': 4154, 'n_tokens_neox': 4143, 'n_words': 963}