NONRTRIC - Enrichment Coordinator Service, Changed error codes
[nonrtric.git] / dmaap-mediator-producer / simulator / consumersimulator.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package main
22
23 import (
24         "encoding/json"
25         "flag"
26         "fmt"
27         "io"
28         http "net/http"
29
30         "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
31 )
32
33 func main() {
34         port := flag.Int("port", 40935, "The port this consumer will listen on")
35         flag.Parse()
36         http.HandleFunc("/jobs", handleData)
37
38         fmt.Print("Starting consumer on port: ", *port)
39         http.ListenAndServe(fmt.Sprintf(":%v", *port), nil)
40         registerJob(*port)
41 }
42
43 func registerJob(port int) {
44         jobInfo := struct {
45                 JobOwner      string `json:"job_owner"`
46                 JobResultUri  string `json:"job_result_uri"`
47                 InfoTypeId    string `json:"info_type_id"`
48                 JobDefinition string `json:"job_definition"`
49         }{fmt.Sprintf("test%v", port), fmt.Sprintf("http://localhost:%v/jobs", port), "STD_Fault_Messages", "{}"}
50         fmt.Print("Registering consumer: ", jobInfo)
51         body, _ := json.Marshal(jobInfo)
52         putErr := restclient.Put(fmt.Sprintf("http://localhost:8083/data-consumer/v1/info-jobs/job%v", port), body)
53         if putErr != nil {
54                 fmt.Printf("Unable to register consumer: %v", putErr)
55         }
56 }
57
58 func handleData(w http.ResponseWriter, req *http.Request) {
59         defer req.Body.Close()
60         if reqData, err := io.ReadAll(req.Body); err == nil {
61                 fmt.Print("Consumer received body: ", string(reqData))
62         }
63 }