elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 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 ( |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 24 | "encoding/json" |
| 25 | "flag" |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 26 | "fmt" |
| 27 | "io" |
| 28 | http "net/http" |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 29 | "time" |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 30 | |
| 31 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient" |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 34 | var httpClient http.Client |
| 35 | |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 36 | func main() { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 37 | httpClient = http.Client{ |
| 38 | Timeout: time.Second * 5, |
| 39 | } |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 40 | port := flag.Int("port", 40935, "The port this consumer will listen on") |
| 41 | flag.Parse() |
| 42 | http.HandleFunc("/jobs", handleData) |
| 43 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 44 | registerJob(*port) |
| 45 | |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 46 | fmt.Print("Starting consumer on port: ", *port) |
| 47 | http.ListenAndServe(fmt.Sprintf(":%v", *port), nil) |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | func 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"` |
elinuxhenrik | 803e81a | 2021-10-26 13:22:55 +0200 | [diff] [blame] | 56 | }{ |
| 57 | JobOwner: fmt.Sprintf("test%v", port), |
| 58 | JobResultUri: fmt.Sprintf("http://localhost:%v/jobs", port), |
| 59 | InfoTypeId: "STD_Fault_Messages", |
| 60 | JobDefinition: "{}", |
| 61 | } |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 62 | fmt.Print("Registering consumer: ", jobInfo) |
| 63 | body, _ := json.Marshal(jobInfo) |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 64 | putErr := restclient.Put(fmt.Sprintf("http://localhost:8083/data-consumer/v1/info-jobs/job%v", port), body, &httpClient) |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 65 | if putErr != nil { |
| 66 | fmt.Printf("Unable to register consumer: %v", putErr) |
| 67 | } |
| 68 | } |
| 69 | |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 70 | func handleData(w http.ResponseWriter, req *http.Request) { |
| 71 | defer req.Body.Close() |
| 72 | if reqData, err := io.ReadAll(req.Body); err == nil { |
elinuxhenrik | c4960f1 | 2021-10-28 16:27:57 +0200 | [diff] [blame] | 73 | fmt.Println("Consumer received body: ", string(reqData)) |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 74 | } |
| 75 | } |