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 */
018package org.apache.oozie.sla;
019
020import java.sql.Timestamp;
021import java.util.ArrayList;
022import java.util.Date;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Map.Entry;
027
028import javax.persistence.Basic;
029import javax.persistence.Column;
030import javax.persistence.Entity;
031import javax.persistence.Id;
032import javax.persistence.NamedQueries;
033import javax.persistence.NamedQuery;
034import javax.persistence.Table;
035import javax.persistence.Transient;
036import org.apache.oozie.AppType;
037import org.apache.oozie.client.event.Event.MessageType;
038import org.apache.oozie.client.rest.JsonBean;
039import org.apache.oozie.util.DateUtils;
040import org.apache.openjpa.persistence.jdbc.Index;
041import org.json.simple.JSONArray;
042import org.json.simple.JSONObject;
043
044@Entity
045@Table(name = "SLA_REGISTRATION")
046@NamedQueries({
047
048 @NamedQuery(name = "UPDATE_SLA_REG_ALL", query = "update SLARegistrationBean w set w.jobId = :jobId, w.nominalTimeTS "
049         + "= :nominalTime, w.expectedStartTS = :expectedStartTime, w.expectedEndTS = :expectedEndTime, w.expectedDuration "
050         + "= :expectedDuration, w.slaConfig = :slaConfig, w.notificationMsg = :notificationMsg, w.upstreamApps = :upstreamApps,"
051         + " w.appType = :appType, w.appName = :appName, w.user = :user, w.parentId = :parentId, w.jobData = :jobData "
052         + "where w.jobId = :jobId"),
053
054 @NamedQuery(name = "UPDATE_SLA_CONFIG", query = "update SLARegistrationBean w set w.slaConfig = :slaConfig where w.jobId"
055         + " = :jobId"),
056
057 @NamedQuery(name = "UPDATE_SLA_EXPECTED_VALUE", query = "update SLARegistrationBean w set w.expectedStartTS "
058         + "= :expectedStartTime, w.expectedEndTS = :expectedEndTime , w.expectedDuration = :expectedDuration  where w.jobId"
059         + " = :jobId"),
060
061 @NamedQuery(name = "GET_SLA_REG_ON_RESTART", query = "select w.notificationMsg, w.upstreamApps, w.slaConfig, w.jobData "
062         + "from SLARegistrationBean w where w.jobId = :id"),
063
064 @NamedQuery(name = "GET_SLA_REG_ALL", query = "select OBJECT(w) from SLARegistrationBean w where w.jobId = :id"),
065
066 @NamedQuery(name = "GET_SLA_CONFIGS", query = "select w.jobId, w.slaConfig from SLARegistrationBean w where w.jobId IN (:ids)"),
067
068 @NamedQuery(name = "GET_SLA_EXPECTED_VALUE_CONFIG", query = "select w.jobId, w.slaConfig, w.expectedStartTS, w.expectedEndTS,"
069         + " w.expectedDuration, w.nominalTimeTS from SLARegistrationBean w where w.jobId = :id"),
070
071 @NamedQuery(name = "GET_SLA_REG_FOR_PARENT_ID", query = "select w.jobId, w.slaConfig from SLARegistrationBean w "
072         + "where w.parentId = :parentId"),
073
074 @NamedQuery(name = "GET_SLA_REGISTRATIONS", query = "select OBJECT(w) from SLARegistrationBean w")
075 })
076
077public class SLARegistrationBean implements JsonBean {
078
079    @Id
080    @Basic
081    @Column(name = "job_id")
082    private String jobId;
083
084    @Basic
085    @Column(name = "parent_id")
086    private String parentId = null;
087
088    @Basic
089    @Column(name = "app_name")
090    private String appName = null;
091
092    @Basic
093    @Column(name = "app_type")
094    private String appType = null;
095
096    @Basic
097    @Column(name = "created_time")
098    private Timestamp createdTimeTS = null;
099
100    @Basic
101    @Index
102    @Column(name = "nominal_time")
103    private Timestamp nominalTimeTS = null;
104
105    @Basic
106    @Column(name = "expected_start")
107    private Timestamp expectedStartTS = null;
108
109    @Basic
110    @Column(name = "expected_end")
111    private Timestamp expectedEndTS = null;
112
113    @Basic
114    @Column(name = "expected_duration")
115    private long expectedDuration = -1;
116
117    @Basic
118    @Column(name = "user_name")
119    private String user = null;
120
121    @Basic
122    @Column(name = "upstream_apps")
123    private String upstreamApps = null;
124
125    @Basic
126    @Column(name = "job_data")
127    private String jobData = null;
128
129    @Basic
130    @Column(name = "sla_config")
131    private String slaConfig = null;
132
133    @Basic
134    @Column(name = "notification_msg")
135    private String notificationMsg = null;
136
137    @Transient
138    private Map<String, String> slaConfigMap;
139
140    @Transient
141    private MessageType msgType;
142
143    private final String ALERT_EVENTS = "alert_events";
144    private final String ALERT_CONTACT = "alert_contact";
145
146    public SLARegistrationBean() {
147        slaConfigMap = new HashMap<String, String>();
148        msgType = MessageType.SLA;
149    }
150
151    public SLARegistrationBean(JSONObject obj) {
152        // TODO use JSONObject
153        this();
154    }
155
156    public String getId() {
157        return jobId;
158    }
159
160    public void setId(String jobId) {
161        this.jobId = jobId;
162    }
163
164    public String getParentId() {
165        return parentId;
166    }
167
168    public void setParentId(String parentId) {
169        this.parentId = parentId;
170    }
171
172    public String getAppName() {
173        return appName;
174    }
175
176    public void setAppName(String appName) {
177        this.appName = appName;
178    }
179
180    public AppType getAppType() {
181        return AppType.valueOf(appType);
182    }
183
184    public void setAppType(AppType appType) {
185        this.appType = appType.toString();
186    }
187
188    public Timestamp getCreatedTimestamp() {
189        return createdTimeTS;
190    }
191
192    public void setCreatedTimestamp(Timestamp createdTime) {
193        this.createdTimeTS = createdTime;
194    }
195
196    public Date getCreatedTime() {
197        return DateUtils.toDate(createdTimeTS);
198    }
199
200    public void setCreatedTime(Date createdTime) {
201        this.createdTimeTS = DateUtils.convertDateToTimestamp(createdTime);
202    }
203
204    public Date getNominalTime() {
205        return DateUtils.toDate(nominalTimeTS);
206    }
207
208    public Timestamp getNominalTimestamp() {
209        return this.nominalTimeTS;
210    }
211
212    public void setNominalTime(Date nominalTime) {
213        this.nominalTimeTS = DateUtils.convertDateToTimestamp(nominalTime);
214    }
215
216    public Date getExpectedStart() {
217        return DateUtils.toDate(expectedStartTS);
218    }
219
220    public Timestamp getExpectedStartTimestamp() {
221        return this.expectedStartTS;
222    }
223
224    public void setExpectedStart(Date expectedStart) {
225        this.expectedStartTS = DateUtils.convertDateToTimestamp(expectedStart);
226    }
227
228    public Date getExpectedEnd() {
229        return DateUtils.toDate(expectedEndTS);
230    }
231
232    public Timestamp getExpectedEndTimestamp() {
233        return this.expectedEndTS;
234    }
235
236    public void setExpectedEnd(Date expectedEnd) {
237        this.expectedEndTS = DateUtils.convertDateToTimestamp(expectedEnd);
238    }
239
240    public long getExpectedDuration() {
241        return expectedDuration;
242    }
243
244    public void setExpectedDuration(long expectedDuration) {
245        this.expectedDuration = expectedDuration;
246    }
247
248    public String getUser() {
249        return user;
250    }
251
252    public void setUser(String user) {
253        this.user = user;
254    }
255
256    public String getUpstreamApps() {
257        return upstreamApps;
258    }
259
260    public void setUpstreamApps(String upstreamApps) {
261        this.upstreamApps = upstreamApps;
262    }
263
264    public String getJobData() {
265        return jobData;
266    }
267
268    public void setJobData(String jobData) {
269        this.jobData = jobData;
270    }
271
272    public String getSlaConfig() {
273        return slaConfig;
274    }
275
276    public void setSlaConfig(String configStr) {
277        this.slaConfig = configStr;
278        slaConfigStringToMap();
279    }
280
281    public String getNotificationMsg() {
282        return notificationMsg;
283    }
284
285    public void setNotificationMsg(String notificationMsg) {
286        this.notificationMsg = notificationMsg;
287    }
288
289    public String getAlertEvents() {
290        return slaConfigMap.get(ALERT_EVENTS);
291    }
292
293    public void setAlertEvents(String alertEvents) {
294        slaConfigMap.put(ALERT_EVENTS, alertEvents);
295        slaConfig = slaConfigMapToString();
296    }
297
298    public String getAlertContact() {
299        return slaConfigMap.get(ALERT_CONTACT);
300    }
301
302    public void setAlertContact(String alertContact) {
303        slaConfigMap.put(ALERT_CONTACT, alertContact);
304        slaConfig = slaConfigMapToString();
305    }
306
307
308    public Map<String, String> getSLAConfigMap() {
309        return slaConfigMap;
310    }
311
312    public void addToSLAConfigMap(String key, String value) {
313        slaConfigMap.put(key, value);
314        slaConfig = slaConfigMapToString();
315    }
316
317    public void removeFromSLAConfigMap(String key) {
318        slaConfigMap.remove(key);
319        slaConfig = slaConfigMapToString();
320    }
321
322    private void slaConfigStringToMap() {
323        if (slaConfig != null) {
324            String[] splitString = slaConfig.split("},");
325            for (String config : splitString) {
326                String[] pair = config.split("=");
327                if (pair.length == 2) {
328                    slaConfigMap.put(pair[0].substring(1), pair[1]);
329                }
330            }
331        }
332    }
333
334    public String slaConfigMapToString() {
335        StringBuilder sb = new StringBuilder();
336        for (Entry<String, String> e : slaConfigMap.entrySet()) {
337            sb.append("{" + e.getKey() + "=" + e.getValue() + "},");
338        }
339        return sb.toString();
340    }
341
342    @Override
343    public JSONObject toJSONObject() {
344        // TODO
345        return null;
346    }
347
348    @Override
349    public JSONObject toJSONObject(String timeZoneId) {
350        // TODO
351        return null;
352    }
353
354    /**
355     * Convert a SLARegistrationBean list into a JSONArray.
356     *
357     * @param events SLARegistrationBean list.
358     * @param timeZoneId time zone to use for dates in the JSON array.
359     * @return the corresponding JSON array.
360     */
361    @SuppressWarnings("unchecked")
362    public static JSONArray toJSONArray(List<? extends SLARegistrationBean> events, String timeZoneId) {
363        JSONArray array = new JSONArray();
364        if (events != null) {
365            for (SLARegistrationBean node : events) {
366                array.add(node.toJSONObject(timeZoneId));
367            }
368        }
369        return array;
370    }
371
372    /**
373     * Convert a JSONArray into a SLARegistrationBean list.
374     *
375     * @param array JSON array.
376     * @return the corresponding SLA SLARegistrationBean list.
377     */
378    public static List<SLARegistrationBean> fromJSONArray(JSONArray array) {
379        List<SLARegistrationBean> list = new ArrayList<SLARegistrationBean>();
380        for (Object obj : array) {
381            list.add(new SLARegistrationBean((JSONObject) obj));
382        }
383        return list;
384    }
385
386    public MessageType getMsgType(){
387        return this.msgType;
388    }
389
390    public void setMsgType(MessageType msgType){
391        this.msgType = msgType;
392    }
393}