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