001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.util.db;
016    
017    import java.sql.Timestamp;
018    import java.util.Date;
019    
020    import org.apache.oozie.ErrorCode;
021    import org.apache.oozie.SLAEventBean;
022    import org.apache.oozie.client.SLAEvent.SlaAppType;
023    import org.apache.oozie.client.SLAEvent.Status;
024    import org.apache.oozie.command.CommandException;
025    import org.apache.oozie.service.Services;
026    import org.apache.oozie.service.StoreService;
027    import org.apache.oozie.store.CoordinatorStore;
028    import org.apache.oozie.store.SLAStore;
029    import org.apache.oozie.store.Store;
030    import org.apache.oozie.util.DateUtils;
031    import org.apache.oozie.util.XmlUtils;
032    import org.jdom.Element;
033    import org.jdom.JDOMException;
034    
035    public class SLADbOperations {
036        public static final String CLIENT_ID_TAG = "oozie:sla:client-id";
037    
038        public static void writeSlaRegistrationEvent(Element eSla, Store store,
039                                                     String slaId, SlaAppType appType, String user, String groupName)
040                throws Exception {
041            // System.out.println("BBBBB SLA added");
042            if (eSla == null) {
043                return;
044            }
045            //System.out.println("Writing REG AAAAA " + slaId);
046            SLAEventBean sla = new SLAEventBean();
047            // sla.setClientId(getTagElement( eSla, "client-id"));
048            // sla.setClientId(getClientId());
049            sla.setAppName(getTagElement(eSla, "app-name"));
050            sla.setParentClientId(getTagElement(eSla, "parent-child-id"));
051            sla.setParentSlaId(getTagElement(eSla, "parent-sla-id"));
052            String strNominalTime = getTagElement(eSla, "nominal-time");
053            // System.out.println("AAAAA SLA nominal time "+ strNominalTime);
054            if (strNominalTime == null || strNominalTime.length() == 0) {
055                throw new RuntimeException("Nominal time is required"); // TODO:
056                // change to
057                // CommandException
058            }
059            Date nominalTime = DateUtils.parseDateUTC(strNominalTime);
060            // Setting expected start time
061            String strRelExpectedStart = getTagElement(eSla, "should-start");
062            if (strRelExpectedStart == null || strRelExpectedStart.length() == 0) {
063                throw new RuntimeException("should-start can't be empty");
064            }
065            int relExpectedStart = Integer.parseInt(strRelExpectedStart);
066            if (relExpectedStart < 0) {
067                sla.setExpectedStart(null);
068            }
069            else {
070                Date expectedStart = new Date(nominalTime.getTime()
071                        + relExpectedStart * 60 * 1000);
072                sla.setExpectedStart(expectedStart);
073                // sla.setExpectedStart(nominalTime);
074            }
075    
076            // Setting expected end time
077            String strRelExpectedEnd = getTagElement(eSla, "should-end");
078            if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) {
079                throw new RuntimeException("should-end can't be empty");
080            }
081            int relExpectedEnd = Integer.parseInt(strRelExpectedEnd);
082            if (relExpectedEnd < 0) {
083                sla.setExpectedEnd(null);
084            }
085            else {
086                Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd
087                        * 60 * 1000);
088                sla.setExpectedEnd(expectedEnd);
089            }
090    
091            sla.setNotificationMsg(getTagElement(eSla, "notification-msg"));
092            sla.setAlertContact(getTagElement(eSla, "alert-contact"));
093            sla.setDevContact(getTagElement(eSla, "dev-contact"));
094            sla.setQaContact(getTagElement(eSla, "qa-contact"));
095            sla.setSeContact(getTagElement(eSla, "se-contact"));
096            sla.setAlertFrequency(getTagElement(eSla, "alert-frequency"));
097            sla.setAlertPercentage(getTagElement(eSla, "alert-percentage"));
098    
099            sla.setUpstreamApps(getTagElement(eSla, "upstream-apps"));
100    
101            // Oozie defined
102    
103            sla.setSlaId(slaId);
104            sla.setAppType(appType);
105            sla.setUser(user);
106            sla.setGroupName(groupName);
107            sla.setJobStatus(Status.CREATED);
108            sla.setStatusTimestamp(new Date());
109    
110            SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class)
111                    .getStore(SLAStore.class, store);
112            slaStore.insertSLAEvent(sla);
113        }
114    
115        public static void writeSlaStatusEvent(String id,
116                                               Status status, Store store, SlaAppType appType) throws Exception {
117            SLAEventBean sla = new SLAEventBean();
118            sla.setSlaId(id);
119            sla.setJobStatus(status);
120            sla.setAppType(appType);
121            sla.setStatusTimestamp(new Date());
122            //System.out.println("Writing STATUS AAAAA " + id);
123            SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class)
124                    .getStore(SLAStore.class, store);
125            slaStore.insertSLAEvent(sla);
126        }
127    
128        public static void writeStausEvent(String slaXml, String id, Store store,
129                                           Status stat, SlaAppType appType) throws CommandException {
130            if (slaXml == null || slaXml.length() == 0) {
131                return;
132            }
133            try {
134                writeSlaStatusEvent(id, stat, store, appType);
135            }
136            catch (Exception e) {
137                throw new CommandException(ErrorCode.E1007, " id " + id, e);
138            }
139        }
140    
141        public static String getClientId() {
142            Services services = Services.get();
143            if (services == null) {
144                throw new RuntimeException("Services is not initialized");
145            }
146            String clientId = services.getConf().get(CLIENT_ID_TAG,
147                                                     "oozie-default-instance"); // TODO" remove default
148            if (clientId == null) {
149                //System.out.println("CONF "
150                //       + XmlUtils.prettyPrint(services.getConf()));
151                throw new RuntimeException(
152                        "No SLA_CLIENT_ID defined in oozie-site.xml with property name "
153                                + CLIENT_ID_TAG);
154            }
155            return clientId;
156        }
157    
158        private static String getTagElement(Element elem, String tagName) {
159            if (elem != null
160                    && elem.getChild(tagName, elem.getNamespace("sla")) != null) {
161                return elem.getChild(tagName, elem.getNamespace("sla")).getText()
162                        .trim();
163            }
164            else {
165                return null;
166            }
167        }
168    
169    }