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 com.fasterxml.jackson.databind.node.ObjectNode;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026import org.jdrupes.vmoperator.runner.qemu.commands.QmpCommand;
027
028/**
029 * A {@link MonitorResult} that reports the hot pluggable CPU status.
030 */
031public class HotpluggableCpuStatus extends MonitorResult {
032
033    @SuppressWarnings("PMD.ImmutableField")
034    private List<ObjectNode> usedCpus = new ArrayList<>();
035    @SuppressWarnings("PMD.ImmutableField")
036    private List<ObjectNode> unusedCpus = new ArrayList<>();
037
038    /**
039     * Instantiates a new hotpluggable cpu result.
040     *
041     * @param command the command
042     * @param response the response
043     */
044    public HotpluggableCpuStatus(QmpCommand command, JsonNode response) {
045        super(command, response);
046        if (!successful()) {
047            return;
048        }
049
050        // Sort
051        for (var itr = values().iterator(); itr.hasNext();) {
052            ObjectNode cpu = (ObjectNode) itr.next();
053            if (cpu.has("qom-path")) {
054                usedCpus.add(cpu);
055            } else {
056                unusedCpus.add(cpu);
057            }
058        }
059        usedCpus = Collections.unmodifiableList(usedCpus);
060        unusedCpus = Collections.unmodifiableList(unusedCpus);
061    }
062
063    /**
064     * Gets the used cpus.
065     *
066     * @return the usedCpus
067     */
068    public List<ObjectNode> usedCpus() {
069        return usedCpus;
070    }
071
072    /**
073     * Gets the unused cpus.
074     *
075     * @return the unusedCpus
076     */
077    public List<ObjectNode> unusedCpus() {
078        return unusedCpus;
079    }
080
081}