Merge "Remove rApp Catalogue"
[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                 fmt.Println("Sending CRITICAL")
75                 critical = false
76         } else {
77                 responseBody = getFaultMessage("NORMAL")
78                 fmt.Println("Sending NORMAL")
79                 critical = true
80         }
81         fmt.Fprint(w, string(responseBody))
82 }
83
84 func getFaultMessage(eventSeverity string) []byte {
85         linkFailureMessage := FaultMessage{
86                 Event: Event{
87                         CommonEventHeader: CommonEventHeader{
88                                 Domain:     "fault",
89                                 SourceName: "ERICSSON-O-RU-11220",
90                         },
91                         FaultFields: FaultFields{
92                                 AlarmCondition: "28",
93                                 EventSeverity:  eventSeverity,
94                         },
95                 },
96         }
97         fmt.Printf("Sending message: %v\n", linkFailureMessage)
98
99         messageAsByteArray, _ := json.Marshal(linkFailureMessage)
100         response := [1]string{string(messageAsByteArray)}
101         responseAsByteArray, _ := json.Marshal(response)
102         return responseAsByteArray
103 }