Non-blocking RMR init ...
[ric-plt/alarm-go.git] / alarm / alarm.go
index 664a7c2..cb18dd7 100755 (executable)
@@ -25,87 +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"
-       SeverityCleared     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"
-       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
-}
-
-const (
-       RIC_ALARM_UPDATE = 13111
-       RIC_ALARM_QUERY  = 13112
-)
-
 // 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
@@ -126,6 +68,14 @@ 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()
@@ -174,8 +124,11 @@ func (r *RICAlarm) AlarmString(a AlarmMessage) string {
 }
 
 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
-       log.Println("Sending alarm: ", r.AlarmString(a))
+       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
@@ -203,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!")
+}