blob: 950fd4269c7e55dc9c44326e13f9aceba2a94a98 [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;
29import java.util.Date;
30
31import static com.google.common.base.Strings.isNullOrEmpty;
32
33/**
34 * Web socket servlet capable of creating web sockets for the STC monitor.
35 */
36public class WardenServlet extends HttpServlet {
37
38 static Warden warden;
39
40 @Override
41 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
42 throws ServletException, IOException {
43 resp.setContentType("text/plain; charset=UTF-8");
44 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
45
46 try (PrintWriter out = resp.getWriter()) {
47 for (String cellName : warden.getCells()) {
48 Reservation reservation = warden.currentCellReservation(cellName);
49 if (reservation != null) {
50 long expiration = reservation.time + reservation.duration * 60_000;
Thomas Vachuska419c8bd2016-05-10 14:36:07 -070051 long remaining = (expiration - System.currentTimeMillis()) / 60_000;
52 out.println(String.format("%-10s\t%-10s\t%s\t%s\t%s mins (%s remaining)",
53 cellName,
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070054 reservation.userName,
55 fmt.format(new Date(reservation.time)),
56 fmt.format(new Date(expiration)),
Thomas Vachuska419c8bd2016-05-10 14:36:07 -070057 reservation.duration, remaining));
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070058 } else {
59 out.println(String.format("%-10s\t%-10s", cellName, "available"));
60 }
61 }
62 } catch (Exception e) {
63 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
64 e.printStackTrace();
65 }
66 }
67
68 @Override
69 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
70 throws ServletException, IOException {
71 try (PrintWriter out = resp.getWriter()) {
72 String sshKey = new String(ByteStreams.toByteArray(req.getInputStream()), "UTF-8");
73 String userName = req.getParameter("user");
74 String sd = req.getParameter("duration");
Thomas Vachuska0d337002016-05-04 10:00:18 -070075 int duration = isNullOrEmpty(sd) ? 0 : Integer.parseInt(sd);
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070076 String cellDefinition = warden.borrowCell(userName, sshKey, duration);
77 out.println(cellDefinition);
78 } catch (Exception e) {
79 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
80 e.printStackTrace();
81 }
82 }
83
84 @Override
85 protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
86 throws ServletException, IOException {
87 try (PrintWriter out = resp.getWriter()) {
88 String userName = req.getParameter("user");
89 warden.returnCell(userName);
90 } catch (Exception e) {
91 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
92 e.printStackTrace();
93 }
94 }
95}