Add UT for REST interface & code refactor
[ric-plt/alarm-go.git] / alarm / alarm.go
index 2246abe..e0bd93b 100755 (executable)
@@ -25,7 +25,6 @@ import (
        "errors"
        "fmt"
        "log"
-       "sync"
        "time"
        "unsafe"
 )
@@ -38,55 +37,6 @@ import (
 */
 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.
@@ -128,7 +78,7 @@ func (r *RICAlarm) Raise(a Alarm) error {
        defer r.mutex.Unlock()
 
        m := r.NewAlarmMessage(a, AlarmActionRaise)
-       return r.SendMessage(m)
+       return r.sendAlarmUpdateReq(m)
 }
 
 // Clear a RIC alarm
@@ -137,7 +87,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 +95,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,17 +111,16 @@ 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 {
+       log.Println("Sending alarm: ", r.AlarmString(a))
 
        payload, err := json.Marshal(a)
        if err != nil {
@@ -179,7 +132,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