blob: 526e61e53935609f30de9470c0a19eaa8387396f [file] [log] [blame]
elinuxhenrik28038562021-09-21 15:43:11 +02001// -
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
21package main
22
23import (
elinuxhenrik382870d2021-09-23 11:09:09 +020024 "encoding/json"
25 "flag"
elinuxhenrik28038562021-09-21 15:43:11 +020026 "fmt"
27 "io"
28 http "net/http"
elinuxhenrik65a53d22021-09-29 15:41:26 +020029 "time"
elinuxhenrik382870d2021-09-23 11:09:09 +020030
31 "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
elinuxhenrik28038562021-09-21 15:43:11 +020032)
33
elinuxhenrik65a53d22021-09-29 15:41:26 +020034var httpClient http.Client
35
elinuxhenrik382870d2021-09-23 11:09:09 +020036func main() {
elinuxhenrik65a53d22021-09-29 15:41:26 +020037 httpClient = http.Client{
38 Timeout: time.Second * 5,
39 }
elinuxhenrik382870d2021-09-23 11:09:09 +020040 port := flag.Int("port", 40935, "The port this consumer will listen on")
41 flag.Parse()
42 http.HandleFunc("/jobs", handleData)
43
elinuxhenrik65a53d22021-09-29 15:41:26 +020044 registerJob(*port)
45
elinuxhenrik4ce49142021-11-23 17:01:07 +010046 fmt.Println("Starting consumer on port: ", *port)
elinuxhenrik7780c9f2021-11-19 08:25:22 +010047 fmt.Println(http.ListenAndServe(fmt.Sprintf(":%v", *port), nil))
elinuxhenrik382870d2021-09-23 11:09:09 +020048}
49
50func registerJob(port int) {
51 jobInfo := struct {
52 JobOwner string `json:"job_owner"`
53 JobResultUri string `json:"job_result_uri"`
54 InfoTypeId string `json:"info_type_id"`
55 JobDefinition string `json:"job_definition"`
elinuxhenrik803e81a2021-10-26 13:22:55 +020056 }{
57 JobOwner: fmt.Sprintf("test%v", port),
58 JobResultUri: fmt.Sprintf("http://localhost:%v/jobs", port),
59 InfoTypeId: "STD_Fault_Messages",
60 JobDefinition: "{}",
61 }
elinuxhenrik4ce49142021-11-23 17:01:07 +010062 fmt.Println("Registering consumer: ", jobInfo)
elinuxhenrik382870d2021-09-23 11:09:09 +020063 body, _ := json.Marshal(jobInfo)
elinuxhenrik6b434262021-12-16 15:22:44 +010064 putErr := restclient.Put(fmt.Sprintf("https://localhost:8083/data-consumer/v1/info-jobs/job%v", port), body, &httpClient)
elinuxhenrik382870d2021-09-23 11:09:09 +020065 if putErr != nil {
elinuxhenrik4ce49142021-11-23 17:01:07 +010066 fmt.Println("Unable to register consumer: ", putErr)
elinuxhenrik382870d2021-09-23 11:09:09 +020067 }
68}
69
elinuxhenrik28038562021-09-21 15:43:11 +020070func handleData(w http.ResponseWriter, req *http.Request) {
71 defer req.Body.Close()
72 if reqData, err := io.ReadAll(req.Body); err == nil {
elinuxhenrikc4960f12021-10-28 16:27:57 +020073 fmt.Println("Consumer received body: ", string(reqData))
elinuxhenrik28038562021-09-21 15:43:11 +020074 }
75}