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 */
018package org.apache.oozie.command.coord;
019
020import java.io.IOException;
021import java.io.StringReader;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.lang.StringUtils;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.oozie.CoordinatorActionBean;
029import org.apache.oozie.CoordinatorJobBean;
030import org.apache.oozie.ErrorCode;
031import org.apache.oozie.XException;
032import org.apache.oozie.client.rest.RestConstants;
033import org.apache.oozie.command.CommandException;
034import org.apache.oozie.command.SLAAlertsXCommand;
035import org.apache.oozie.coord.CoordUtils;
036import org.apache.oozie.executor.jpa.CoordJobQueryExecutor;
037import org.apache.oozie.executor.jpa.JPAExecutorException;
038import org.apache.oozie.sla.SLAOperations;
039import org.apache.oozie.util.XConfiguration;
040import org.apache.oozie.util.XmlUtils;
041import org.jdom.Element;
042import org.jdom.JDOMException;
043
044public abstract class CoordSLAAlertsXCommand extends SLAAlertsXCommand {
045
046    private String scope;
047    private String dates;
048    private List<String> actionIds;
049
050    @Override
051    protected void loadState() throws CommandException {
052        actionIds = getActionListForScopeAndDate(getJobId(), scope, dates);
053
054    }
055
056    public CoordSLAAlertsXCommand(String jobId, String name, String type, String actions, String dates) {
057        super(jobId, name, type);
058        this.scope = actions;
059        this.dates = dates;
060
061    }
062
063    /**
064     * Update job conf.
065     *
066     * @param newConf the new conf
067     * @throws CommandException the command exception
068     */
069    protected void updateJobConf(Configuration newConf) throws CommandException {
070
071        try {
072            CoordinatorJobBean job = new CoordinatorJobBean();
073            XConfiguration conf = null;
074            conf = getJobConf();
075            XConfiguration.copy(newConf, conf);
076            job.setId(getJobId());
077            job.setConf(XmlUtils.prettyPrint(conf).toString());
078            CoordJobQueryExecutor.getInstance().executeUpdate(
079                    CoordJobQueryExecutor.CoordJobQuery.UPDATE_COORD_JOB_CONF, job);
080        }
081
082        catch (XException e) {
083            throw new CommandException(e);
084        }
085    }
086
087    /**
088     * Update job sla.
089     *
090     * @param newParams the new params
091     * @throws CommandException the command exception
092     */
093    protected void updateJobSLA(Map<String, String> newParams) throws CommandException {
094
095        try {
096
097            CoordinatorJobBean job = CoordJobQueryExecutor.getInstance().get(
098                    CoordJobQueryExecutor.CoordJobQuery.GET_COORD_JOB_XML, getJobId());
099
100            Element eAction;
101            try {
102                eAction = XmlUtils.parseXml(job.getJobXml());
103            }
104            catch (JDOMException e) {
105                throw new CommandException(ErrorCode.E1005, e.getMessage(), e);
106            }
107            Element eSla = eAction.getChild("action", eAction.getNamespace()).getChild("info",
108                    eAction.getNamespace("sla"));
109
110            if (newParams != null) {
111                if (newParams.get(RestConstants.SLA_NOMINAL_TIME) != null) {
112                    updateSlaTagElement(eSla, SLAOperations.NOMINAL_TIME,
113                            newParams.get(RestConstants.SLA_NOMINAL_TIME));
114                }
115                if (newParams.get(RestConstants.SLA_SHOULD_START) != null) {
116                    updateSlaTagElement(eSla, SLAOperations.SHOULD_START,
117                            newParams.get(RestConstants.SLA_SHOULD_START));
118                }
119                if (newParams.get(RestConstants.SLA_SHOULD_END) != null) {
120                    updateSlaTagElement(eSla, SLAOperations.SHOULD_END, newParams.get(RestConstants.SLA_SHOULD_END));
121                }
122                if (newParams.get(RestConstants.SLA_MAX_DURATION) != null) {
123                    updateSlaTagElement(eSla, SLAOperations.MAX_DURATION,
124                            newParams.get(RestConstants.SLA_MAX_DURATION));
125                }
126            }
127
128            String actualXml = XmlUtils.prettyPrint(eAction).toString();
129            job.setJobXml(actualXml);
130            job.setId(getJobId());
131
132            CoordJobQueryExecutor.getInstance().executeUpdate(CoordJobQueryExecutor.CoordJobQuery.UPDATE_COORD_JOB_XML,
133                    job);
134        }
135        catch (XException e) {
136            throw new CommandException(e);
137        }
138
139    }
140
141    /**
142     * Gets the action and date list as string.
143     *
144     * @return the action date list as string
145     */
146    protected String getActionDateListAsString() {
147        StringBuffer bf = new StringBuffer();
148        if (!StringUtils.isEmpty(dates)) {
149            bf.append(dates);
150        }
151
152        if (!StringUtils.isEmpty(scope)) {
153            if (!StringUtils.isEmpty(bf.toString())) {
154                bf.append(",");
155            }
156            bf.append(scope);
157        }
158
159        return bf.toString();
160
161    }
162
163    /**
164     * Gets the action list for scope and date.
165     *
166     * @param id the id
167     * @param scope the scope
168     * @param dates the dates
169     * @return the action list for scope and date
170     * @throws CommandException the command exception
171     */
172    private List<String> getActionListForScopeAndDate(String id, String scope, String dates) throws CommandException {
173        List<String> actionIds = new ArrayList<String>();
174
175        if (scope == null && dates == null) {
176            return null;
177        }
178        List<String> parsed = new ArrayList<String>();
179        if (dates != null) {
180            List<CoordinatorActionBean> actionSet = CoordUtils.getCoordActionsFromDates(id, dates, true);
181            for (CoordinatorActionBean action : actionSet) {
182                actionIds.add(action.getId());
183            }
184            parsed.addAll(actionIds);
185        }
186        if (scope != null) {
187            parsed.addAll(CoordUtils.getActionsIds(id, scope));
188        }
189        return parsed;
190    }
191
192    /**
193     * Gets the action list.
194     *
195     * @return the action list
196     */
197    protected List<String> getActionList() {
198        return actionIds;
199    }
200
201    protected boolean isJobRequest() {
202        return StringUtils.isEmpty(dates) && StringUtils.isEmpty(scope);
203    }
204
205
206    /**
207     * Update Sla tag element.
208     *
209     * @param elem the elem
210     * @param tagName the tag name
211     * @param value the value
212     */
213    public void updateSlaTagElement(Element elem, String tagName, String value) {
214        if (elem != null && elem.getChild(tagName, elem.getNamespace("sla")) != null) {
215            elem.getChild(tagName, elem.getNamespace("sla")).setText(value);
216        }
217    }
218
219    protected XConfiguration getJobConf() throws JPAExecutorException, CommandException {
220        CoordinatorJobBean job = CoordJobQueryExecutor.getInstance().get(
221                CoordJobQueryExecutor.CoordJobQuery.GET_COORD_JOB_CONF, getJobId());
222        String jobConf = job.getConf();
223        XConfiguration conf = null;
224        try {
225            conf = new XConfiguration(new StringReader(jobConf));
226        }
227        catch (IOException e) {
228            throw new CommandException(ErrorCode.E1005, e.getMessage(), e);
229        }
230        return conf;
231    }
232
233}