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