Static RT for alarm
[ric-plt/alarm-go.git] / alarm / alarm.go
index 3291621..dddccb1 100755 (executable)
@@ -27,11 +27,13 @@ import (
        "log"
        "time"
        "unsafe"
+       "os"
+       "io/ioutil"
 )
 
 /*
 #cgo CFLAGS: -I../
-#cgo LDFLAGS: -lrmr_nng -lnng
+#cgo LDFLAGS: -lrmr_si
 
 #include "utils.h"
 */
@@ -41,17 +43,13 @@ 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,
-               }
-
-               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
@@ -128,8 +126,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
@@ -157,3 +158,32 @@ func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
        }
        return errors.New("rmrRcv failed!")
 }
+
+func InitRMR(r *RICAlarm) error {
+       // Setup static RT for alarm system
+       endpoint := "service-ricplt-alarmadapter-rmr.ricplt:4560"
+       if r.moId == "my-pod" {
+               endpoint = "localhost:4560"
+       } else if r.moId == "my-pod-lib" {
+               endpoint = "localhost: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!")
+}