X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=cli%2Falarm-cli.go;h=a3ea90c3ce11763180d07d874cde39cecd31cf9a;hb=4cedd5020e5c7ba0383156f9929ab3db986c0593;hp=212f8ca06bbc9d9636418c9ad82ccbf94f8a5332;hpb=0c38973604b5c63a91121d3641dd6f306b580acb;p=ric-plt%2Falarm-go.git diff --git a/cli/alarm-cli.go b/cli/alarm-cli.go index 212f8ca..a3ea90c 100755 --- a/cli/alarm-cli.go +++ b/cli/alarm-cli.go @@ -10,10 +10,14 @@ import ( "net/http" "os" "time" - + "strconv" "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm" ) +type CliAlarmDefinitions struct { + AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"` +} + func main() { // configure commando @@ -74,6 +78,41 @@ func main() { postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear) }) + // Configure an alarm manager + commando. + Register("configure"). + SetShortDescription("Configure alarm manager with given parameters"). + AddFlag("mal", "max active alarms", commando.Int, nil). + AddFlag("mah", "max alarm history", commando.Int, nil). + AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("port", "Alarm manager host address", commando.String, "8080"). + SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { + postAlarmConfig(flags) + }) + // Create alarm defenition + commando. + Register("define"). + SetShortDescription("Define alarm with given parameters"). + AddFlag("aid", "alarm identifier", commando.Int, nil). + AddFlag("atx", "alarm text", commando.String, nil). + AddFlag("ety", "event type", commando.String, nil). + AddFlag("oin", "operation instructions", commando.String, nil). + AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("port", "Alarm manager host address", commando.String, "8080"). + SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { + postAlarmDefinition(flags) + }) + // Delete alarm defenition + commando. + Register("undefine"). + SetShortDescription("Define alarm with given parameters"). + AddFlag("aid", "alarm identifier", commando.Int, nil). + AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("port", "Alarm manager host address", commando.String, "8080"). + SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { + deleteAlarmDefinition(flags) + }) + // parse command-line arguments commando.Parse(nil) } @@ -115,7 +154,6 @@ func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (a } json.Unmarshal([]byte(body), &alarms) - fmt.Println(alarms) return alarms } @@ -142,7 +180,7 @@ func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) { t := table.NewWriter() t.SetOutputMirror(os.Stdout) if isHistory { - t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME", "ACTION"}) + t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"}) } else { t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"}) } @@ -151,7 +189,7 @@ func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) { alarmTime := time.Unix(0, a.AlarmTime).Format("02/01/2006, 15:04:05") if isHistory { t.AppendRows([]table.Row{ - {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime, a.AlarmAction}, + {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime}, }) } else { t.AppendRows([]table.Row{ @@ -163,3 +201,73 @@ func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) { t.SetStyle(table.StyleColoredBright) t.Render() } + +func postAlarmConfig(flags map[string]commando.FlagValue) { + host, _ := flags["host"].GetString() + port, _ := flags["port"].GetString() + maxactivealarms, _ := flags["mal"].GetInt() + maxalarmhistory, _ := flags["mah"].GetInt() + targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/config", host, port) + + m := alarm.AlarmConfigParams{MaxActiveAlarms: maxactivealarms, MaxAlarmHistory: maxalarmhistory} + jsonData, err := json.Marshal(m) + if err != nil { + fmt.Println("json.Marshal failed: %v", err) + return + } + + resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData)) + if err != nil || resp == nil { + fmt.Println("Couldn't fetch post alarm configuration due to error: %v", err) + return + } +} + +func postAlarmDefinition(flags map[string]commando.FlagValue) { + host, _ := flags["host"].GetString() + port, _ := flags["port"].GetString() + alarmid, _ := flags["aid"].GetInt() + alarmtxt, _ := flags["atx"].GetString() + etype, _ := flags["ety"].GetString() + operation, _ := flags["oin"].GetString() + targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define", host, port) + + var alarmdefinition alarm.AlarmDefinition + alarmdefinition.AlarmId = alarmid + alarmdefinition.AlarmText = alarmtxt + alarmdefinition.EventType = etype + alarmdefinition.OperationInstructions = operation + + m := CliAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarmdefinition}} + jsonData, err := json.Marshal(m) + if err != nil { + fmt.Println("json.Marshal failed: %v", err) + return + } + + resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData)) + if err != nil || resp == nil { + fmt.Println("Couldn't post alarm definition due to error: %v", err) + return + } +} + +func deleteAlarmDefinition(flags map[string]commando.FlagValue) { + host, _ := flags["host"].GetString() + port, _ := flags["port"].GetString() + alarmid, _ := flags["aid"].GetInt() + salarmid := strconv.FormatUint(uint64(alarmid), 10) + targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define/%s", host, port, salarmid) + + client := &http.Client{} + req, err := http.NewRequest("DELETE", targetUrl, nil) + if err != nil || req == nil { + fmt.Println("Couldn't make delete request due to error: %v", err) + return + } + resp, errr := client.Do(req) + if errr != nil || resp == nil { + fmt.Println("Couldn't send delete request due to error: %v", err) + return + } +}