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