blob: 9f6ad3315e14c36fd0da5b5e0d3d654845cc32d5 [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;
51 out.println(String.format("%-10s\t%-10s\t%s\t%s\t%s minutes", cellName,
52 reservation.userName,
53 fmt.format(new Date(reservation.time)),
54 fmt.format(new Date(expiration)),
55 reservation.duration));
56 } else {
57 out.println(String.format("%-10s\t%-10s", cellName, "available"));
58 }
59 }
60 } catch (Exception e) {
61 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
62 e.printStackTrace();
63 }
64 }
65
66 @Override
67 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
68 throws ServletException, IOException {
69 try (PrintWriter out = resp.getWriter()) {
70 String sshKey = new String(ByteStreams.toByteArray(req.getInputStream()), "UTF-8");
71 String userName = req.getParameter("user");
72 String sd = req.getParameter("duration");
73 int duration = isNullOrEmpty(sd) ? 60 : Integer.parseInt(sd);
74 String cellDefinition = warden.borrowCell(userName, sshKey, duration);
75 out.println(cellDefinition);
76 } catch (Exception e) {
77 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
78 e.printStackTrace();
79 }
80 }
81
82 @Override
83 protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
84 throws ServletException, IOException {
85 try (PrintWriter out = resp.getWriter()) {
86 String userName = req.getParameter("user");
87 warden.returnCell(userName);
88 } catch (Exception e) {
89 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
90 e.printStackTrace();
91 }
92 }
93}