blob: 27e7ec1c686082191cfd468055039e578e415ec5 [file] [log] [blame]
Thomas Vachuska1eff3a62016-05-03 01:07:24 -07001/*
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
17package org.onlab.warden;
18
19import com.google.common.io.ByteStreams;
20import org.eclipse.jetty.server.Response;
21
22import javax.servlet.ServletException;
23import javax.servlet.http.HttpServlet;
24import javax.servlet.http.HttpServletRequest;
25import javax.servlet.http.HttpServletResponse;
26import java.io.IOException;
27import java.io.PrintWriter;
28import java.text.SimpleDateFormat;
Thomas Vachuskaeeb20562016-05-16 19:26:42 -070029import java.util.ArrayList;
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070030import java.util.Date;
Thomas Vachuskaeeb20562016-05-16 19:26:42 -070031import java.util.List;
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070032
33import static com.google.common.base.Strings.isNullOrEmpty;
34
35/**
36 * Web socket servlet capable of creating web sockets for the STC monitor.
37 */
38public class WardenServlet extends HttpServlet {
39
40 static Warden warden;
41
Thomas Vachuskaeeb20562016-05-16 19:26:42 -070042 private SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
43
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070044 @Override
45 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
46 throws ServletException, IOException {
47 resp.setContentType("text/plain; charset=UTF-8");
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070048 try (PrintWriter out = resp.getWriter()) {
Thomas Vachuskaeeb20562016-05-16 19:26:42 -070049 if (req.getPathInfo().endsWith("data")) {
50 String userName = req.getParameter("user");
51 if (userName != null) {
52 printUserInfo(out, userName);
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070053 } else {
Thomas Vachuskaeeb20562016-05-16 19:26:42 -070054 printAvailability(out);
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070055 }
Thomas Vachuskaeeb20562016-05-16 19:26:42 -070056 } else {
57 printAvailabilityText(out);
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070058 }
59 } catch (Exception e) {
60 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
61 e.printStackTrace();
62 }
63 }
64
Thomas Vachuskaeeb20562016-05-16 19:26:42 -070065 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 Vachuska1eff3a62016-05-03 01:07:24 -0700111 @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 Vachuska5420ba32016-05-13 14:45:25 -0400118 String spec = req.getParameter("spec");
Thomas Vachuska0d337002016-05-04 10:00:18 -0700119 int duration = isNullOrEmpty(sd) ? 0 : Integer.parseInt(sd);
Thomas Vachuska5420ba32016-05-13 14:45:25 -0400120 String cellDefinition = warden.borrowCell(userName, sshKey, duration, spec);
Thomas Vachuska1eff3a62016-05-03 01:07:24 -0700121 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}