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.manager.events;
020
021import org.jdrupes.vmoperator.common.K8sObserver;
022import org.jdrupes.vmoperator.common.VmDefinition;
023import org.jgrapes.core.Channel;
024import org.jgrapes.core.Components;
025import org.jgrapes.core.Event;
026
027/**
028 * Indicates a change in a VM "resource". Note that the resource
029 * combines the VM CR's metadata (mostly immutable), the VM CR's
030 * "spec" part, the VM CR's "status" subresource and state information
031 * from the pod. Consumers that are only interested in "spec" changes
032 * should check {@link #specChanged()} before processing the event any
033 * further. 
034 */
035@SuppressWarnings("PMD.DataClass")
036public class VmResourceChanged extends Event<Void> {
037
038    private final K8sObserver.ResponseType type;
039    private final VmDefinition vmDefinition;
040    private final boolean specChanged;
041    private final boolean podChanged;
042
043    /**
044     * Instantiates a new VM changed event.
045     *
046     * @param type the type
047     * @param vmDefinition the VM definition
048     * @param specChanged the spec part changed
049     */
050    public VmResourceChanged(K8sObserver.ResponseType type,
051            VmDefinition vmDefinition, boolean specChanged,
052            boolean podChanged) {
053        this.type = type;
054        this.vmDefinition = vmDefinition;
055        this.specChanged = specChanged;
056        this.podChanged = podChanged;
057    }
058
059    /**
060     * Returns the type.
061     *
062     * @return the type
063     */
064    public K8sObserver.ResponseType type() {
065        return type;
066    }
067
068    /**
069     * Return the VM definition.
070     *
071     * @return the VM definition
072     */
073    public VmDefinition vmDefinition() {
074        return vmDefinition;
075    }
076
077    /**
078     * Indicates if the "spec" part changed.
079     */
080    public boolean specChanged() {
081        return specChanged;
082    }
083
084    /**
085     * Indicates if the pod status changed.
086     */
087    public boolean podChanged() {
088        return podChanged;
089    }
090
091    @Override
092    public String toString() {
093        StringBuilder builder = new StringBuilder();
094        builder.append(Components.objectName(this)).append(" [")
095            .append(vmDefinition.name()).append(' ').append(type);
096        if (channels() != null) {
097            builder.append(", channels=").append(Channel.toString(channels()));
098        }
099        builder.append(']');
100        return builder.toString();
101    }
102}