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.store.Store; 029 import org.apache.oozie.util.DateUtils; 030 import org.apache.oozie.util.XLog; 031 import org.jdom.Element; 032 033 public class SLADbOperations { 034 public static final String CLIENT_ID_TAG = "oozie:sla:client-id"; 035 036 public static SLAEventBean createSlaRegistrationEvent(Element eSla, Store store, String slaId, SlaAppType appType, String user, 037 String groupName) throws Exception { 038 // System.out.println("BBBBB SLA added"); 039 if (eSla == null) { 040 return null; 041 } 042 // System.out.println("Writing REG AAAAA " + slaId); 043 SLAEventBean sla = new SLAEventBean(); 044 // sla.setClientId(getTagElement( eSla, "client-id")); 045 // sla.setClientId(getClientId()); 046 sla.setAppName(getTagElement(eSla, "app-name")); 047 sla.setParentClientId(getTagElement(eSla, "parent-child-id")); 048 sla.setParentSlaId(getTagElement(eSla, "parent-sla-id")); 049 String strNominalTime = getTagElement(eSla, "nominal-time"); 050 // System.out.println("AAAAA SLA nominal time "+ strNominalTime); 051 if (strNominalTime == null || strNominalTime.length() == 0) { 052 throw new RuntimeException("Nominal time is required"); // TODO: 053 // change to 054 // CommandException 055 } 056 Date nominalTime = DateUtils.parseDateOozieTZ(strNominalTime); 057 // Setting expected start time 058 String strRelExpectedStart = getTagElement(eSla, "should-start"); 059 if (strRelExpectedStart == null || strRelExpectedStart.length() == 0) { 060 throw new RuntimeException("should-start can't be empty"); 061 } 062 int relExpectedStart = Integer.parseInt(strRelExpectedStart); 063 if (relExpectedStart < 0) { 064 sla.setExpectedStart(null); 065 } 066 else { 067 Date expectedStart = new Date(nominalTime.getTime() + relExpectedStart * 60 * 1000); 068 sla.setExpectedStart(expectedStart); 069 // sla.setExpectedStart(nominalTime); 070 } 071 072 // Setting expected end time 073 String strRelExpectedEnd = getTagElement(eSla, "should-end"); 074 if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) { 075 throw new RuntimeException("should-end can't be empty"); 076 } 077 int relExpectedEnd = Integer.parseInt(strRelExpectedEnd); 078 if (relExpectedEnd < 0) { 079 sla.setExpectedEnd(null); 080 } 081 else { 082 Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd * 60 * 1000); 083 sla.setExpectedEnd(expectedEnd); 084 } 085 086 sla.setNotificationMsg(getTagElement(eSla, "notification-msg")); 087 sla.setAlertContact(getTagElement(eSla, "alert-contact")); 088 sla.setDevContact(getTagElement(eSla, "dev-contact")); 089 sla.setQaContact(getTagElement(eSla, "qa-contact")); 090 sla.setSeContact(getTagElement(eSla, "se-contact")); 091 sla.setAlertFrequency(getTagElement(eSla, "alert-frequency")); 092 sla.setAlertPercentage(getTagElement(eSla, "alert-percentage")); 093 094 sla.setUpstreamApps(getTagElement(eSla, "upstream-apps")); 095 096 // Oozie defined 097 098 sla.setSlaId(slaId); 099 sla.setAppType(appType); 100 sla.setUser(user); 101 sla.setGroupName(groupName); 102 sla.setJobStatus(Status.CREATED); 103 sla.setStatusTimestamp(new Date()); 104 105 return sla; 106 } 107 108 public static SLAEventBean createSlaRegistrationEvent(Element eSla, 109 String slaId, SlaAppType appType, String user, String groupName, XLog log) 110 throws Exception { 111 // System.out.println("BBBBB SLA added"); 112 if (eSla == null) { 113 return null; 114 } 115 //System.out.println("Writing REG AAAAA " + slaId); 116 SLAEventBean sla = new SLAEventBean(); 117 // sla.setClientId(getTagElement( eSla, "client-id")); 118 // sla.setClientId(getClientId()); 119 sla.setAppName(getTagElement(eSla, "app-name")); 120 sla.setParentClientId(getTagElement(eSla, "parent-child-id")); 121 sla.setParentSlaId(getTagElement(eSla, "parent-sla-id")); 122 String strNominalTime = getTagElement(eSla, "nominal-time"); 123 // System.out.println("AAAAA SLA nominal time "+ strNominalTime); 124 if (strNominalTime == null || strNominalTime.length() == 0) { 125 throw new RuntimeException("Nominal time is required"); // TODO: 126 // change to 127 // CommandException 128 } 129 Date nominalTime = DateUtils.parseDateOozieTZ(strNominalTime); 130 // Setting expected start time 131 String strRelExpectedStart = getTagElement(eSla, "should-start"); 132 if (strRelExpectedStart == null || strRelExpectedStart.length() == 0) { 133 throw new RuntimeException("should-start can't be empty"); 134 } 135 int relExpectedStart = Integer.parseInt(strRelExpectedStart); 136 if (relExpectedStart < 0) { 137 sla.setExpectedStart(null); 138 } 139 else { 140 Date expectedStart = new Date(nominalTime.getTime() 141 + relExpectedStart * 60 * 1000); 142 sla.setExpectedStart(expectedStart); 143 // sla.setExpectedStart(nominalTime); 144 } 145 146 // Setting expected end time 147 String strRelExpectedEnd = getTagElement(eSla, "should-end"); 148 if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) { 149 throw new RuntimeException("should-end can't be empty"); 150 } 151 int relExpectedEnd = Integer.parseInt(strRelExpectedEnd); 152 if (relExpectedEnd < 0) { 153 sla.setExpectedEnd(null); 154 } 155 else { 156 Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd 157 * 60 * 1000); 158 sla.setExpectedEnd(expectedEnd); 159 } 160 161 sla.setNotificationMsg(getTagElement(eSla, "notification-msg")); 162 sla.setAlertContact(getTagElement(eSla, "alert-contact")); 163 sla.setDevContact(getTagElement(eSla, "dev-contact")); 164 sla.setQaContact(getTagElement(eSla, "qa-contact")); 165 sla.setSeContact(getTagElement(eSla, "se-contact")); 166 sla.setAlertFrequency(getTagElement(eSla, "alert-frequency")); 167 sla.setAlertPercentage(getTagElement(eSla, "alert-percentage")); 168 169 sla.setUpstreamApps(getTagElement(eSla, "upstream-apps")); 170 171 // Oozie defined 172 173 sla.setSlaId(slaId); 174 sla.setAppType(appType); 175 sla.setUser(user); 176 sla.setGroupName(groupName); 177 sla.setJobStatus(Status.CREATED); 178 sla.setStatusTimestamp(new Date()); 179 180 //SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class) 181 // .getStore(SLAStore.class, store); 182 //slaStore.insertSLAEvent(sla); 183 184 return sla; 185 } 186 187 public static SLAEventBean createSlaStatusEvent(String id, 188 Status status, Store store, SlaAppType appType) throws Exception { 189 SLAEventBean sla = new SLAEventBean(); 190 sla.setSlaId(id); 191 sla.setJobStatus(status); 192 sla.setAppType(appType); 193 sla.setStatusTimestamp(new Date()); 194 //System.out.println("Writing STATUS AAAAA " + id); 195 //SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class) 196 //.getStore(SLAStore.class, store); 197 //slaStore.insertSLAEvent(sla); 198 return sla; 199 } 200 201 public static SLAEventBean createSlaStatusEvent(String id, Status status, SlaAppType appType, XLog log) throws Exception { 202 SLAEventBean sla = new SLAEventBean(); 203 sla.setSlaId(id); 204 sla.setJobStatus(status); 205 sla.setAppType(appType); 206 sla.setStatusTimestamp(new Date()); 207 // System.out.println("Writing STATUS AAAAA " + id); 208 //SLAStore slaStore = (SLAStore) Services.get().get(StoreService.class).getStore(SLAStore.class, store); 209 //slaStore.insertSLAEvent(sla); 210 211 return sla; 212 } 213 214 public static SLAEventBean createStatusEvent(String slaXml, String id, Store store, 215 Status stat, SlaAppType appType) throws CommandException { 216 if (slaXml == null || slaXml.length() == 0) { 217 return null; 218 } 219 try { 220 return createSlaStatusEvent(id, stat, store, appType); 221 } 222 catch (Exception e) { 223 throw new CommandException(ErrorCode.E1007, " id " + id, e.getMessage(), e); 224 } 225 } 226 227 public static SLAEventBean createStatusEvent(String slaXml, String id, Status stat, SlaAppType appType, XLog log) 228 throws CommandException { 229 if (slaXml == null || slaXml.length() == 0) { 230 return null; 231 } 232 try { 233 return createSlaStatusEvent(id, stat, appType, log); 234 } 235 catch (Exception e) { 236 throw new CommandException(ErrorCode.E1007, " id " + id, e.getMessage(), e); 237 } 238 } 239 240 public static String getClientId() { 241 Services services = Services.get(); 242 if (services == null) { 243 throw new RuntimeException("Services is not initialized"); 244 } 245 String clientId = services.getConf().get(CLIENT_ID_TAG, 246 "oozie-default-instance"); // TODO" remove default 247 if (clientId == null) { 248 //System.out.println("CONF " 249 // + XmlUtils.prettyPrint(services.getConf())); 250 throw new RuntimeException( 251 "No SLA_CLIENT_ID defined in oozie-site.xml with property name " 252 + CLIENT_ID_TAG); 253 } 254 return clientId; 255 } 256 257 private static String getTagElement(Element elem, String tagName) { 258 if (elem != null 259 && elem.getChild(tagName, elem.getNamespace("sla")) != null) { 260 return elem.getChild(tagName, elem.getNamespace("sla")).getText() 261 .trim(); 262 } 263 else { 264 return null; 265 } 266 } 267 268 }