All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opsmatters.newrelic.batch.parsers.MetricConditionParser Maven / Gradle / Ivy

/*
 * Copyright 2018 Gerald Curley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.opsmatters.newrelic.batch.parsers;

import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;
import com.opsmatters.newrelic.api.model.alerts.policies.AlertPolicy;
import com.opsmatters.newrelic.api.model.alerts.policies.AlertPolicyList;
import com.opsmatters.newrelic.api.model.alerts.conditions.MetricCondition;
import com.opsmatters.newrelic.api.model.Entity;
import com.opsmatters.newrelic.api.model.EntityList;
import com.opsmatters.newrelic.batch.templates.TemplateFactory;
import com.opsmatters.newrelic.batch.templates.FileInstance;

/**
 * Base class for all metric alert condition parsers.
 * 
 * @author Gerald Curley (opsmatters)
 */
public abstract class MetricConditionParser extends TermsConditionParser
{
    private static final Logger logger = Logger.getLogger(MetricConditionParser.class.getName());

    /**
     * Protected constructor.
     */
    protected MetricConditionParser()
    {
    }

    /**
     * Creates the alert conditions from the given lines.
     * @param policies The set of alert policies for the conditions
     * @param entities The set of entities for the conditions
     * @param headers The headers of the file
     * @param lines The input file lines
     * @return The alert conditions created from the lines
     */
    protected List get(List policies, List entities, String[] headers, List lines)
    {
        List ret = new ArrayList();
        FileInstance file = TemplateFactory.getTemplate(getClass()).getInstance(headers);
        AlertPolicyList policyList = new AlertPolicyList(policies);
        EntityList entityList = new EntityList(entities);
        logger.fine("Processing "+file.getType()+" file: policies="+policies.size()
            +" entities="+entities.size()+" headers="+headers.length+" lines="+lines.size());

        file.checkColumns();
        for(String[] line : lines)
        {
            // Check that the line matches the file type
            if(!file.matches(line))
            {
                logger.severe("found illegal line in "+file.getType()+" file: "+file.getType(line));
                continue;
            }

            T condition = create(file, line);
            setPolicyId(condition, file.getString(MetricCondition.POLICY_NAME, line), policyList);
            setEntities(condition, file.getString(MetricCondition.ENTITIES, line), entityList);
            ret.add(condition);
        }

        return ret;
    }

    /**
     * Sets the entities of the given condition.
     * @param condition The condition to be set
     * @param entities A comma-separated list of entity names for the condition
     * @param entityList The list of entities
     * @throws IllegalStateException if the policy is null or the id of the policy is null or empty
     */
    protected void setEntities(T condition, String entities, EntityList entityList)
    {
        if(entities != null)
            condition.setEntities(toIdList(entityList.list(entities)));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy