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