LN0739_FM_FR8: relaxing the active alarm and alarm history restrictions
[ric-plt/alarm-go.git] / alarm / alarm.go
index dddccb1..d097c73 100755 (executable)
 package alarm
 
 import (
+       "bytes"
        "encoding/json"
        "errors"
        "fmt"
+       "io/ioutil"
        "log"
+       "net/http"
+       "os"
        "time"
        "unsafe"
-       "os"
-       "io/ioutil"
 )
 
 /*
@@ -44,10 +46,18 @@ import "C"
 // The identities are used when raising/clearing alarms, unless provided by the applications.
 func InitAlarm(mo, id string) (*RICAlarm, error) {
        r := &RICAlarm{
-               moId:  mo,
-               appId: id,
+               moId:       mo,
+               appId:      id,
+               managerUrl: ALARM_MANAGER_HTTP_URL,
+       }
+
+       if os.Getenv("ALARM_MANAGER_URL") != "" {
+               r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
+       }
+
+       if os.Getenv("ALARM_IF_RMR") != "" {
+               go InitRMR(r)
        }
-       go InitRMR(r)
 
        return r, nil
 }
@@ -66,7 +76,7 @@ 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}
 }
 
@@ -126,15 +136,22 @@ func (r *RICAlarm) AlarmString(a AlarmMessage) string {
 }
 
 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 {
+               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)
@@ -161,11 +178,11 @@ func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
 
 func InitRMR(r *RICAlarm) error {
        // Setup static RT for alarm system
-       endpoint := "service-ricplt-alarmadapter-rmr.ricplt:4560"
+       endpoint := ALARM_MANAGER_RMR_URL
        if r.moId == "my-pod" {
-               endpoint = "localhost:4560"
+               endpoint = "127.0.0.1:4560"
        } else if r.moId == "my-pod-lib" {
-               endpoint = "localhost:4588"
+               endpoint = "127.0.0.1:4588"
        }
 
        alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", endpoint)