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