Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.warden; |
| 18 | |
| 19 | import com.google.common.io.ByteStreams; |
| 20 | import org.eclipse.jetty.server.Response; |
| 21 | |
| 22 | import javax.servlet.ServletException; |
| 23 | import javax.servlet.http.HttpServlet; |
| 24 | import javax.servlet.http.HttpServletRequest; |
| 25 | import javax.servlet.http.HttpServletResponse; |
| 26 | import java.io.IOException; |
| 27 | import java.io.PrintWriter; |
| 28 | import java.text.SimpleDateFormat; |
Thomas Vachuska | eeb2056 | 2016-05-16 19:26:42 -0700 | [diff] [blame^] | 29 | import java.util.ArrayList; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 30 | import java.util.Date; |
Thomas Vachuska | eeb2056 | 2016-05-16 19:26:42 -0700 | [diff] [blame^] | 31 | import java.util.List; |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 32 | |
| 33 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 34 | |
| 35 | /** |
| 36 | * Web socket servlet capable of creating web sockets for the STC monitor. |
| 37 | */ |
| 38 | public class WardenServlet extends HttpServlet { |
| 39 | |
| 40 | static Warden warden; |
| 41 | |
Thomas Vachuska | eeb2056 | 2016-05-16 19:26:42 -0700 | [diff] [blame^] | 42 | private SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 43 | |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 44 | @Override |
| 45 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 46 | throws ServletException, IOException { |
| 47 | resp.setContentType("text/plain; charset=UTF-8"); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 48 | try (PrintWriter out = resp.getWriter()) { |
Thomas Vachuska | eeb2056 | 2016-05-16 19:26:42 -0700 | [diff] [blame^] | 49 | if (req.getPathInfo().endsWith("data")) { |
| 50 | String userName = req.getParameter("user"); |
| 51 | if (userName != null) { |
| 52 | printUserInfo(out, userName); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 53 | } else { |
Thomas Vachuska | eeb2056 | 2016-05-16 19:26:42 -0700 | [diff] [blame^] | 54 | printAvailability(out); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 55 | } |
Thomas Vachuska | eeb2056 | 2016-05-16 19:26:42 -0700 | [diff] [blame^] | 56 | } else { |
| 57 | printAvailabilityText(out); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 58 | } |
| 59 | } catch (Exception e) { |
| 60 | resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR); |
| 61 | e.printStackTrace(); |
| 62 | } |
| 63 | } |
| 64 | |
Thomas Vachuska | eeb2056 | 2016-05-16 19:26:42 -0700 | [diff] [blame^] | 65 | private void printUserInfo(PrintWriter out, String userName) { |
| 66 | Reservation reservation = warden.currentUserReservation(userName); |
| 67 | out.println(getCellStatus(null, reservation)); |
| 68 | } |
| 69 | |
| 70 | private void printAvailability(PrintWriter out) { |
| 71 | List<String> list = new ArrayList<>(warden.getCells()); |
| 72 | list.sort(String::compareTo); |
| 73 | for (String cellName : list) { |
| 74 | Reservation reservation = warden.currentCellReservation(cellName); |
| 75 | out.println(getCellStatus(cellName, reservation)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private String getCellStatus(String cellName, Reservation reservation) { |
| 80 | if (reservation != null) { |
| 81 | long expiration = reservation.time + reservation.duration * 60_000; |
| 82 | long remaining = (expiration - System.currentTimeMillis()) / 60_000; |
| 83 | return String.format("%s,%s,%s,%s", reservation.cellName, |
| 84 | reservation.cellSpec, reservation.userName, remaining); |
| 85 | } else if (cellName != null) { |
| 86 | return String.format("%s", cellName); |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | private void printAvailabilityText(PrintWriter out) { |
| 92 | List<String> list = new ArrayList<>(warden.getCells()); |
| 93 | list.sort(String::compareTo); |
| 94 | for (String cellName : list) { |
| 95 | Reservation reservation = warden.currentCellReservation(cellName); |
| 96 | if (reservation != null) { |
| 97 | long expiration = reservation.time + reservation.duration * 60_000; |
| 98 | long remaining = (expiration - System.currentTimeMillis()) / 60_000; |
| 99 | out.println(String.format("%-14s\t%-10s\t%s\t%s\t%s mins (%s remaining)", |
| 100 | cellName + "-" + reservation.cellSpec, |
| 101 | reservation.userName, |
| 102 | fmt.format(new Date(reservation.time)), |
| 103 | fmt.format(new Date(expiration)), |
| 104 | reservation.duration, remaining)); |
| 105 | } else { |
| 106 | out.println(String.format("%-10s\t%-10s", cellName, "available")); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 111 | @Override |
| 112 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| 113 | throws ServletException, IOException { |
| 114 | try (PrintWriter out = resp.getWriter()) { |
| 115 | String sshKey = new String(ByteStreams.toByteArray(req.getInputStream()), "UTF-8"); |
| 116 | String userName = req.getParameter("user"); |
| 117 | String sd = req.getParameter("duration"); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 118 | String spec = req.getParameter("spec"); |
Thomas Vachuska | 0d33700 | 2016-05-04 10:00:18 -0700 | [diff] [blame] | 119 | int duration = isNullOrEmpty(sd) ? 0 : Integer.parseInt(sd); |
Thomas Vachuska | 5420ba3 | 2016-05-13 14:45:25 -0400 | [diff] [blame] | 120 | String cellDefinition = warden.borrowCell(userName, sshKey, duration, spec); |
Thomas Vachuska | 1eff3a6 | 2016-05-03 01:07:24 -0700 | [diff] [blame] | 121 | out.println(cellDefinition); |
| 122 | } catch (Exception e) { |
| 123 | resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR); |
| 124 | e.printStackTrace(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | protected void doDelete(HttpServletRequest req, HttpServletResponse resp) |
| 130 | throws ServletException, IOException { |
| 131 | try (PrintWriter out = resp.getWriter()) { |
| 132 | String userName = req.getParameter("user"); |
| 133 | warden.returnCell(userName); |
| 134 | } catch (Exception e) { |
| 135 | resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR); |
| 136 | e.printStackTrace(); |
| 137 | } |
| 138 | } |
| 139 | } |