001/*
002 * VM-Operator
003 * Copyright (C) 2023 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.vmoperator.runner.qemu.events;
020
021import com.fasterxml.jackson.databind.JsonNode;
022import java.util.Optional;
023import org.jgrapes.core.Event;
024
025/**
026 * An {@link Event} that signals the reception of a QMP event from 
027 * the Qemu process.
028 */
029public class MonitorEvent extends Event<Void> {
030
031    private static final String EVENT_DATA = "data";
032
033    /**
034     * The kind of monitor event.
035     */
036    public enum Kind {
037        READY, POWERDOWN, DEVICE_TRAY_MOVED, BALLOON_CHANGE, SHUTDOWN,
038        SPICE_CONNECTED, SPICE_INITIALIZED, SPICE_DISCONNECTED, VSERPORT_CHANGE
039    }
040
041    private final Kind kind;
042    private final JsonNode data;
043
044    /**
045     * Create event from response.
046     *
047     * @param response the response
048     * @return the optional
049     */
050    @SuppressWarnings("PMD.TooFewBranchesForASwitchStatement")
051    public static Optional<MonitorEvent> from(JsonNode response) {
052        try {
053            var kind = Kind.valueOf(response.get("event").asText());
054            switch (kind) {
055            case POWERDOWN:
056                return Optional.of(new PowerdownEvent(kind, null));
057            case DEVICE_TRAY_MOVED:
058                return Optional
059                    .of(new TrayMovedEvent(kind, response.get(EVENT_DATA)));
060            case BALLOON_CHANGE:
061                return Optional.of(
062                    new BalloonChangeEvent(kind, response.get(EVENT_DATA)));
063            case SHUTDOWN:
064                return Optional
065                    .of(new ShutdownEvent(kind, response.get(EVENT_DATA)));
066            case SPICE_CONNECTED:
067                return Optional.of(new SpiceConnectedEvent(kind,
068                    response.get(EVENT_DATA)));
069            case SPICE_INITIALIZED:
070                return Optional.of(new SpiceInitializedEvent(kind,
071                    response.get(EVENT_DATA)));
072            case SPICE_DISCONNECTED:
073                return Optional.of(new SpiceDisconnectedEvent(kind,
074                    response.get(EVENT_DATA)));
075            case VSERPORT_CHANGE:
076                return Optional.of(new VserportChangeEvent(kind,
077                    response.get(EVENT_DATA)));
078            default:
079                return Optional
080                    .of(new MonitorEvent(kind, response.get(EVENT_DATA)));
081            }
082        } catch (IllegalArgumentException e) {
083            return Optional.empty();
084        }
085    }
086
087    /**
088     * Instantiates a new monitor event.
089     *
090     * @param kind the kind
091     * @param data the data
092     */
093    protected MonitorEvent(Kind kind, JsonNode data) {
094        this.kind = kind;
095        this.data = data;
096    }
097
098    /**
099     * Returns the kind of event.
100     *
101     * @return the kind
102     */
103    public Kind kind() {
104        return kind;
105    }
106
107    /**
108     * Returns the data associated with the event.
109     *
110     * @return the object[]
111     */
112    public JsonNode data() {
113        return data;
114    }
115}