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.executor.jpa.SLAEventInsertJPAExecutor; 028 import org.apache.oozie.service.JPAService; 029 import org.apache.oozie.service.Services; 030 import org.apache.oozie.util.DateUtils; 031 import org.jdom.Element; 032 033 public class SLADbXOperations { 034 public static final String CLIENT_ID_TAG = "oozie:sla:client-id"; 035 036 /** 037 * Create SLA registration event 038 * 039 * @param eSla SLA xml element 040 * @param slaId SLA Id 041 * @param appType SLA app type 042 * @param user user name 043 * @param groupName group name 044 * @throws Exception 045 */ 046 public static void writeSlaRegistrationEvent(Element eSla, String slaId, 047 SlaAppType appType, String user, String groupName) 048 throws Exception { 049 if (eSla == null) { 050 return; 051 } 052 SLAEventBean sla = new SLAEventBean(); 053 // sla.setClientId(getTagElement( eSla, "client-id")); 054 // sla.setClientId(getClientId()); 055 sla.setAppName(getTagElement(eSla, "app-name")); 056 sla.setParentClientId(getTagElement(eSla, "parent-child-id")); 057 sla.setParentSlaId(getTagElement(eSla, "parent-sla-id")); 058 String strNominalTime = getTagElement(eSla, "nominal-time"); 059 if (strNominalTime == null || strNominalTime.length() == 0) { 060 throw new CommandException(ErrorCode.E1101); 061 } 062 Date nominalTime = DateUtils.parseDateUTC(strNominalTime); 063 // Setting expected start time 064 String strRelExpectedStart = getTagElement(eSla, "should-start"); 065 if (strRelExpectedStart == null || strRelExpectedStart.length() == 0) { 066 throw new CommandException(ErrorCode.E1102); 067 } 068 int relExpectedStart = Integer.parseInt(strRelExpectedStart); 069 if (relExpectedStart < 0) { 070 sla.setExpectedStart(null); 071 } 072 else { 073 Date expectedStart = new Date(nominalTime.getTime() 074 + relExpectedStart * 60 * 1000); 075 sla.setExpectedStart(expectedStart); 076 } 077 078 // Setting expected end time 079 String strRelExpectedEnd = getTagElement(eSla, "should-end"); 080 if (strRelExpectedEnd == null || strRelExpectedEnd.length() == 0) { 081 throw new RuntimeException("should-end can't be empty"); 082 } 083 int relExpectedEnd = Integer.parseInt(strRelExpectedEnd); 084 if (relExpectedEnd < 0) { 085 sla.setExpectedEnd(null); 086 } 087 else { 088 Date expectedEnd = new Date(nominalTime.getTime() + relExpectedEnd * 60 * 1000); 089 sla.setExpectedEnd(expectedEnd); 090 } 091 092 sla.setNotificationMsg(getTagElement(eSla, "notification-msg")); 093 sla.setAlertContact(getTagElement(eSla, "alert-contact")); 094 sla.setDevContact(getTagElement(eSla, "dev-contact")); 095 sla.setQaContact(getTagElement(eSla, "qa-contact")); 096 sla.setSeContact(getTagElement(eSla, "se-contact")); 097 sla.setAlertFrequency(getTagElement(eSla, "alert-frequency")); 098 sla.setAlertPercentage(getTagElement(eSla, "alert-percentage")); 099 100 sla.setUpstreamApps(getTagElement(eSla, "upstream-apps")); 101 102 // Oozie defined 103 sla.setSlaId(slaId); 104 sla.setAppType(appType); 105 sla.setUser(user); 106 sla.setGroupName(groupName); 107 sla.setJobStatus(Status.CREATED); 108 sla.setStatusTimestamp(new Date()); 109 110 JPAService jpaService = Services.get().get(JPAService.class); 111 112 if (jpaService != null) { 113 jpaService.execute(new SLAEventInsertJPAExecutor(sla)); 114 } 115 else { 116 throw new CommandException(ErrorCode.E0610, "unable to write sla event."); 117 } 118 119 } 120 121 /** 122 * Create SLA status event 123 * 124 * @param id SLA Id 125 * @param status SLA status 126 * @param appType SLA app type 127 * @throws Exception 128 */ 129 public static void writeSlaStatusEvent(String id, 130 Status status, SlaAppType appType) throws Exception { 131 SLAEventBean sla = new SLAEventBean(); 132 sla.setSlaId(id); 133 sla.setJobStatus(status); 134 sla.setAppType(appType); 135 sla.setStatusTimestamp(new Date()); 136 137 JPAService jpaService = Services.get().get(JPAService.class); 138 139 if (jpaService != null) { 140 jpaService.execute(new SLAEventInsertJPAExecutor(sla)); 141 } 142 else { 143 throw new CommandException(ErrorCode.E0610, "unable to write sla event."); 144 } 145 } 146 147 /** 148 * Create SLA status event 149 * 150 * @param slaXml SLA xml element 151 * @param id SLA Id 152 * @param stat SLA status 153 * @param appType SLA app type 154 * @throws CommandException 155 */ 156 public static void writeStausEvent(String slaXml, String id, Status stat, 157 SlaAppType appType) throws CommandException { 158 if (slaXml == null || slaXml.length() == 0) { 159 return; 160 } 161 try { 162 writeSlaStatusEvent(id, stat, appType); 163 } 164 catch (Exception e) { 165 throw new CommandException(ErrorCode.E1007, " id " + id, e); 166 } 167 } 168 169 /** 170 * Return client id 171 * 172 * @return client id 173 */ 174 public static String getClientId() { 175 Services services = Services.get(); 176 if (services == null) { 177 throw new RuntimeException("Services is not initialized"); 178 } 179 String clientId = services.getConf().get(CLIENT_ID_TAG, 180 "oozie-default-instance"); // TODO remove default 181 if (clientId == null) { 182 throw new RuntimeException( 183 "No SLA_CLIENT_ID defined in oozie-site.xml with property name " 184 + CLIENT_ID_TAG); 185 } 186 return clientId; 187 } 188 189 private static String getTagElement(Element elem, String tagName) { 190 if (elem != null 191 && elem.getChild(tagName, elem.getNamespace("sla")) != null) { 192 return elem.getChild(tagName, elem.getNamespace("sla")).getText() 193 .trim(); 194 } 195 else { 196 return null; 197 } 198 } 199 200 }