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    }
039
040    private final Kind kind;
041    private final JsonNode data;
042
043    /**
044     * Create event from response.
045     *
046     * @param response the response
047     * @return the optional
048     */
049    @SuppressWarnings("PMD.TooFewBranchesForASwitchStatement")
050    public static Optional<MonitorEvent> from(JsonNode response) {
051        try {
052            var kind = MonitorEvent.Kind
053                .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            default:
067                return Optional
068                    .of(new MonitorEvent(kind, response.get(EVENT_DATA)));
069            }
070        } catch (IllegalArgumentException e) {
071            return Optional.empty();
072        }
073    }
074
075    /**
076     * Instantiates a new monitor event.
077     *
078     * @param kind the kind
079     * @param data the data
080     */
081    protected MonitorEvent(Kind kind, JsonNode data) {
082        this.kind = kind;
083        this.data = data;
084    }
085
086    /**
087     * Returns the kind of event.
088     *
089     * @return the kind
090     */
091    public Kind kind() {
092        return kind;
093    }
094
095    /**
096     * Returns the data associated with the event.
097     *
098     * @return the object[]
099     */
100    public JsonNode data() {
101        return data;
102    }
103}