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 java.io.IOException;
022import java.io.UnsupportedEncodingException;
023import java.net.URLDecoder;
024import java.text.ParseException;
025import java.util.Arrays;
026import java.util.HashSet;
027import java.util.List;
028import java.util.Map;
029import java.util.Set;
030
031import javax.servlet.ServletException;
032import javax.servlet.http.HttpServletRequest;
033import javax.servlet.http.HttpServletResponse;
034import org.apache.oozie.ErrorCode;
035import org.apache.oozie.XException;
036import org.apache.oozie.client.OozieClient;
037import org.apache.oozie.client.rest.RestConstants;
038import org.apache.oozie.command.CommandException;
039import org.apache.oozie.executor.jpa.sla.SLASummaryGetForFilterJPAExecutor;
040import org.apache.oozie.executor.jpa.sla.SLASummaryGetForFilterJPAExecutor.SLASummaryFilter;
041import org.apache.oozie.service.JPAService;
042import org.apache.oozie.service.Services;
043import org.apache.oozie.sla.SLASummaryBean;
044import org.apache.oozie.util.DateUtils;
045import org.apache.oozie.util.XLog;
046import org.json.simple.JSONObject;
047
048@SuppressWarnings("serial")
049public class V2SLAServlet extends SLAServlet {
050
051    private static final String INSTRUMENTATION_NAME = "v2sla";
052    private static final JsonRestServlet.ResourceInfo RESOURCES_INFO[] = new JsonRestServlet.ResourceInfo[1];
053    private static final Set<String> SLA_FILTER_NAMES = new HashSet<String>();
054
055    static {
056        SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_ID);
057        SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_PARENT_ID);
058        SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_APPNAME);
059        SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_NOMINAL_START);
060        SLA_FILTER_NAMES.add(OozieClient.FILTER_SLA_NOMINAL_END);
061    }
062
063    static {
064        RESOURCES_INFO[0] = new JsonRestServlet.ResourceInfo("", Arrays.asList("GET"),
065                Arrays.asList(new JsonRestServlet.ParameterInfo(RestConstants.JOBS_FILTER_PARAM, String.class, false,
066                        Arrays.asList("GET"))));
067    }
068
069    public V2SLAServlet() {
070        super(INSTRUMENTATION_NAME, RESOURCES_INFO);
071    }
072
073    @Override
074    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
075
076        XLog.getLog(getClass()).debug("Got SLA GET request:" + request.getQueryString());
077        try {
078            stopCron();
079            JSONObject json = getSLASummaryList(request, response);
080            startCron();
081            if (json == null) {
082                response.setStatus(HttpServletResponse.SC_OK);
083            }
084            else {
085                sendJsonResponse(response, HttpServletResponse.SC_OK, json);
086            }
087        }
088        catch (CommandException ce) {
089            XLog.getLog(getClass()).error("Command exception ", ce);
090            throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ce);
091        }
092        catch (RuntimeException re) {
093            XLog.getLog(getClass()).error("Runtime error ", re);
094            throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ErrorCode.E0307, re.getMessage());
095        }
096    }
097
098    private JSONObject getSLASummaryList(HttpServletRequest request, HttpServletResponse response)
099            throws ServletException, CommandException {
100        String timeZoneId = request.getParameter(RestConstants.TIME_ZONE_PARAM) == null ? null : request
101                .getParameter(RestConstants.TIME_ZONE_PARAM);
102        String filterString = request.getParameter(RestConstants.JOBS_FILTER_PARAM);
103        String maxResults = request.getParameter(RestConstants.LEN_PARAM);
104        int numMaxResults = 1000; // Default
105
106        if (maxResults != null) {
107            numMaxResults = Integer.parseInt(maxResults);
108        }
109
110        if (filterString == null || filterString.equals("")) {
111            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0305,
112                    RestConstants.JOBS_FILTER_PARAM);
113        }
114
115        try {
116            Map<String, List<String>> filterList = parseFilter(URLDecoder.decode(filterString, "UTF-8"), SLA_FILTER_NAMES);
117            SLASummaryFilter filter = new SLASummaryFilter();
118            if (filterList.containsKey(OozieClient.FILTER_SLA_ID)) {
119                filter.setJobId(filterList.get(OozieClient.FILTER_SLA_ID).get(0));
120            }
121            if (filterList.containsKey(OozieClient.FILTER_SLA_PARENT_ID)) {
122                filter.setParentId(filterList.get(OozieClient.FILTER_SLA_PARENT_ID).get(0));
123            }
124            if (filterList.containsKey(OozieClient.FILTER_SLA_APPNAME)) {
125                filter.setAppName(filterList.get(OozieClient.FILTER_SLA_APPNAME).get(0));
126            }
127            if (filterList.containsKey(OozieClient.FILTER_SLA_NOMINAL_START)) {
128                filter.setNominalStart(DateUtils.parseDateUTC(filterList.get(OozieClient.FILTER_SLA_NOMINAL_START).get(0)));
129            }
130            if (filterList.containsKey(OozieClient.FILTER_SLA_NOMINAL_END)) {
131                filter.setNominalEnd(DateUtils.parseDateUTC(filterList.get(OozieClient.FILTER_SLA_NOMINAL_END).get(0)));
132            }
133
134            if (filter.getAppName() == null && filter.getJobId() == null && filter.getParentId() == null) {
135                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0305,
136                        "At least one of the filter parameters - " + OozieClient.FILTER_SLA_ID + ","
137                                + OozieClient.FILTER_SLA_PARENT_ID + " or " + OozieClient.FILTER_SLA_APPNAME
138                                + " should be specified in the filter query parameter");
139            }
140
141            JPAService jpaService = Services.get().get(JPAService.class);
142            List<SLASummaryBean> slaSummaryList = null;
143            if (jpaService != null) {
144                slaSummaryList = jpaService.execute(new SLASummaryGetForFilterJPAExecutor(filter, numMaxResults));
145            }
146            else {
147                XLog.getLog(getClass()).error(ErrorCode.E0610);
148            }
149            return SLASummaryBean.toJSONObject(slaSummaryList, timeZoneId);
150        }
151        catch (XException ex) {
152            throw new CommandException(ex);
153        }
154        catch (UnsupportedEncodingException e) {
155            throw new XServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ErrorCode.E0307,
156                    "Unsupported Encoding", e);
157        }
158        catch (ParseException e) {
159            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0303,
160                    filterString, e);
161        }
162
163    }
164
165}