LN0739_FM_FR7: support for setting and clearing alarm via RMR 90/4790/1
authorAnssi Mannila <anssi.mannila@nokia.com>
Wed, 30 Sep 2020 05:57:04 +0000 (08:57 +0300)
committerAnssi Mannila <anssi.mannila@nokia.com>
Wed, 30 Sep 2020 05:57:52 +0000 (08:57 +0300)
Signed-off-by: Anssi Mannila <anssi.mannila@nokia.com>
Change-Id: I11c5b7bf599c49f4ceaaf86cd6fbbf4ce0d885df

alarm/alarm.go
cli/alarm-cli.go
go.mod
manager/cmd/manager_test.go

index d097c73..5dfe896 100755 (executable)
@@ -55,10 +55,9 @@ func InitAlarm(mo, id string) (*RICAlarm, error) {
                r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
        }
 
                r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
        }
 
-       if os.Getenv("ALARM_IF_RMR") != "" {
+       if os.Getenv("ALARM_IF_RMR") == "" {
                go InitRMR(r)
        }
                go InitRMR(r)
        }
-
        return r, nil
 }
 
        return r, nil
 }
 
@@ -136,6 +135,7 @@ func (r *RICAlarm) AlarmString(a AlarmMessage) string {
 }
 
 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
 }
 
 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
+
        payload, err := json.Marshal(a)
        if err != nil {
                log.Println("json.Marshal failed with error: ", err)
        payload, err := json.Marshal(a)
        if err != nil {
                log.Println("json.Marshal failed with error: ", err)
@@ -162,6 +162,7 @@ func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
                log.Println("rmrSend failed with error: ", state)
                return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
        }
                log.Println("rmrSend failed with error: ", state)
                return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
        }
+
        return nil
 }
 
        return nil
 }
 
@@ -204,3 +205,7 @@ func InitRMR(r *RICAlarm) error {
 
        return errors.New("rmrInit failed!")
 }
 
        return errors.New("rmrInit failed!")
 }
+
+func (r *RICAlarm) IsRMRReady() bool {
+       return r.rmrReady
+}
index a3ea90c..0a1312a 100755 (executable)
@@ -4,20 +4,25 @@ import (
        "bytes"
        "encoding/json"
        "fmt"
        "bytes"
        "encoding/json"
        "fmt"
-       "github.com/jedib0t/go-pretty/table"
-       "github.com/thatisuday/commando"
        "io/ioutil"
        "net/http"
        "os"
        "io/ioutil"
        "net/http"
        "os"
+       "strconv"
        "time"
        "time"
-        "strconv"
+
        "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
        "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
+       "github.com/jedib0t/go-pretty/table"
+       "github.com/thatisuday/commando"
 )
 
 type CliAlarmDefinitions struct {
        AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
 }
 
 )
 
 type CliAlarmDefinitions struct {
        AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
 }
 
+type AlarmClient struct {
+       alarmer *alarm.RICAlarm
+}
+
 func main() {
 
        // configure commando
 func main() {
 
        // configure commando
@@ -60,6 +65,7 @@ func main() {
                AddFlag("aai", "Application additional info", commando.String, "-").
                AddFlag("host", "Alarm manager host address", commando.String, "localhost").
                AddFlag("port", "Alarm manager host address", commando.String, "8080").
                AddFlag("aai", "Application additional info", commando.String, "-").
                AddFlag("host", "Alarm manager host address", commando.String, "localhost").
                AddFlag("port", "Alarm manager host address", commando.String, "8080").
+               AddFlag("if", "http or rmr used as interface", commando.String, "http").
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise)
                })
@@ -74,6 +80,7 @@ func main() {
                AddFlag("iinfo", "Application identifying info", commando.String, nil).
                AddFlag("host", "Alarm manager host address", commando.String, "localhost").
                AddFlag("port", "Alarm manager host address", commando.String, "8080").
                AddFlag("iinfo", "Application identifying info", commando.String, nil).
                AddFlag("host", "Alarm manager host address", commando.String, "localhost").
                AddFlag("port", "Alarm manager host address", commando.String, "8080").
+               AddFlag("if", "http or rmr used as interface", commando.String, "http").
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear)
                })
@@ -89,7 +96,7 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarmConfig(flags)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarmConfig(flags)
                })
-       // Create alarm defenition
+       // Create alarm definition
        commando.
                Register("define").
                SetShortDescription("Define alarm with given parameters").
        commando.
                Register("define").
                SetShortDescription("Define alarm with given parameters").
@@ -102,7 +109,7 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarmDefinition(flags)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarmDefinition(flags)
                })
