3 // ========================LICENSE_START=================================
6 // Copyright (C) 2023: Nordix Foundation
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 // ========================LICENSE_END===================================
24 jsoniter "github.com/json-iterator/go"
25 log "github.com/sirupsen/logrus"
26 "main/common/dataTypes"
28 "main/components/kafkacollector"
38 var ics_server = os.Getenv("ICS")
39 var self = os.Getenv("SELF")
41 // This are optional - set if using SASL protocol is used towards kafka
42 var creds_grant_type = os.Getenv("CREDS_GRANT_TYPE")
44 var bootstrapserver = os.Getenv("KAFKA_SERVER")
46 const config_file = "application_configuration.json"
47 const producer_name = "kafka-producer"
49 var producer_instance_name string = producer_name
51 const reader_queue_length = 100 //Per type job
52 const writer_queue_length = 100 //Per info job
54 var files_volume = os.Getenv("FILES_VOLUME")
56 var data_out_channel = make(chan *dataTypes.KafkaPayload, writer_queue_length)
57 var writer_control = make(chan dataTypes.WriterControl, 1)
59 const registration_delay_short = 2
60 const registration_delay_long = 120
66 // Lock for all internal data
67 var datalock sync.Mutex
70 Init dataTypes.AppStates = iota
75 const registeringProducer = "Registering producer: "
80 //log.SetLevel(log.InfoLevel)
81 log.SetLevel(log.TraceLevel)
83 log.Info("Server starting...")
86 log.Panic("Env SELF not configured")
88 if bootstrapserver == "" {
89 log.Panic("Env KAFKA_SERVER not set")
92 log.Panic("Env ICS not set")
94 if os.Getenv("KP") != "" {
95 producer_instance_name = producer_instance_name + "-" + os.Getenv("KP")
98 go kafkacollector.StartTopicWriter(writer_control, data_out_channel)
100 //Setup proc for periodic type registration
101 var eventChan = make(chan int) //Channel for stopping the proc
102 go periodicRegistration(eventChan)
104 //Wait for term/int signal do try to shut down gracefully
105 sigs := make(chan os.Signal, 1)
106 signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
109 fmt.Printf("Received signal %s - application will terminate\n", sig)
110 eventChan <- 0 // Stop periodic registration
112 defer datalock.Unlock()
113 AppState = Terminating
118 //Wait until all go routines has exited
121 fmt.Println("main routine exit")
122 fmt.Println("server stopped")
125 // == Core functions ==//
126 // Run periodic registration of producers
127 func periodicRegistration(evtch chan int) {
132 if msg == 0 { // Stop thread
135 case <-time.After(time.Duration(delay) * time.Second):
136 ok := registerProducer()
138 delay = registration_delay_long
140 if delay < registration_delay_long {
141 delay += registration_delay_short
143 delay = registration_delay_short
150 func registerProducer() bool {
152 log.Info(registeringProducer, producer_instance_name)
154 file, err := os.ReadFile(config_file)
156 log.Error("Cannot read config file: ", config_file)
158 log.Error(registeringProducer, producer_instance_name, " - failed")
161 data := dataTypes.DataTypes{}
162 err = jsoniter.Unmarshal([]byte(file), &data)
164 log.Error("Cannot parse config file: ", config_file)
166 log.Error(registeringProducer, producer_instance_name, " - failed")
169 var newTypeNames []string
171 for i := 0; i < len(data.ProdDataTypes); i++ {
172 t1 := make(map[string]interface{})
173 t2 := make(map[string]interface{})
175 t2["schema"] = "http://json-schema.org/draft-07/schema#"
176 t2["title"] = data.ProdDataTypes[i].ID
177 t2["description"] = data.ProdDataTypes[i].ID
178 t2["type"] = "object"
180 t1["info_job_data_schema"] = t2
182 json, err := jsoniter.Marshal(t1)
184 log.Error("Cannot create json for type: ", data.ProdDataTypes[i].ID)
186 log.Error(registeringProducer, producer_instance_name, " - failed")
189 ok := utils.SendHttpRequest(json, http.MethodPut, "http://"+ics_server+"/data-producer/v1/info-types/"+data.ProdDataTypes[i].ID, true, creds_grant_type != "")
191 log.Error("Cannot register type: ", data.ProdDataTypes[i].ID)
193 log.Error(registeringProducer, producer_instance_name, " - failed")
196 newTypeNames = append(newTypeNames, data.ProdDataTypes[i].ID)
201 log.Debug("Registering types: ", newTypeNames)
203 defer datalock.Unlock()
205 for _, v := range data.ProdDataTypes {
206 log.Info("Adding type job for type: ", v.ID, " Type added to configuration")
210 dataTypes.InfoTypes = data
211 log.Debug("Datatypes: ", dataTypes.InfoTypes)
212 log.Info(registeringProducer, producer_instance_name, " - OK")
216 func startTypeJob(dp dataTypes.DataType) {
217 log.Info("Starting type job: ", dp.ID)
218 jobRecord := dataTypes.TypeJobRecord{}
220 jobRecord.Job_control = make(chan dataTypes.JobControl, 1)
221 jobRecord.Reader_control = make(chan dataTypes.ReaderControl, 1)
222 jobRecord.Data_in_channel = make(chan *dataTypes.KafkaPayload, reader_queue_length)
223 jobRecord.InfoType = dp.ID
224 jobRecord.InputTopic = dp.KafkaInputTopic
225 jobRecord.GroupId = "kafka-procon-" + dp.ID
226 jobRecord.ClientId = dp.ID + "-" + os.Getenv("KP")
229 case "xml-file-data-to-filestore":
230 go kafkacollector.StartJobXmlFileData(dp.ID, jobRecord.Job_control, jobRecord.Data_in_channel, data_out_channel, "", "pm-files-json")
231 case "xml-file-data":
232 go kafkacollector.StartJobXmlFileData(dp.ID, jobRecord.Job_control, jobRecord.Data_in_channel, data_out_channel, files_volume, "")
236 go kafkacollector.StartTopicReader(dp.KafkaInputTopic, dp.ID, jobRecord.Reader_control, jobRecord.Data_in_channel, jobRecord.GroupId, jobRecord.ClientId)
238 dataTypes.TypeJobs[dp.ID] = jobRecord
239 log.Debug("Type job input type: ", dp.InputJobType)