Change of ECS to ICS in test env
[nonrtric.git] / dmaap-mediator-producer / stub / dmaap / mrstub.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         "encoding/json"
25         "flag"
26         "fmt"
27         "math/rand"
28         "net/http"
29         "time"
30 )
31
32 var r = rand.New(rand.NewSource(time.Now().UnixNano()))
33
34 type FaultMessage struct {
35         Event Event `json:"event"`
36 }
37
38 type Event struct {
39         CommonEventHeader CommonEventHeader `json:"commonEventHeader"`
40         FaultFields       FaultFields       `json:"faultFields"`
41 }
42
43 type CommonEventHeader struct {
44         Domain     string `json:"domain"`
45         SourceName string `json:"sourceName"`
46 }
47
48 type FaultFields struct {
49         AlarmCondition string `json:"alarmCondition"`
50         EventSeverity  string `json:"eventSeverity"`
51 }
52
53 func main() {
54         port := flag.Int("port", 3905, "The port this message router will listen on")
55         flag.Parse()
56
57         http.HandleFunc("/events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/STD_Fault_Messages", handleData)
58
59         fmt.Print("Starting mr on port: ", *port)
60         fmt.Println(http.ListenAndServeTLS(fmt.Sprintf(":%v", *port), "../../security/producer.crt", "../../security/producer.key", nil))
61
62 }
63
64 var critical = true
65
66 func handleData(w http.ResponseWriter, req *http.Request) {
67         time.Sleep(time.Duration(r.Intn(3)) * time.Second)
68
69         w.Header().Set("Content-Type", "application/json")
70
71         var responseBody []byte
72         if critical {
73                 responseBody = getFaultMessage("CRITICAL")
74                 critical = false
75         } else {
76                 responseBody = getFaultMessage("NORMAL")
77                 critical = true
78         }
79         // w.Write(responseBody)
80         fmt.Fprint(w, string(responseBody))
81 }
82
83 func getFaultMessage(eventSeverity string) []byte {
84         linkFailureMessage := FaultMessage{
85                 Event: Event{
86                         CommonEventHeader: CommonEventHeader{
87                                 Domain:     "fault",
88                                 SourceName: "ERICSSON-O-RU-11220",
89                         },
90                         FaultFields: FaultFields{
91                                 AlarmCondition: "28",
92                                 EventSeverity:  eventSeverity,
93                         },
94                 },
95         }
96         fmt.Printf("Sending message: %v\n", linkFailureMessage)
97
98         messageAsByteArray, _ := json.Marshal(linkFailureMessage)
99         response := [1]string{string(messageAsByteArray)}
100         responseAsByteArray, _ := json.Marshal(response)
101         return responseAsByteArray
102 }