-               // Delete alarm defenition
+               // Delete alarm definition
        commando.
                Register("undefine").
                SetShortDescription("Define alarm with given parameters").
        commando.
                Register("undefine").
                SetShortDescription("Define alarm with given parameters").
@@ -131,7 +138,6 @@ func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.A
        if !clear {
                a.AdditionalInfo, _ = flags["aai"].GetString()
        }
        if !clear {
                a.AdditionalInfo, _ = flags["aai"].GetString()
        }
-
        return
 }
 
        return
 }
 
@@ -158,6 +164,30 @@ func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (a
 }
 
 func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm.AlarmAction) {
 }
 
 func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm.AlarmAction) {
+
+       // Check the interface to be used for raise or clear the alarm
+       rmr_or_http, _ := flags["if"].GetString()
+       if rmr_or_http == "rmr" {
+               alarmClient := NewAlarmClient("my-pod", "my-app")
+               if alarmClient == nil {
+                       return
+               }
+
+               // Wait until RMR is up-and-running
+               for !alarmClient.alarmer.IsRMRReady() {
+                       time.Sleep(100 * time.Millisecond)
+               }
+
+               if action == alarm.AlarmActionRaise {
+                       alarmClient.alarmer.Raise(a)
+               }
+
+               if action == alarm.AlarmActionClear {
+                       alarmClient.alarmer.Clear(a)
+               }
+               return
+       }
+
        host, _ := flags["host"].GetString()
        port, _ := flags["port"].GetString()
        targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port)
        host, _ := flags["host"].GetString()
        port, _ := flags["port"].GetString()
        targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port)
@@ -271,3 +301,15 @@ func deleteAlarmDefinition(flags map[string]commando.FlagValue) {
                return
        }
 }
                return
        }
 }
+
+// NewAlarmClient returns a new AlarmClient.
+func NewAlarmClient(moId, appId string) *AlarmClient {
+       alarmInstance, err := alarm.InitAlarm(moId, appId)
+       if err == nil {
+               return &AlarmClient{
+                       alarmer: alarmInstance,
+               }
+       }
+       fmt.Println("Failed to create alarmInstance", err)
+       return nil
+}
diff --git a/go.mod b/go.mod
index 9b21fbb..b6ae937 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -12,15 +12,14 @@ replace gerrit.o-ran-sc.org/r/com/golog => gerrit.o-ran-sc.org/r/com/golog.git v
 
 require (
        gerrit.o-ran-sc.org/r/com/golog v0.0.1
 
 require (
        gerrit.o-ran-sc.org/r/com/golog v0.0.1
+       gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm v0.4.2 // indirect
        gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm v0.0.0-00010101000000-000000000000
        gerrit.o-ran-sc.org/r/ric-plt/xapp-frame v0.0.0-00010101000000-000000000000
        github.com/go-openapi/runtime v0.19.11
        github.com/go-openapi/strfmt v0.19.4
        github.com/gorilla/mux v1.7.1
        gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm v0.0.0-00010101000000-000000000000
        gerrit.o-ran-sc.org/r/ric-plt/xapp-frame v0.0.0-00010101000000-000000000000
        github.com/go-openapi/runtime v0.19.11
        github.com/go-openapi/strfmt v0.19.4
        github.com/gorilla/mux v1.7.1
-       github.com/jedib0t/go-pretty v4.3.0+incompatible // indirect
+       github.com/jedib0t/go-pretty v4.3.0+incompatible
        github.com/mattn/go-runewidth v0.0.9 // indirect
        github.com/mattn/go-runewidth v0.0.9 // indirect
-       github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
-       github.com/modern-go/reflect2 v1.0.1 // indirect
        github.com/prometheus/alertmanager v0.20.0
        github.com/spf13/viper v1.6.2
        github.com/stretchr/testify v1.5.1
        github.com/prometheus/alertmanager v0.20.0
        github.com/spf13/viper v1.6.2
        github.com/stretchr/testify v1.5.1
index 883b112..f4a5aee 100755 (executable)
@@ -47,7 +47,6 @@ var eventChan chan string
 
 // Test cases
 func TestMain(M *testing.M) {
 
 // Test cases
 func TestMain(M *testing.M) {
-       os.Setenv("ALARM_IF_RMR", "true")
        alarmManager = NewAlarmManager("localhost:9093", 500)
        go alarmManager.Run(false)
        time.Sleep(time.Duration(10) * time.Second)
        alarmManager = NewAlarmManager("localhost:9093", 500)
        go alarmManager.Run(false)
        time.Sleep(time.Duration(10) * time.Second)