2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
31 "github.com/gorilla/mux"
32 "oransc.org/usecase/oruclosedloop/internal/ves"
39 r.HandleFunc("/create/{jobId}", createJobHandler).Methods(http.MethodPut)
40 r.HandleFunc("/delete/{jobId}", deleteJobHandler).Methods(http.MethodDelete)
42 fmt.Println("Listening on port 8085")
43 fmt.Println(http.ListenAndServe(":8085", r))
46 func createJobHandler(w http.ResponseWriter, r *http.Request) {
48 id, ok := vars["jobId"]
50 http.Error(w, "No job ID provided", http.StatusBadRequest)
55 fmt.Println("Start pushing messages for job: ", id)
56 go startPushingMessages()
59 func deleteJobHandler(w http.ResponseWriter, r *http.Request) {
61 id, ok := vars["jobId"]
63 http.Error(w, "No job ID provided", http.StatusBadRequest)
67 fmt.Println("Stop pushing messages for job: ", id)
71 func getEnv(key string, defaultVal string) string {
72 if value, exists := os.LookupEnv(key); exists {
79 func startPushingMessages() {
80 message := ves.FaultMessage{
82 CommonEventHeader: ves.CommonEventHeader{
84 SourceName: "ERICSSON-O-RU-11220",
86 FaultFields: ves.FaultFields{
92 client := &http.Client{
93 Timeout: 5 * time.Second,
97 for range time.Tick(2 * time.Second) {
102 message.Event.FaultFields.EventSeverity = "CRITICAL"
106 message.Event.FaultFields.EventSeverity = "NORMAL"
108 m, _ := json.Marshal(message)
109 msgToSend, _ := json.Marshal([]string{string(m)})
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")
115 r, err := client.Do(req)
117 fmt.Println("Error sending to consumer: ", err)
119 fmt.Printf("Sent %v message to consumer. Got response %v\n", message.Event.FaultFields.EventSeverity, r.Status)