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