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