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
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            default:
076                return Optional
077                    .of(new MonitorEvent(kind, response.get(EVENT_DATA)));
078            }
079        } catch (IllegalArgumentException e) {
080            return Optional.empty();
081        }
082    }
083
084    /**
085     * Instantiates a new monitor event.
086     *
087     * @param kind the kind
088     * @param data the data
089     */
090    protected MonitorEvent(Kind kind, JsonNode data) {
091        this.kind = kind;
092        this.data = data;
093    }
094
095    /**
096     * Returns the kind of event.
097     *
098     * @return the kind
099     */
100    public Kind kind() {
101        return kind;
102    }
103
104    /**
105     * Returns the data associated with the event.
106     *
107     * @return the object[]
108     */
109    public JsonNode data() {
110        return data;
111    }
112}