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.store.Store;
030import org.apache.oozie.util.DateUtils;
031import org.apache.oozie.util.XLog;
032import org.apache.oozie.util.XmlUtils;
033import org.jdom.Element;
034
035@Deprecated
036public class SLADbOperations {
037    public static final String CLIENT_ID_TAG = "oozie:sla:client-id";
038
039    public static SLAEventBean createSlaRegistrationEvent(Element eSla, Store store, String slaId, SlaAppType appType, String user,
040            String groupName) throws Exception {
041        if (eSla == null) {
042            return null;
043        }
044        SLAEventBean sla = new SLAEventBean();
045        sla.setAppName(getTagElement(eSla, "app-name"));
046        sla.setParentClientId(getTagElement(eSla, "parent-child-id"));
047        sla.setParentSlaId(getTagElement(eSla, "parent-sla-id"));
048        String strNominalTime = getTagElement(eSla, "nominal-time");
049        if (strNominalTime == null || strNominalTime.length() == 0) {
050            throw new RuntimeException("Nominal time is required"); // TODO:
051            // change to
052            // CommandException
053        }
054        Date nominalTime = DateUtils.parseDateOozieTZ(strNominalTime);
055        // Setting expected start time
056        String strRelExpectedStart = getTagElement(eSla, "should-start");
057        if (strRelExpectedStart != null && strRelExpectedStart.length() > 0) {
058            int relExpectedStart = Integer.parseInt(strRelExpectedStart);
059            if (relExpectedStart < 0) {
060                sla.setExpectedStart(null);
061            }
062            else {
063                Date expectedStart = new Date(nominalTime.getTime() + relExpectedStart * 60 * 1000);
064                sla.setExpectedStart(expectedStart);
065            }
066        } else {
067            sla.setExpectedStart(null);
068        }
069
070        // Setting expected end time
071        String strRelExpectedEnd = getTagElement(eSla, "should-end");
072        if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) {
073            throw new RuntimeException("should-end can't be empty");
074        }
075        int relExpectedEnd = Integer.parseInt(strRelExpectedEnd);
076        if (relExpectedEnd < 0) {
077            sla.setExpectedEnd(null);
078        }
079        else {
080            Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd * 60 * 1000);
081            sla.setExpectedEnd(expectedEnd);
082        }
083
084        sla.setNotificationMsg(getTagElement(eSla, "notification-msg"));
085        sla.setAlertContact(getTagElement(eSla, "alert-contact"));
086        sla.setDevContact(getTagElement(eSla, "dev-contact"));
087        sla.setQaContact(getTagElement(eSla, "qa-contact"));
088        sla.setSeContact(getTagElement(eSla, "se-contact"));
089        sla.setAlertFrequency(getTagElement(eSla, "alert-frequency"));
090        sla.setAlertPercentage(getTagElement(eSla, "alert-percentage"));
091
092        sla.setUpstreamApps(getTagElement(eSla, "upstream-apps"));
093
094        // Oozie defined
095
096        sla.setSlaId(slaId);
097        sla.setAppType(appType);
098        sla.setUser(user);
099        sla.setGroupName(groupName);
100        sla.setJobStatus(Status.CREATED);
101        sla.setStatusTimestamp(new Date());
102
103        return sla;
104    }
105
106    public static SLAEventBean createSlaRegistrationEvent(Element eSla,
107                                                 String slaId, SlaAppType appType, String user, String groupName, XLog log)
108            throws Exception {
109        if (eSla == null) {
110            return null;
111        }
112        SLAEventBean sla = new SLAEventBean();
113        sla.setAppName(getTagElement(eSla, "app-name"));
114        sla.setParentClientId(getTagElement(eSla, "parent-child-id"));
115        sla.setParentSlaId(getTagElement(eSla, "parent-sla-id"));
116        String strNominalTime = getTagElement(eSla, "nominal-time");
117        if (strNominalTime == null || strNominalTime.length() == 0) {
118            throw new RuntimeException("Nominal time is required"); // TODO:
119            // change to
120            // CommandException
121        }
122        Date nominalTime = DateUtils.parseDateOozieTZ(strNominalTime);
123        // Setting expected start time
124        String strRelExpectedStart = getTagElement(eSla, "should-start");
125        if (strRelExpectedStart != null && strRelExpectedStart.length() > 0) {
126            int relExpectedStart = Integer.parseInt(strRelExpectedStart);
127            if (relExpectedStart < 0) {
128                sla.setExpectedStart(null);
129            }
130            else {
131                Date expectedStart = new Date(nominalTime.getTime() + relExpectedStart * 60 * 1000);
132                sla.setExpectedStart(expectedStart);
133            }
134        } else {
135            sla.setExpectedStart(null);
136        }
137
138        // Setting expected end time
139        String strRelExpectedEnd = getTagElement(eSla, "should-end");
140        if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) {
141            throw new RuntimeException("should-end can't be empty");
142        }
143        int relExpectedEnd = Integer.parseInt(strRelExpectedEnd);
144        if (relExpectedEnd < 0) {
145            sla.setExpectedEnd(null);
146        }
147        else {
148            Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd
149                    * 60 * 1000);
150            sla.setExpectedEnd(expectedEnd);
151        }
152
153        sla.setNotificationMsg(getTagElement(eSla, "notification-msg"));
154        sla.setAlertContact(getTagElement(eSla, "alert-contact"));
155        sla.setDevContact(getTagElement(eSla, "dev-contact"));
156        sla.setQaContact(getTagElement(eSla, "qa-contact"));
157        sla.setSeContact(getTagElement(eSla, "se-contact"));
158        sla.setAlertFrequency(getTagElement(eSla, "alert-frequency"));
159        sla.setAlertPercentage(getTagElement(eSla, "alert-percentage"));
160
161        sla.setUpstreamApps(getTagElement(eSla, "upstream-apps"));
162
163        // Oozie defined
164
165        sla.setSlaId(slaId);
166        sla.setAppType(appType);
167        sla.setUser(user);
168        sla.setGroupName(groupName);
169        sla.setJobStatus(Status.CREATED);
170        sla.setStatusTimestamp(new Date());
171
172        return sla;
173    }
174
175    public static SLAEventBean createSlaStatusEvent(String id, Status status, SlaAppType appType, String appName,
176            XLog log) throws Exception {
177        SLAEventBean sla = new SLAEventBean();
178        sla.setSlaId(id);
179        sla.setJobStatus(status);
180        sla.setAppType(appType);
181        sla.setAppName(appName);
182        sla.setStatusTimestamp(new Date());
183        return sla;
184    }
185
186    public static SLAEventBean createStatusEvent(String slaXml, String id, Status stat, SlaAppType appType, XLog log)
187            throws CommandException {
188        if (slaXml == null || slaXml.length() == 0) {
189            return null;
190        }
191        try {
192            Element eSla = XmlUtils.parseXml(slaXml);
193            Element eAppName = eSla.getChild("app-name", eSla.getNamespace());
194            //stop-gap null handling till deprecated class is removed
195            String appNameStr = eAppName != null ? eAppName.getText() : null;
196            return createSlaStatusEvent(id, stat, appType, appNameStr, log);
197        }
198        catch (Exception e) {
199            throw new CommandException(ErrorCode.E1007, " id " + id, e.getMessage(), e);
200        }
201    }
202
203    public static String getClientId() {
204        Services services = Services.get();
205        if (services == null) {
206            throw new RuntimeException("Services is not initialized");
207        }
208        String clientId = services.getConf().get(CLIENT_ID_TAG,
209                                                 "oozie-default-instance"); // TODO" remove default
210        if (clientId == null) {
211            throw new RuntimeException(
212                    "No SLA_CLIENT_ID defined in oozie-site.xml with property name "
213                            + CLIENT_ID_TAG);
214        }
215        return clientId;
216    }
217
218    private static String getTagElement(Element elem, String tagName) {
219        if (elem != null
220                && elem.getChild(tagName, elem.getNamespace("sla")) != null) {
221            return elem.getChild(tagName, elem.getNamespace("sla")).getText()
222                    .trim();
223        }
224        else {
225            return null;
226        }
227    }
228
229}