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