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.util.db; 019 020import java.util.Date; 021 022import org.apache.oozie.ErrorCode; 023import org.apache.oozie.SLAEventBean; 024import org.apache.oozie.client.SLAEvent.SlaAppType; 025import org.apache.oozie.client.SLAEvent.Status; 026import org.apache.oozie.command.CommandException; 027import org.apache.oozie.service.Services; 028import org.apache.oozie.util.DateUtils; 029import org.jdom.Element; 030 031@Deprecated 032public class SLADbXOperations { 033 public static final String CLIENT_ID_TAG = "oozie:sla:client-id"; 034 035 /** 036 * Create SLA registration event 037 * 038 * @param eSla SLA xml element 039 * @param slaId SLA Id 040 * @param appType SLA app type 041 * @param user user name 042 * @param groupName group name 043 * @throws Exception 044 */ 045 public static SLAEventBean createSlaRegistrationEvent(Element eSla, String slaId, 046 SlaAppType appType, String user, String groupName) 047 throws Exception { 048 if (eSla == null) { 049 return null; 050 } 051 SLAEventBean sla = new SLAEventBean(); 052 // sla.setClientId(getTagElement( eSla, "client-id")); 053 // sla.setClientId(getClientId()); 054 sla.setAppName(getTagElement(eSla, "app-name")); 055 sla.setParentClientId(getTagElement(eSla, "parent-child-id")); 056 sla.setParentSlaId(getTagElement(eSla, "parent-sla-id")); 057 String strNominalTime = getTagElement(eSla, "nominal-time"); 058 if (strNominalTime == null || strNominalTime.length() == 0) { 059 throw new CommandException(ErrorCode.E1101); 060 } 061 Date nominalTime = DateUtils.parseDateOozieTZ(strNominalTime); 062 // Setting expected start time 063 String strRelExpectedStart = getTagElement(eSla, "should-start"); 064 if (strRelExpectedStart != null && strRelExpectedStart.length() > 0) { 065 int relExpectedStart = Integer.parseInt(strRelExpectedStart); 066 if (relExpectedStart < 0) { 067 sla.setExpectedStart(null); 068 } 069 else { 070 Date expectedStart = new Date(nominalTime.getTime() 071 + relExpectedStart * 60 * 1000); 072 sla.setExpectedStart(expectedStart); 073 } 074 } else { 075 sla.setExpectedStart(null); 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 return sla; 111 112 } 113 114 /** 115 * Create SLA status event 116 * 117 * @param id SLA Id 118 * @param status SLA status 119 * @param appType SLA app type 120 * @throws Exception 121 */ 122 public static SLAEventBean createSlaStatusEvent(String id, 123 Status status, SlaAppType appType) throws Exception { 124 SLAEventBean sla = new SLAEventBean(); 125 sla.setSlaId(id); 126 sla.setJobStatus(status); 127 sla.setAppType(appType); 128 sla.setStatusTimestamp(new Date()); 129 130 return sla; 131 } 132 133 /** 134 * Create SLA status event 135 * 136 * @param slaXml SLA xml element 137 * @param id SLA Id 138 * @param stat SLA status 139 * @param appType SLA app type 140 * @throws CommandException 141 */ 142 public static SLAEventBean createStatusEvent(String slaXml, String id, Status stat, 143 SlaAppType appType) throws CommandException { 144 if (slaXml == null || slaXml.length() == 0) { 145 return null; 146 } 147 try { 148 return createSlaStatusEvent(id, stat, appType); 149 } 150 catch (Exception e) { 151 throw new CommandException(ErrorCode.E1007, " id " + id, e.getMessage(), e); 152 } 153 } 154 155 /** 156 * Return client id 157 * 158 * @return client id 159 */ 160 public static String getClientId() { 161 Services services = Services.get(); 162 if (services == null) { 163 throw new RuntimeException("Services is not initialized"); 164 } 165 String clientId = services.getConf().get(CLIENT_ID_TAG, 166 "oozie-default-instance"); // TODO remove default 167 if (clientId == null) { 168 throw new RuntimeException( 169 "No SLA_CLIENT_ID defined in oozie-site.xml with property name " 170 + CLIENT_ID_TAG); 171 } 172 return clientId; 173 } 174 175 private static String getTagElement(Element elem, String tagName) { 176 if (elem != null 177 && elem.getChild(tagName, elem.getNamespace("sla")) != null) { 178 return elem.getChild(tagName, elem.getNamespace("sla")).getText() 179 .trim(); 180 } 181 else { 182 return null; 183 } 184 } 185 186}