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.servlet; 020 021import com.google.common.annotations.VisibleForTesting; 022import com.google.common.base.Strings; 023import com.google.common.collect.ListMultimap; 024import org.apache.oozie.ErrorCode; 025import org.apache.oozie.FilterParser; 026import org.apache.oozie.XException; 027import org.apache.oozie.client.rest.RestConstants; 028import org.apache.oozie.command.CommandException; 029import org.apache.oozie.executor.jpa.JPAExecutorException; 030import org.apache.oozie.executor.jpa.SLARegistrationQueryExecutor; 031import org.apache.oozie.executor.jpa.SLARegistrationQueryExecutor.SLARegQuery; 032import org.apache.oozie.executor.jpa.sla.SLASummaryGetForFilterJPAExecutor; 033import org.apache.oozie.service.JPAService; 034import org.apache.oozie.service.Services; 035import org.apache.oozie.sla.SLARegistrationBean; 036import org.apache.oozie.sla.SLASummaryBean; 037import org.apache.oozie.util.XLog; 038import org.json.simple.JSONObject; 039 040import javax.servlet.ServletException; 041import javax.servlet.http.HttpServletRequest; 042import javax.servlet.http.HttpServletResponse; 043import java.io.IOException; 044import java.text.ParseException; 045import java.util.ArrayList; 046import java.util.Arrays; 047import java.util.HashMap; 048import java.util.List; 049import java.util.Map; 050 051@SuppressWarnings("serial") 052public class V2SLAServlet extends SLAServlet { 053 054 private static final String INSTRUMENTATION_NAME = "v2sla"; 055 private static final JsonRestServlet.ResourceInfo RESOURCES_INFO[] = new JsonRestServlet.ResourceInfo[1]; 056 057 static { 058 RESOURCES_INFO[0] = new JsonRestServlet.ResourceInfo("", Arrays.asList("GET"), 059 Arrays.asList(new JsonRestServlet.ParameterInfo(RestConstants.JOBS_FILTER_PARAM, String.class, false, 060 Arrays.asList("GET")), new JsonRestServlet.ParameterInfo(RestConstants.ORDER_PARAM, String.class, false, 061 Arrays.asList("GET")), new JsonRestServlet.ParameterInfo(RestConstants.SORTBY_PARAM, String.class, 062 false, Arrays.asList("GET")) 063 )); 064 } 065 066 public V2SLAServlet() { 067 super(INSTRUMENTATION_NAME, RESOURCES_INFO); 068 } 069 070 @Override 071 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 072 073 XLog.getLog(getClass()).debug("Got SLA GET request: {0}", request.getQueryString()); 074 try { 075 stopCron(); 076 JSONObject json = getSLASummaryList(request); 077 startCron(); 078 sendJsonResponse(response, HttpServletResponse.SC_OK, json); 079 } 080 catch (CommandException ce) { 081 XLog.getLog(getClass()).error("Command exception ", ce); 082 throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ce); 083 } 084 catch (RuntimeException re) { 085 XLog.getLog(getClass()).error("Runtime error ", re); 086 throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ErrorCode.E0307, re.getMessage()); 087 } 088 } 089 090 private JSONObject getSLASummaryList(final HttpServletRequest request) throws ServletException, CommandException { 091 String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM); 092 String filterString = request.getParameter(RestConstants.JOBS_FILTER_PARAM); 093 String orderString = request.getParameter(RestConstants.ORDER_PARAM); 094 String sortbyString = request.getParameter(RestConstants.SORTBY_PARAM); 095 String maxResults = request.getParameter(RestConstants.LEN_PARAM); 096 int numMaxResults = 1000; // Default 097 boolean isDescendingOrder = false; // Default 098 099 if (maxResults != null) { 100 numMaxResults = Integer.parseInt(maxResults); 101 } 102 103 if (Strings.isNullOrEmpty(filterString)) { 104 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0305, 105 RestConstants.JOBS_FILTER_PARAM); 106 } 107 108 if (!Strings.isNullOrEmpty(orderString)) { 109 isDescendingOrder = getOrder(orderString); 110 } 111 112 try { 113 ListMultimap<String, String> filterParams = FilterParser.parseFilter(filterString); 114 return getSLASummaryListByFilterParams(timeZoneId, numMaxResults, filterParams, sortbyString, isDescendingOrder); 115 } 116 catch (XException ex) { 117 throw new CommandException(ex); 118 } 119 catch (ParseException | IllegalArgumentException e) { 120 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, 121 filterString, e); 122 } 123 } 124 125 private boolean getOrder(String orderString) throws XServletException { 126 switch (orderString) { 127 case "desc": 128 return true; 129 case "asc": 130 return false; 131 default: 132 throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303, "order", orderString); 133 } 134 } 135 136 @VisibleForTesting 137 JSONObject getSLASummaryListByFilterParams(String timeZoneId, int numMaxResults, ListMultimap<String, String> filterList, 138 String sortbyColumn, boolean isDescendingOrder) throws 139 ServletException, ParseException, IllegalArgumentException, JPAExecutorException { 140 SLASummaryGetForFilterJPAExecutor slaSummaryGetForFilterJPAExecutor = 141 createSlaSummaryGetForFilterJPAExecutor(numMaxResults, filterList, sortbyColumn, isDescendingOrder); 142 List<SLASummaryBean> slaSummaryList = filterForSlaSummaryBeans(slaSummaryGetForFilterJPAExecutor); 143 144 List<String> jobIds = new ArrayList<>(); 145 if (slaSummaryList != null) { 146 for (SLASummaryBean summaryBean : slaSummaryList) { 147 jobIds.add(summaryBean.getId()); 148 } 149 } 150 List<SLARegistrationBean> SLARegistrationList = SLARegistrationQueryExecutor.getInstance().getList( 151 SLARegQuery.GET_SLA_CONFIGS, jobIds); 152 153 Map<String, Map<String, String>> jobIdSLAConfigMap = new HashMap<>(); 154 for(SLARegistrationBean registrationBean : SLARegistrationList){ 155 jobIdSLAConfigMap.put(registrationBean.getId(), registrationBean.getSLAConfigMap()); 156 } 157 return SLASummaryBean.toJSONObject(slaSummaryList, jobIdSLAConfigMap, timeZoneId); 158 } 159 160 private List<SLASummaryBean> filterForSlaSummaryBeans(SLASummaryGetForFilterJPAExecutor slaSummaryGetForFilterJPAExecutor) 161 throws JPAExecutorException, IllegalArgumentException { 162 JPAService jpaService = Services.get().get(JPAService.class); 163 List<SLASummaryBean> slaSummaryList = null; 164 if (jpaService != null) { 165 slaSummaryList = jpaService.execute(slaSummaryGetForFilterJPAExecutor); 166 } 167 else { 168 XLog.getLog(getClass()).error(ErrorCode.E0610); 169 } 170 return slaSummaryList; 171 } 172 173 private SLASummaryGetForFilterJPAExecutor createSlaSummaryGetForFilterJPAExecutor(int numMaxResults, 174 ListMultimap<String, String> filterList, 175 String sortbyColumn, 176 boolean isDescendingOrder) 177 throws ServletException, ParseException { 178 SLASummaryGetForFilterJPAExecutor slaSummaryGetForFilterJPAExecutor = 179 new SLASummaryGetForFilterJPAExecutor(numMaxResults); 180 slaSummaryGetForFilterJPAExecutor.setSortbyColumn(sortbyColumn); 181 slaSummaryGetForFilterJPAExecutor.setDescendingOrder(isDescendingOrder); 182 183 for(String filterName : filterList.keySet()) { 184 String filterValue = filterList.get(filterName).get(0); 185 slaSummaryGetForFilterJPAExecutor.checkAndSetFilterField(filterName, filterValue); 186 } 187 return slaSummaryGetForFilterJPAExecutor; 188 } 189}