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.Channel;
024import org.jgrapes.core.Components;
025import org.jgrapes.core.Event;
026
027/**
028 * An {@link Event} that signals the reception of a QMP event from 
029 * the Qemu process.
030 */
031public class MonitorEvent extends Event<Void> {
032
033    private static final String EVENT_DATA = "data";
034
035    /**
036     * The kind of monitor event.
037     */
038    public enum Kind {
039        READY, POWERDOWN, DEVICE_TRAY_MOVED, BALLOON_CHANGE, SHUTDOWN,
040        SPICE_CONNECTED, SPICE_INITIALIZED, SPICE_DISCONNECTED, VSERPORT_CHANGE
041    }
042
043    private final Kind kind;
044    private final JsonNode data;
045
046    /**
047     * Create event from response.
048     *
049     * @param response the response
050     * @return the optional
051     */
052    @SuppressWarnings("PMD.TooFewBranchesForASwitchStatement")
053    public static Optional<MonitorEvent> from(JsonNode response) {
054        try {
055            var kind = Kind.valueOf(response.get("event").asText());
056            switch (kind) {
057            case POWERDOWN:
058                return Optional.of(new PowerdownEvent(kind, null));
059            case DEVICE_TRAY_MOVED:
060                return Optional
061                    .of(new TrayMovedEvent(kind, response.get(EVENT_DATA)));
062            case BALLOON_CHANGE:
063                return Optional.of(
064                    new BalloonChangeEvent(kind, response.get(EVENT_DATA)));
065            case SHUTDOWN:
066                return Optional
067                    .of(new ShutdownEvent(kind, response.get(EVENT_DATA)));
068            case SPICE_CONNECTED:
069                return Optional.of(new SpiceConnectedEvent(kind,
070                    response.get(EVENT_DATA)));
071            case SPICE_INITIALIZED:
072                return Optional.of(new SpiceInitializedEvent(kind,
073                    response.get(EVENT_DATA)));
074            case SPICE_DISCONNECTED:
075                return Optional.of(new SpiceDisconnectedEvent(kind,
076                    response.get(EVENT_DATA)));
077            case VSERPORT_CHANGE:
078                return Optional.of(new VserportChangeEvent(kind,
079                    response.get(EVENT_DATA)));
080            default:
081                return Optional
082                    .of(new MonitorEvent(kind, response.get(EVENT_DATA)));
083            }
084        } catch (IllegalArgumentException e) {
085            return Optional.empty();
086        }
087    }
088
089    /**
090     * Instantiates a new monitor event.
091     *
092     * @param kind the kind
093     * @param data the data
094     */
095    protected MonitorEvent(Kind kind, JsonNode data) {
096        this.kind = kind;
097        this.data = data;
098    }
099
100    /**
101     * Returns the kind of event.
102     *
103     * @return the kind
104     */
105    public Kind kind() {
106        return kind;
107    }
108
109    /**
110     * Returns the data associated with the event.
111     *
112     * @return the object[]
113     */
114    public JsonNode data() {
115        return data;
116    }
117
118    /*
119     * (non-Javadoc)
120     * 
121     * @see java.lang.Object#toString()
122     */
123    @Override
124    public String toString() {
125        StringBuilder builder = new StringBuilder();
126        builder.append(Components.objectName(this)).append(" [").append(data);
127        if (channels() != null) {
128            builder.append(", channels=").append(Channel.toString(channels()));
129        }
130        builder.append(']');
131        return builder.toString();
132    }
133}