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