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 */ 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 public VmChannel setVmDefinition(VmDefinition definition) { 059 this.definition = definition; 060 return this; 061 } 062 063 /** 064 * Returns the last known definition of the VM. 065 * 066 * @return the defintion 067 */ 068 public VmDefinition vmDefinition() { 069 return definition; 070 } 071 072 /** 073 * Gets the last processed generation. Returns -1 if no 074 * definition has been processed yet. 075 * 076 * @return the generation 077 */ 078 public long generation() { 079 return generation; 080 } 081 082 /** 083 * Sets the last processed generation. 084 * 085 * @param generation the generation to set 086 * @return true if value has changed 087 */ 088 public boolean setGeneration(long generation) { 089 if (this.generation == generation) { 090 return false; 091 } 092 this.generation = generation; 093 return true; 094 } 095 096 /** 097 * Returns the pipeline. 098 * 099 * @return the event pipeline 100 */ 101 public EventPipeline pipeline() { 102 return pipeline; 103 } 104 105 /** 106 * Fire the given event on this channel, using the associated 107 * {@link #pipeline()}. 108 * 109 * @param <T> the generic type 110 * @param event the event 111 * @return the t 112 */ 113 public <T extends Event<?>> T fire(T event) { 114 pipeline.fire(event, this); 115 return event; 116 } 117 118 /** 119 * Returns the API client. 120 * 121 * @return the API client 122 */ 123 public K8sClient client() { 124 return client; 125 } 126}