<commit_before>/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * Modifications:
 * 
 * Created: March 2, 2009
 *
 * Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 *      OpenNMS Licensing       <<EMAIL>>
 *      http://www.opennms.org/
 *      http://www.opennms.com/
 */
package org.opennms.netmgt.provision;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.opennms.core.concurrent.PausibleScheduledThreadPoolExecutor;

/**
 * This class takes the work out of scheduling and queuing calls from the provisioner.  Each provisioning
 * adapter can extend this class for this functionality.  The interface API methods are final so that
 * the child class can not implement them and override the queuing for what would be the point.  (Unless
 * of course we decide that it is worth while to override a subset of the API methods.
 * 
 * To use the class, have your provisioning adapter extend this abstract class.  You see that you must
 * implement abstract methods to compile.
 * 
 * To change the schedule, override the createScheduleForNode method and return a schedule suitable for
 * the node.  In this base class, the same schedule is used for all nodes.
 * 
 * TODO: Add logging
 * TODO: Verify correct Exception handling
 * TODO: Write tests (especially the equals method of the NodeOperation for proper queue handling)
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 *
 */
public abstract class SimpleQueuedProvisioningAdapter implements ProvisioningAdapter {
    
    private final BlockingQueue<AdapterOperation> m_operQueue = new LinkedBlockingQueue<AdapterOperation>();
    private volatile ScheduledExecutorService m_executorService;
    
    protected SimpleQueuedProvisioningAdapter(ScheduledExecutorService executorService) {
        m_executorService = executorService;
    }
    
    protected SimpleQueuedProvisioningAdapter() {
        this(createDefaultSchedulerService());
    }

    private static PausibleScheduledThreadPoolExecutor createDefaultSchedulerService() {
        PausibleScheduledThreadPoolExecutor executorService = new PausibleScheduledThreadPoolExecutor(1);
        
        return executorService;
    }

    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#getName()
     */
    public abstract String getName();
    
    /**
     * This method is called when the sc
     * Adapters extending this class must implement this method
     */
    public abstract boolean isNodeReady(int nodeId);
    public abstract void processPendingOperationsForNode(List<AdapterOperation> ops) throws ProvisioningAdapterException;
    

    /**
     * Override this method to change the default schedule
     * @return
     */
    AdapterOperationSchedule createScheduleForNode(int nodeId) {
        return new AdapterOperationSchedule();
    }
    
