Update O-RU Fronthaul Recovery usecase
[nonrtric.git] / test / usecases / oruclosedlooprecovery / goversion / stub / producer / producerstub.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         "bytes"
25         "encoding/json"
26         "fmt"
27         "net/http"
28         "time"
29
30         "github.com/gorilla/mux"
31         "oransc.org/usecase/oruclosedloop/internal/ves"
32 )
33
34 var started bool
35
36 func main() {
37         r := mux.NewRouter()
38         r.HandleFunc("/create/{jobId}", createJobHandler).Methods(http.MethodPut)
39         r.HandleFunc("/delete/{jobId}", deleteJobHandler).Methods(http.MethodDelete)
40
41         fmt.Println("Listening on port 8085")
42         fmt.Println(http.ListenAndServe(":8085", r))
43 }
44
45 func createJobHandler(w http.ResponseWriter, r *http.Request) {
46         vars := mux.Vars(r)
47         id, ok := vars["jobId"]
48         if !ok {
49                 http.Error(w, "No job ID provided", http.StatusBadRequest)
50                 return
51         }
52
53         started = true
54         fmt.Println("Start pushing messages for job: ", id)
55         go startPushingMessages()
56 }
57
58 func deleteJobHandler(w http.ResponseWriter, r *http.Request) {
59         vars := mux.Vars(r)
60         id, ok := vars["jobId"]
61         if !ok {
62                 http.Error(w, "No job ID provided", http.StatusBadRequest)
63                 return
64         }
65
66         fmt.Println("Stop pushing messages for job: ", id)
67         started = false
68 }
69
70 func startPushingMessages() {
71         message := ves.FaultMessage{
72                 Event: ves.Event{
73                         CommonEventHeader: ves.CommonEventHeader{
74                                 Domain:     "fault",
75                                 SourceName: "ERICSSON-O-RU-11220",
76                         },
77                         FaultFields: ves.FaultFields{
78                                 AlarmCondition: "28",
79                         },
80                 },
81         }
82
83         client := &http.Client{
84                 Timeout: 5 * time.Second,
85         }
86
87         critical := true
88         for range time.Tick(2 * time.Second) {
89                 if !started {
90                         break
91                 }
92                 if critical {
93                         message.Event.FaultFields.EventSeverity = "CRITICAL"
94                         critical = false
95                 } else {
96                         critical = true
97                         message.Event.FaultFields.EventSeverity = "NORMAL"
98                 }
99                 m, _ := json.Marshal(message)
100                 msgToSend, _ := json.Marshal([]string{string(m)})
101
102                 req, _ := http.NewRequest(http.MethodPost, "http://localhost:40935", bytes.NewBuffer(msgToSend))
103                 req.Header.Set("Content-Type", "application/json; charset=utf-8")
104
105                 r, err := client.Do(req)
106                 if err != nil {
107                         fmt.Println("Error sending to consumer: ", err)
108                 }
109                 fmt.Printf("Sent %v message to consumer. Got response %v\n", message.Event.FaultFields.EventSeverity, r.Status)
110         }
111 }