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.K8sClient;
022import org.jdrupes.vmoperator.common.VmDefinition;
023import org.jgrapes.core.Channel;
024import org.jgrapes.core.Event;
025import org.jgrapes.core.EventPipeline;
026import org.jgrapes.core.Subchannel.DefaultSubchannel;
027
028/**
029 * A subchannel used to send the events related to a specific VM.
030 */
031@SuppressWarnings("PMD.DataClass")
032public class VmChannel extends DefaultSubchannel {
033
034    private final EventPipeline pipeline;
035    private final K8sClient client;
036    private VmDefinition definition;
037    private long generation = -1;
038
039    /**
040     * Instantiates a new watch channel.
041     *
042     * @param mainChannel the main channel
043     * @param pipeline the pipeline
044     * @param client the client
045     */
046    public VmChannel(Channel mainChannel, EventPipeline pipeline,
047            K8sClient client) {
048        super(mainChannel);
049        this.pipeline = pipeline;
050        this.client = client;
051    }
052
053    /**
054     * Sets the last known definition of the resource.
055     *
056     * @param definition the definition
057     * @return the watch channel
058     */
059    @SuppressWarnings("PMD.LinguisticNaming")
060    public VmChannel setVmDefinition(VmDefinition definition) {
061        this.definition = definition;
062        return this;
063    }
064
065    /**
066     * Returns the last known definition of the VM.
067     *
068     * @return the defintion
069     */
070    public VmDefinition vmDefinition() {
071        return definition;
072    }
073
074    /**
075     * Gets the last processed generation. Returns -1 if no
076     * definition has been processed yet.
077     *
078     * @return the generation
079     */
080    public long generation() {
081        return generation;
082    }
083
084    /**
085     * Sets the last processed generation.
086     *
087     * @param generation the generation to set
088     * @return true if value has changed
089     */
090    @SuppressWarnings("PMD.LinguisticNaming")
091    public boolean setGeneration(long generation) {
092        if (this.generation == generation) {
093            return false;
094        }
095        this.generation = generation;
096        return true;
097    }
098
099    /**
100     * Returns the pipeline.
101     *
102     * @return the event pipeline
103     */
104    public EventPipeline pipeline() {
105        return pipeline;
106    }
107
108    /**
109     * Fire the given event on this channel, using the associated
110     * {@link #pipeline()}.
111     *
112     * @param <T> the generic type
113     * @param event the event
114     * @return the t
115     */
116    public <T extends Event<?>> T fire(T event) {
117        pipeline.fire(event, this);
118        return event;
119    }
120
121    /**
122     * Returns the API client.
123     *
124     * @return the API client
125     */
126    public K8sClient client() {
127        return client;
128    }
129}