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;
020
021import java.util.Map;
022
023import org.apache.oozie.ErrorCode;
024import org.apache.oozie.client.rest.RestConstants;
025import org.apache.oozie.service.ServiceException;
026import org.apache.oozie.util.LogUtils;
027
028public abstract class SLAAlertsXCommand extends XCommand<Void> {
029
030    private String jobId;
031
032    public SLAAlertsXCommand(String jobId, String name, String type) {
033        super(name, type, 1);
034        this.jobId = jobId;
035    }
036
037    @Override
038    final protected boolean isLockRequired() {
039        return true;
040    }
041
042    @Override
043    final public String getEntityKey() {
044        return getJobId();
045    }
046
047    final public String getJobId() {
048        return jobId;
049    }
050
051    @Override
052    protected void setLogInfo() {
053        LogUtils.setLogInfo(jobId);
054    }
055
056    @Override
057    protected void loadState() throws CommandException {
058
059    }
060
061    @Override
062    protected void verifyPrecondition() throws CommandException, PreconditionException {
063    }
064
065    @Override
066    protected Void execute() throws CommandException {
067        try {
068            if (!executeSlaCommand()) {
069                if (!isJobRequest()) {
070                    throw new CommandException(ErrorCode.E1026, "No record found");
071                }
072            }
073
074        }
075        catch (ServiceException e) {
076            throw new CommandException(e);
077        }
078        updateJob();
079        return null;
080    }
081
082    @Override
083    public String getKey() {
084        return getName() + "_" + jobId;
085    }
086
087    protected void validateSLAChangeParam(Map<String, String> slaParams) throws CommandException, PreconditionException {
088        for (String key : slaParams.keySet()) {
089            if (key.equals(RestConstants.SLA_NOMINAL_TIME) || key.equals(RestConstants.SLA_SHOULD_START)
090                    || key.equals(RestConstants.SLA_SHOULD_END) || key.equals(RestConstants.SLA_MAX_DURATION)) {
091                // good.
092            }
093            else {
094                throw new CommandException(ErrorCode.E1027, "Unsupported parameter " + key);
095            }
096        }
097    }
098
099    /**
100     * Execute sla command.
101     *
102     * @return true, if successful
103     * @throws ServiceException the service exception
104     * @throws CommandException the command exception
105     */
106    protected abstract boolean executeSlaCommand() throws ServiceException, CommandException;
107
108    /**
109     * Update job.
110     *
111     * @throws CommandException the command exception
112     */
113    protected abstract void updateJob() throws CommandException;
114
115    protected abstract boolean isJobRequest() throws CommandException;
116
117}