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    public static Optional<MonitorEvent> from(JsonNode response) {
053        try {
054            var kind = Kind.valueOf(response.get("event").asText());
055            switch (kind) {
056            case POWERDOWN:
057                return Optional.of(new PowerdownEvent(kind, null));
058            case DEVICE_TRAY_MOVED:
059                return Optional
060                    .of(new TrayMovedEvent(kind, response.get(EVENT_DATA)));
061            case BALLOON_CHANGE:
062                return Optional.of(
063                    new BalloonChangeEvent(kind, response.get(EVENT_DATA)));
064            case SHUTDOWN:
065                return Optional
066                    .of(new ShutdownEvent(kind, response.get(EVENT_DATA)));
067            case SPICE_CONNECTED:
068                return Optional.of(new SpiceConnectedEvent(kind,
069                    response.get(EVENT_DATA)));
070            case SPICE_INITIALIZED:
071                return Optional.of(new SpiceInitializedEvent(kind,
072                    response.get(EVENT_DATA)));
073            case SPICE_DISCONNECTED:
074                return Optional.of(new SpiceDisconnectedEvent(kind,
075                    response.get(EVENT_DATA)));
076            case VSERPORT_CHANGE:
077                return Optional.of(new VserportChangeEvent(kind,
078                    response.get(EVENT_DATA)));
079            default:
080                return Optional
081                    .of(new MonitorEvent(kind, response.get(EVENT_DATA)));
082            }
083        } catch (IllegalArgumentException e) {
084            return Optional.empty();
085        }
086    }
087
088    /**
089     * Instantiates a new monitor event.
090     *
091     * @param kind the kind
092     * @param data the data
093     */
094    protected MonitorEvent(Kind kind, JsonNode data) {
095        this.kind = kind;
096        this.data = data;
097    }
098
099    /**
100     * Returns the kind of event.
101     *
102     * @return the kind
103     */
104    public Kind kind() {
105        return kind;
106    }
107
108    /**
109     * Returns the data associated with the event.
110     *
111     * @return the object[]
112     */
113    public JsonNode data() {
114        return data;
115    }
116
117    /*
118     * (non-Javadoc)
119     * 
120     * @see java.lang.Object#toString()
121     */
122    @Override
123    public String toString() {
124        StringBuilder builder = new StringBuilder();
125        builder.append(Components.objectName(this)).append(" [").append(data);
126        if (channels() != null) {
127            builder.append(", channels=").append(Channel.toString(channels()));
128        }
129        builder.append(']');
130        return builder.toString();
131    }
132}