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