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.bundle;
020
021import java.util.Arrays;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Set;
025
026import org.apache.oozie.CoordinatorJobBean;
027import org.apache.oozie.ErrorCode;
028import org.apache.oozie.XException;
029import org.apache.oozie.command.CommandException;
030import org.apache.oozie.command.SLAAlertsXCommand;
031import org.apache.oozie.executor.jpa.CoordJobQueryExecutor;
032import org.apache.oozie.executor.jpa.CoordJobQueryExecutor.CoordJobQuery;
033import org.apache.oozie.service.ServiceException;
034
035public abstract class BundleSLAAlertsXCommand extends SLAAlertsXCommand {
036
037    private String actions;
038
039    private String dates;
040
041    private String childIds;
042
043    public BundleSLAAlertsXCommand(String jobId, String actions, String dates, String childIds) {
044        super(jobId, "SLA.command", "SLA.command");
045        this.actions = actions;
046        this.dates = dates;
047        this.childIds = childIds;
048
049    }
050
051    @Override
052    protected void loadState() throws CommandException {
053    }
054
055    /**
056     * Gets the coord jobs from bundle.
057     *
058     * @param id the bundle id
059     * @param coords the coords name/id
060     * @return the coord jobs from bundle
061     * @throws CommandException the command exception
062     */
063    protected Set<String> getCoordJobsFromBundle(String id, String coords) throws CommandException {
064        Set<String> jobs = new HashSet<String>();
065        List<CoordinatorJobBean> coordJobs;
066        try {
067            if (coords == null) {
068                coordJobs = CoordJobQueryExecutor.getInstance()
069                        .getList(CoordJobQuery.GET_COORD_JOBS_WITH_PARENT_ID, id);
070            }
071            else {
072                coordJobs = CoordJobQueryExecutor.getInstance().getList(
073                        CoordJobQuery.GET_COORD_JOBS_FOR_BUNDLE_BY_APPNAME_ID, Arrays.asList(coords.split(",")), id);
074            }
075        }
076        catch (XException e) {
077            throw new CommandException(e);
078        }
079        for (CoordinatorJobBean jobBean : coordJobs) {
080            jobs.add(jobBean.getId());
081        }
082        return jobs;
083
084    }
085
086    /**
087     * Gets the coord jobs.
088     *
089     * @return the coord jobs
090     */
091    protected String getCoordJobs() {
092        return childIds;
093    }
094
095    /**
096     * Gets the actions.
097     *
098     * @return the actions
099     */
100    protected String getActions() {
101        return actions;
102    }
103
104    /**
105     * Gets the dates.
106     *
107     * @return the dates
108     */
109    protected String getDates() {
110        return dates;
111    }
112
113    protected boolean isJobRequest() {
114        return true;
115
116    }
117
118    @Override
119    protected boolean executeSlaCommand() throws ServiceException, CommandException {
120        StringBuffer report = new StringBuffer();
121
122        Set<String> coordJobs = getCoordJobsFromBundle(getJobId(), getCoordJobs());
123
124        if (coordJobs.isEmpty()) {
125            throw new CommandException(ErrorCode.E1026, "No record found");
126        }
127        else {
128            for (String job : coordJobs) {
129                try {
130                    executeCoordCommand(job, getActions(), getDates());
131                }
132                catch (Exception e) {
133                    // Ignore exception for coords.
134                    String errorMsg = "SLA command for coord job " + job + " failed. Error message is  : " + e.getMessage();
135                    LOG.error(errorMsg, e);
136                    report.append(errorMsg).append(System.getProperty("line.separator"));
137                }
138            }
139            if (!report.toString().isEmpty()) {
140                throw new CommandException(ErrorCode.E1026, report.toString());
141            }
142            return true;
143        }
144    }
145
146    protected abstract void executeCoordCommand(String id, String actions, String dates) throws ServiceException,
147            CommandException;
148
149}