    final List<AdapterOperation> removeOperationsForNode(Integer nodeId) {

        List<AdapterOperation> ops = new ArrayList<AdapterOperation>();
        Iterator<AdapterOperation> opIter = m_operQueue.iterator();
        while (opIter.hasNext()) {
            AdapterOperation op = (AdapterOperation) opIter.next();
            if (op.getNodeId() == nodeId) {
                ops.add(op);
                m_operQueue.remove(op);
            }
        }
        return ops;
    }
    
    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#addNode(int)
     */
    public final void addNode(int nodeId) {
        AdapterOperation op = new AdapterOperation(Integer.valueOf(nodeId), AdapterOperationType.ADD, createScheduleForNode(nodeId));
        if (!m_operQueue.contains(op)) {
            m_operQueue.offer(op);
            op.schedule(m_executorService);
        }
    }

    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#updateNode(int)
     */
    public final void updateNode(int nodeId) {
        AdapterOperation op = new AdapterOperation(Integer.valueOf(nodeId), AdapterOperationType.UPDATE, createScheduleForNode(nodeId));
        if (!m_operQueue.contains(op)) {
            m_operQueue.offer(op);
            op.schedule(m_executorService);
        }
    }
    
    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#deleteNode(int)
     */
    public final void deleteNode(int nodeId) {
        AdapterOperation op = new AdapterOperation(Integer.valueOf(nodeId), AdapterOperationType.DELETE, createScheduleForNode(nodeId));
        if (!m_operQueue.contains(op)) {
            m_operQueue.offer(op);
            op.schedule(m_executorService);
        }
    }
    
    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#nodeConfigChanged(int)
     */
    public final void nodeConfigChanged(int nodeId) {
        AdapterOperation op = new AdapterOperation(Integer.valueOf(nodeId), AdapterOperationType.CONFIG_CHANGE, createScheduleForNode(nodeId));
        if (!m_operQueue.contains(op)) {<commit_msg>Added operation type to method that creates a schedule<commit_after>/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * Modifications:
 * 
 * Created: March 2, 2009
 *
 * Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 *      OpenNMS Licensing       <<EMAIL>>
 *      http://www.opennms.org/
 *      http://www.opennms.com/
 */
package org.opennms.netmgt.provision;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.opennms.core.concurrent.PausibleScheduledThreadPoolExecutor;

/**
 * This class takes the work out of scheduling and queuing calls from the provisioner.  Each provisioning
 * adapter can extend this class for this functionality.  The interface API methods are final so that
 * the child class can not implement them and override the queuing for what would be the point.  (Unless
 * of course we decide that it is worth while to override a subset of the API methods.
 * 
 * To use the class, have your provisioning adapter extend this abstract class.  You see that you must
 * implement abstract methods to compile.
 * 
 * To change the schedule, override the createScheduleForNode method and return a schedule suitable for
 * the node.  In this base class, the same schedule is used for all nodes.
 * 
 * TODO: Add logging
 * TODO: Verify correct Exception handling
 * TODO: Write tests (especially the equals method of the NodeOperation for proper queue handling)
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 *
 */
public abstract class SimpleQueuedProvisioningAdapter implements ProvisioningAdapter {
    
    private final BlockingQueue<AdapterOperation> m_operQueue = new LinkedBlockingQueue<AdapterOperation>();
    private volatile ScheduledExecutorService m_executorService;
    
    protected SimpleQueuedProvisioningAdapter(ScheduledExecutorService executorService) {
        m_executorService = executorService;
    }
    
    protected SimpleQueuedProvisioningAdapter() {
        this(createDefaultSchedulerService());
    }

    private static PausibleScheduledThreadPoolExecutor createDefaultSchedulerService() {
        PausibleScheduledThreadPoolExecutor executorService = new PausibleScheduledThreadPoolExecutor(1);
        
        return executorService;
    }

    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#getName()
     */
    public abstract String getName();
    
    /**
     * This method is called when the sc
     * Adapters extending this class must implement this method
     */
    public abstract boolean isNodeReady(int nodeId);
    public abstract void processPendingOperationsForNode(List<AdapterOperation> ops) throws ProvisioningAdapterException;
    

    /**
     * Override this method to change the default schedule
     * @param adapterOperationType 
     * @return
     */
    AdapterOperationSchedule createScheduleForNode(int nodeId, AdapterOperationType adapterOperationType) {
        return new AdapterOperationSchedule();
    }
    
    final List<AdapterOperation> removeOperationsForNode(Integer nodeId) {

        List<AdapterOperation> ops = new ArrayList<AdapterOperation>();
        Iterator<AdapterOperation> opIter = m_operQueue.iterator();
        while (opIter.hasNext()) {
            AdapterOperation op = (AdapterOperation) opIter.next();
            if (op.getNodeId() == nodeId) {
                ops.add(op);
                m_operQueue.remove(op);
            }
        }
        return ops;
    }
    
    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#addNode(int)
     */
    public final void addNode(int nodeId) {
        AdapterOperation op = new AdapterOperation(Integer.valueOf(nodeId), AdapterOperationType.ADD, 
                                                   createScheduleForNode(nodeId, AdapterOperationType.ADD));
        if (!m_operQueue.contains(op)) {
            m_operQueue.offer(op);
            op.schedule(m_executorService);
        }
    }

    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#updateNode(int)
     */
    public final void updateNode(int nodeId) {
        AdapterOperation op = new AdapterOperation(Integer.valueOf(nodeId), AdapterOperationType.UPDATE, 
                                                   createScheduleForNode(nodeId, AdapterOperationType.UPDATE));
        if (!m_operQueue.contains(op)) {
            m_operQueue.offer(op);
            op.schedule(m_executorService);
        }
    }
    
    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#deleteNode(int)
     */
    public final void deleteNode(int nodeId) {
        AdapterOperation op = new AdapterOperation(Integer.valueOf(nodeId), AdapterOperationType.DELETE, 
                                                   createScheduleForNode(nodeId, AdapterOperationType.DELETE));
        if (!m_operQueue.contains(op)) {
            m_operQueue.offer(op);
            op.schedule(m_executorService);
        }
    }
    
    /*
     * (non-Javadoc)
     * @see org.opennms.netmgt.provision.ProvisioningAdapter#nodeConfigChanged(int)
     */
    public final void nodeConfigChanged(int nodeId) {
        AdapterOperation op = new AdapterOperation(Integer.valueOf(nodeId), AdapterOperationType.CONFIG_CHANGE, 
                                                   createScheduleForNode(nodeId, AdapterOperationType.CONFIG_CHANGE));
        if (!m_operQueue.contains(op)) {
{'fraction_non_alphanumeric': 0.08006877990430622, 'fraction_numerical': 0.004336124401913876, 'mean_word_length': 3.1907894736842106, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '686267', 'n_tokens_mistral': 3656, 'n_tokens_neox': 3553, 'n_words': 1104}