Non-blocking RMR init ...
[ric-plt/alarm-go.git] / alarm / alarm.go
index 2246abe..cb18dd7 100755 (executable)
@@ -25,83 +25,29 @@ import (
        "errors"
        "fmt"
        "log"
-       "sync"
        "time"
        "unsafe"
 )
 
 /*
 #cgo CFLAGS: -I../
-#cgo LDFLAGS: -lrmr_nng -lnng
+#cgo LDFLAGS: -lrmr_si
 
 #include "utils.h"
 */
 import "C"
 
-// Severity for alarms
-type Severity string
-
-// Possible values for Severity
-const (
-       SeverityUnspecified Severity = "UNSPECIFIED"
-       SeverityCritical    Severity = "CRITICAL"
-       SeverityMajor       Severity = "MAJOR"
-       SeverityMinor       Severity = "MINOR"
-       SeverityWarning     Severity = "WARNING"
-       SeverityNormal      Severity = "CLEARED"
-       SeverityDefault     Severity = "DEFAULT"
-)
-
-// Alarm object - see README for more information
-type Alarm struct {
-       ManagedObjectId   string   `json:"managedObjectId"`
-       ApplicationId     string   `json:"applicationId"`
-       SpecificProblem   int      `json:"specificProblem"`
-       PerceivedSeverity Severity `json:"perceivedSeverity"`
-       AdditionalInfo    string   `json:"additionalInfo"`
-       IdentifyingInfo   string   `json:"identifyingInfo"`
-}
-
-// Alarm actions
-type AlarmAction string
-
-// Possible values for alarm actions
-const (
-       AlarmActionRaise    AlarmAction = "RAISE"
-       AlarmActionClear    AlarmAction = "CLEAR"
-       AlarmActionReraise  AlarmAction = "RERAISE"
-       AlarmActionClearAll AlarmAction = "CLEARALL"
-)
-
-type AlarmMessage struct {
-       Alarm
-       AlarmAction
-       AlarmTime int64
-}
-
-// RICAlarm is an alarm instance
-type RICAlarm struct {
-       moId   string
-       appId  string
-       rmrCtx unsafe.Pointer
-       mutex  sync.Mutex
-}
-
 // InitAlarm is the init routine which returns a new alarm instance.
 // 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,
-               }
-
-               return r, nil
+       r := &RICAlarm{
+               moId:  mo,
+               appId: id,
        }
+       go InitRMR(r)
 
-       return nil, errors.New("rmrInit failed!")
+       return r, nil
 }
 
 // Create a new Alarm instance
@@ -122,13 +68,21 @@ func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessag
        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()
        defer r.mutex.Unlock()
 
        m := r.NewAlarmMessage(a, AlarmActionRaise)
-       return r.SendMessage(m)
+       return r.sendAlarmUpdateReq(m)
 }
 
 // Clear a RIC alarm
@@ -137,7 +91,7 @@ func (r *RICAlarm) Clear(a Alarm) error {
        defer r.mutex.Unlock()
 
        m := r.NewAlarmMessage(a, AlarmActionClear)
-       return r.SendMessage(m)
+       return r.sendAlarmUpdateReq(m)
 }
 
 // Re-raise a RIC alarm
@@ -145,8 +99,12 @@ func (r *RICAlarm) Reraise(a Alarm) error {
        r.mutex.Lock()
        defer r.mutex.Unlock()
 
-       m := r.NewAlarmMessage(a, AlarmActionReraise)
-       return r.SendMessage(m)
+       m := r.NewAlarmMessage(a, AlarmActionClear)
+       if err := r.sendAlarmUpdateReq(m); err != nil {
+               return errors.New(fmt.Sprintf("Reraise failed: %v", err))
+       }
+
+       return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
 }
 
 // Clear all alarms raised by the application
@@ -157,18 +115,20 @@ func (r *RICAlarm) ClearAll() error {
        a := r.NewAlarm(0, SeverityDefault, "", "")
        m := r.NewAlarmMessage(a, AlarmActionClearAll)
 
-       return r.SendMessage(m)
+       return r.sendAlarmUpdateReq(m)
 }
 
-// Internal functions
 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
        s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
        return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
 }
 
-func (r *RICAlarm) SendMessage(a AlarmMessage) error {
-       log.Println("Sending alarm:", r.AlarmString(a))
+func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
+       if r.rmrCtx == nil || !r.rmrReady {
+               return errors.New("RMR no ready yet!")
+       }
 
+       log.Println("Sending alarm: ", r.AlarmString(a))
        payload, err := json.Marshal(a)
        if err != nil {
                return err
@@ -179,7 +139,8 @@ func (r *RICAlarm) SendMessage(a AlarmMessage) error {
        meid := C.CString("ric")
        defer C.free(unsafe.Pointer(meid))
 
-       if state := C.rmrSend(r.rmrCtx, 1234, datap, C.int(len(payload)), meid); state != C.RMR_OK {
+       if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
+               log.Println("rmrSend failed with error: ", state)
                return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
        }
        return nil
@@ -195,3 +156,13 @@ func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
        }
        return errors.New("rmrRcv failed!")
 }
+
+func InitRMR(r *RICAlarm) error {
+       if ctx := C.rmrInit(); ctx != nil {
+               r.rmrCtx = ctx
+               r.rmrReady = true
+               return nil
+       }
+
+       return errors.New("rmrInit failed!")
+}