1+ /**
2+ * @license
3+ * Copyright CERN and copyright holders of ALICE O2. This software is
4+ * distributed under the terms of the GNU General Public License v3 (GPL
5+ * Version 3), copied verbatim in the file "COPYING".
6+ *
7+ * See http://alice-o2.web.cern.ch/license for full licensing information.
8+ *
9+ * In applying this license CERN does not waive the privileges and immunities
10+ * granted to it by virtue of its status as an Intergovernmental Organization
11+ * or submit itself to any jurisdiction.
12+ */
13+
14+ package alice .dip .beam .mode ;
15+
16+
17+ import org .apache .kafka .common .serialization .ByteArraySerializer ;
18+ import org .apache .kafka .common .serialization .IntegerSerializer ;
19+
20+ import alice .dip .adapters .BeamModeProtoAdapter ;
21+ import alice .dip .AliDip2BK ;
22+ import alice .dip .enums .BeamModeEnum ;
23+ import alice .dip .LhcInfoObj ;
24+ import alice .dip .kafka .KafkaProducerInterface ;
25+
26+ import ch .cern .alice .o2 .control .common .Common ;
27+ import ch .cern .alice .o2 .control .events .Events ;
28+
29+ /**
30+ * Kafka producer for LHC Beam Mode events, serialized using Protocol Buffers.
31+ */
32+ public class BeamModeEventsKafkaProducer extends KafkaProducerInterface <Integer , byte []> {
33+ public static final String KAFKA_PRODUCER_TOPIC_DIP = "dip.lhc.beam_mode" ;
34+
35+ /**
36+ * Constructor to create a BeamModeEventsKafkaProducer
37+ * @param bootstrapServers - Kafka bootstrap servers connection string in format of host:port
38+ */
39+ public BeamModeEventsKafkaProducer (String bootstrapServers ) {
40+ super (bootstrapServers , KAFKA_PRODUCER_TOPIC_DIP , new IntegerSerializer (), new ByteArraySerializer ());
41+ AliDip2BK .log (2 , "BeamModeEventsKafkaProducer" , "Initialized producer for topic: " + KAFKA_PRODUCER_TOPIC_DIP );
42+ }
43+
44+ /**
45+ * Given a fill number for partitioning, a LhcInfoObj containing fill information,
46+ * and a timestamp, creates and sends a proto serialized Beam Mode Event to the Kafka topic.
47+ * @param fillNumber - fill number to be used for partition to ensure ordering
48+ * @param fill - LhcInfoObj containing fill information
49+ * @param timestamp - event timestamp at which the beam mode change event was received from DIP
50+ */
51+ public void sendEvent (Integer fillNumber , LhcInfoObj fill , long timestamp ) {
52+ String beamModeStr = fill .getBeamMode ();
53+ BeamModeEnum beamMode = BeamModeProtoAdapter .fromStringToEnum (beamModeStr );
54+
55+ Common .BeamInfo beamInfo = Common .BeamInfo .newBuilder ()
56+ .setStableBeamsStart (fill .getStableBeamStart ())
57+ .setStableBeamsEnd (fill .getStableBeamStop ())
58+ .setFillNumber (fill .fillNo )
59+ .setFillingSchemeName (fill .LHCFillingSchemeName )
60+ .setBeamMode (Common .BeamMode .valueOf (beamMode .name ()))
61+ .setBeamType (fill .beamType )
62+ .build ();
63+
64+ Events .Ev_BeamModeEvent beamModeEvent = Events .Ev_BeamModeEvent .newBuilder ()
65+ .setTimestamp (timestamp )
66+ .setBeamInfo (beamInfo )
67+ .build ();
68+
69+ Events .Event event = Events .Event .newBuilder ()
70+ .setTimestamp (timestamp )
71+ .setTimestampNano ((timestamp ) * 1000000 )
72+ .setBeamModeEvent (beamModeEvent )
73+ .build ();
74+ byte [] value = event .toByteArray ();
75+
76+ send (fillNumber , value );
77+ AliDip2BK .log (2 , "BeamModeEventsKafkaProducer" , "Sent Beam Mode event for fill " + fill .fillNo + " with mode " + fill .getBeamMode () + " at timestamp " + timestamp );
78+ }
79+ }
0 commit comments