X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=alarm%2Falarm.go;h=f8d909af629b5ae94036a75e60a92336eaa58790;hb=3649fae7a06ad3ad099d0aa4e68f7ca3a2ae5a87;hp=e0bd93b0bf03acb65323b7d3fca15ff7bd4dd5f5;hpb=af0c57085f6f34c609d63facc4d224e2d733c538;p=ric-plt%2Falarm-go.git diff --git a/alarm/alarm.go b/alarm/alarm.go index e0bd93b..f8d909a 100755 --- a/alarm/alarm.go +++ b/alarm/alarm.go @@ -21,17 +21,21 @@ package alarm import ( + "bytes" "encoding/json" "errors" "fmt" + "io/ioutil" "log" + "net/http" + "os" "time" "unsafe" ) /* #cgo CFLAGS: -I../ -#cgo LDFLAGS: -lrmr_nng -lnng +#cgo LDFLAGS: -lrmr_si #include "utils.h" */ @@ -41,17 +45,23 @@ import "C" // The MO and APP identities are given as a parameters. // The identities are used when raising/clearing alarms, unless provided by the applications. func InitAlarm(mo, id string) (*RICAlarm, error) { - if ctx := C.rmrInit(); ctx != nil { - r := &RICAlarm{ - moId: mo, - appId: id, - rmrCtx: ctx, - } + r := &RICAlarm{ + moId: mo, + appId: id, + managerUrl: ALARM_MANAGER_HTTP_URL, + } - return r, nil + if os.Getenv("ALARM_MANAGER_URL") != "" { + r.managerUrl = os.Getenv("ALARM_MANAGER_URL") } - return nil, errors.New("rmrInit failed!") + if os.Getenv("ALARM_IF_RMR") == "" { + go InitRMR(r, "") + } else { + go InitRMR(r, ALARM_MANAGER_RMR_URL) + } + + return r, nil } // Create a new Alarm instance @@ -68,10 +78,18 @@ func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alar // Create a new AlarmMessage instance func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage { - alarmTime := time.Now().UnixNano() / 1000 + alarmTime := time.Now().UnixNano() return AlarmMessage{a, alarmAction, alarmTime} } +func (r *RICAlarm) SetManagedObjectId(mo string) { + r.moId = mo +} + +func (r *RICAlarm) SetApplicationId(app string) { + r.appId = app +} + // Raise a RIC alarm func (r *RICAlarm) Raise(a Alarm) error { r.mutex.Lock() @@ -120,12 +138,23 @@ func (r *RICAlarm) AlarmString(a AlarmMessage) string { } func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error { - log.Println("Sending alarm: ", r.AlarmString(a)) payload, err := json.Marshal(a) if err != nil { + log.Println("json.Marshal failed with error: ", err) return err } + log.Println("Sending alarm: ", fmt.Sprintf("%s", payload)) + + if r.rmrCtx == nil || !r.rmrReady { + url := fmt.Sprintf("%s/%s", r.managerUrl, "ric/v1/alarms") + resp, err := http.Post(url, "application/json", bytes.NewReader(payload)) + if err != nil || resp == nil { + return fmt.Errorf("Unable to send alarm: %v", err) + } + log.Printf("Alarm posted to %s [status=%d]", url, resp.StatusCode) + return nil + } datap := C.CBytes(payload) defer C.free(datap) @@ -136,6 +165,7 @@ func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error { log.Println("rmrSend failed with error: ", state) return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state)) } + return nil } @@ -149,3 +179,37 @@ func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error { } return errors.New("rmrRcv failed!") } + +func InitRMR(r *RICAlarm, endpoint string) error { + // Setup static RT for alarm system + if endpoint == "" { + if r.moId == "my-pod" { + endpoint = "127.0.0.1:4560" + } else if r.moId == "my-pod-lib" { + endpoint = "127.0.0.1:4588" + } + } + + alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", endpoint) + alarmRTFile := "/tmp/alarm.rt" + + if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil { + log.Println("ioutil.WriteFile failed with error: ", err) + return err + } + + os.Setenv("RMR_SEED_RT", alarmRTFile) + os.Setenv("RMR_RTG_SVC", "-1") + + if ctx := C.rmrInit(); ctx != nil { + r.rmrCtx = ctx + r.rmrReady = true + return nil + } + + return errors.New("rmrInit failed!") +} + +func (r *RICAlarm) IsRMRReady() bool { + return r.rmrReady +}