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.util.db;
020
021import java.util.Date;
022
023import org.apache.oozie.ErrorCode;
024import org.apache.oozie.SLAEventBean;
025import org.apache.oozie.client.SLAEvent.SlaAppType;
026import org.apache.oozie.client.SLAEvent.Status;
027import org.apache.oozie.command.CommandException;
028import org.apache.oozie.service.Services;
029import org.apache.oozie.util.DateUtils;
030import org.jdom.Element;
031
032@Deprecated
033public class SLADbXOperations {
034    public static final String CLIENT_ID_TAG = "oozie:sla:client-id";
035
036    /**
037     * Create SLA registration event
038     *
039     * @param eSla SLA xml element
040     * @param slaId SLA Id
041     * @param appType SLA app type
042     * @param user user name
043     * @param groupName group name
044     * @throws Exception
045     */
046    public static SLAEventBean createSlaRegistrationEvent(Element eSla, String slaId,
047                                                 SlaAppType appType, String user, String groupName)
048            throws Exception {
049        if (eSla == null) {
050            return null;
051        }
052        SLAEventBean sla = new SLAEventBean();
053        // sla.setClientId(getTagElement( eSla, "client-id"));
054        // sla.setClientId(getClientId());
055        sla.setAppName(getTagElement(eSla, "app-name"));
056        sla.setParentClientId(getTagElement(eSla, "parent-child-id"));
057        sla.setParentSlaId(getTagElement(eSla, "parent-sla-id"));
058        String strNominalTime = getTagElement(eSla, "nominal-time");
059        if (strNominalTime == null || strNominalTime.length() == 0) {
060            throw new CommandException(ErrorCode.E1101);
061        }
062        Date nominalTime = DateUtils.parseDateOozieTZ(strNominalTime);
063        // Setting expected start time
064        String strRelExpectedStart = getTagElement(eSla, "should-start");
065        if (strRelExpectedStart != null && strRelExpectedStart.length() > 0) {
066            int relExpectedStart = Integer.parseInt(strRelExpectedStart);
067            if (relExpectedStart < 0) {
068                sla.setExpectedStart(null);
069            }
070            else {
071                Date expectedStart = new Date(nominalTime.getTime()
072                        + relExpectedStart * 60 * 1000);
073                sla.setExpectedStart(expectedStart);
074            }
075        } else {
076            sla.setExpectedStart(null);
077        }
078
079        // Setting expected end time
080        String strRelExpectedEnd = getTagElement(eSla, "should-end");
081        if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) {
082            throw new RuntimeException("should-end can't be empty");
083        }
084        int relExpectedEnd = Integer.parseInt(strRelExpectedEnd);
085        if (relExpectedEnd < 0) {
086            sla.setExpectedEnd(null);
087        }
088        else {
089            Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd * 60 * 1000);
090            sla.setExpectedEnd(expectedEnd);
091        }
092
093        sla.setNotificationMsg(getTagElement(eSla, "notification-msg"));
094        sla.setAlertContact(getTagElement(eSla, "alert-contact"));
095        sla.setDevContact(getTagElement(eSla, "dev-contact"));
096        sla.setQaContact(getTagElement(eSla, "qa-contact"));
097        sla.setSeContact(getTagElement(eSla, "se-contact"));
098        sla.setAlertFrequency(getTagElement(eSla, "alert-frequency"));
099        sla.setAlertPercentage(getTagElement(eSla, "alert-percentage"));
100
101        sla.setUpstreamApps(getTagElement(eSla, "upstream-apps"));
102
103        // Oozie defined
104        sla.setSlaId(slaId);
105        sla.setAppType(appType);
106        sla.setUser(user);
107        sla.setGroupName(groupName);
108        sla.setJobStatus(Status.CREATED);
109        sla.setStatusTimestamp(new Date());
110
111        return sla;
112
113    }
114
115    /**
116     * Create SLA status event
117     *
118     * @param id SLA Id
119     * @param status SLA status
120     * @param appType SLA app type
121     * @throws Exception
122     */
123    public static SLAEventBean createSlaStatusEvent(String id,
124                                           Status status, SlaAppType appType) throws Exception {
125        SLAEventBean sla = new SLAEventBean();
126        sla.setSlaId(id);
127        sla.setJobStatus(status);
128        sla.setAppType(appType);
129        sla.setStatusTimestamp(new Date());
130
131        return sla;
132    }
133
134    /**
135     * Create SLA status event
136     *
137     * @param slaXml SLA xml element
138     * @param id SLA Id
139     * @param stat SLA status
140     * @param appType SLA app type
141     * @throws CommandException
142     */
143    public static SLAEventBean createStatusEvent(String slaXml, String id, Status stat,
144                                       SlaAppType appType) throws CommandException {
145        if (slaXml == null || slaXml.length() == 0) {
146            return null;
147        }
148        try {
149            return createSlaStatusEvent(id, stat, appType);
150        }
151        catch (Exception e) {
152            throw new CommandException(ErrorCode.E1007, " id " + id, e.getMessage(), e);
153        }
154    }
155
156    /**
157     * Return client id
158     *
159     * @return client id
160     */
161    public static String getClientId() {
162        Services services = Services.get();
163        if (services == null) {
164            throw new RuntimeException("Services is not initialized");
165        }
166        String clientId = services.getConf().get(CLIENT_ID_TAG,
167                                                 "oozie-default-instance"); // TODO remove default
168        if (clientId == null) {
169            throw new RuntimeException(
170                    "No SLA_CLIENT_ID defined in oozie-site.xml with property name "
171                            + CLIENT_ID_TAG);
172        }
173        return clientId;
174    }
175
176    private static String getTagElement(Element elem, String tagName) {
177        if (elem != null
178                && elem.getChild(tagName, elem.getNamespace("sla")) != null) {
179            return elem.getChild(tagName, elem.getNamespace("sla")).getText()
180                    .trim();
181        }
182        else {
183            return null;
184        }
185    }
186
187}