001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.command.coord;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.oozie.CoordinatorActionBean;
028import org.apache.oozie.ErrorCode;
029import org.apache.oozie.command.CommandException;
030import org.apache.oozie.command.PreconditionException;
031import org.apache.oozie.coord.CoordELEvaluator;
032import org.apache.oozie.coord.CoordELFunctions;
033import org.apache.oozie.executor.jpa.CoordActionQueryExecutor;
034import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery;
035import org.apache.oozie.executor.jpa.JPAExecutorException;
036import org.apache.oozie.service.ServiceException;
037import org.apache.oozie.service.Services;
038import org.apache.oozie.sla.service.SLAService;
039import org.apache.oozie.util.ELEvaluator;
040import org.apache.oozie.util.Pair;
041import org.apache.oozie.util.XmlUtils;
042import org.jdom.Element;
043
044public class CoordSLAChangeXCommand extends CoordSLAAlertsXCommand {
045
046    Map<String, String> newParams;
047
048    public CoordSLAChangeXCommand(String jobId, String actions, String dates, Map<String, String> newParams) {
049        super(jobId, "SLA.alerts.change", "SLA.alerts.change", actions, dates);
050        this.newParams = newParams;
051    }
052
053    @Override
054    protected boolean executeSlaCommand() throws ServiceException, CommandException {
055        try {
056            List<Pair<String, Map<String, String>>> idSlaDefinitionList = new ArrayList<Pair<String, Map<String, String>>>();
057            List<CoordinatorActionBean> coordinatorActionBeanList = getNotTerminatedActions();
058            Configuration conf = getJobConf();
059            for (CoordinatorActionBean coordAction : coordinatorActionBeanList) {
060                Map<String, String> slaDefinitionMap = new HashMap<String, String>(newParams);
061                for (String key : slaDefinitionMap.keySet()) {
062                    Element eAction = XmlUtils.parseXml(coordAction.getActionXml().toString());
063                    ELEvaluator evalSla = CoordELEvaluator.createSLAEvaluator(eAction, coordAction, conf);
064                    String updateValue = CoordELFunctions.evalAndWrap(evalSla, slaDefinitionMap.get(key));
065                    slaDefinitionMap.put(key, updateValue);
066                }
067                idSlaDefinitionList.add(new Pair<String, Map<String, String>>(coordAction.getId(), slaDefinitionMap));
068            }
069            return Services.get().get(SLAService.class).changeDefinition(idSlaDefinitionList);
070        }
071        catch (Exception e) {
072            throw new CommandException(ErrorCode.E1027, e.getMessage(), e);
073        }
074
075    }
076
077    @Override
078    protected void updateJob() throws CommandException {
079        if (isJobRequest()) {
080            updateJobSLA(newParams);
081        }
082    }
083
084    private List<CoordinatorActionBean> getNotTerminatedActions() throws JPAExecutorException {
085        if (isJobRequest()) {
086            return CoordActionQueryExecutor.getInstance().getList(
087                    CoordActionQuery.GET_ACTIVE_ACTIONS_JOBID_FOR_SLA_CHANGE, getJobId());
088        }
089        else {
090            return CoordActionQueryExecutor.getInstance().getList(
091                    CoordActionQuery.GET_ACTIVE_ACTIONS_IDS_FOR_SLA_CHANGE, getActionList());
092        }
093
094    }
095
096    @Override
097    protected void verifyPrecondition() throws CommandException, PreconditionException {
098        validateSLAChangeParam(newParams);
099    }
100}