LN0739_FM_FR8: relaxing the active alarm and alarm history restrictions
[ric-plt/alarm-go.git] / cli / alarm-cli.go
1 package main
2
3 import (
4         "bytes"
5         "encoding/json"
6         "fmt"
7         "github.com/jedib0t/go-pretty/table"
8         "github.com/thatisuday/commando"
9         "io/ioutil"
10         "net/http"
11         "os"
12         "time"
13
14         "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
15 )
16
17 func main() {
18
19         // configure commando
20         commando.
21                 SetExecutableName("alarm-cli").
22                 SetVersion("1.0.0").
23                 SetDescription("This CLI tool provides management interface to SEP alarm system")
24
25         // Get active alarms
26         commando.
27                 Register("active").
28                 SetShortDescription("Displays the SEP active alarms").
29                 SetDescription("This command displays more information about the SEP active alarms").
30                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
31                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
32                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
33                         displayAlarms(getAlarms(flags, "active"), false)
34                 })
35
36         // Get alarm history
37         commando.
38                 Register("history").
39                 SetShortDescription("Displays the SEP alarm history").
40                 SetDescription("This command displays more information about the SEP alarm history").
41                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
42                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
43                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
44                         displayAlarms(getAlarms(flags, "history"), true)
45                 })
46
47         // Raise an alarm
48         commando.
49                 Register("raise").
50                 SetShortDescription("Raises alarm with given parameters").
51                 AddFlag("moid", "Managed object Id", commando.String, nil).
52                 AddFlag("apid", "Application Id", commando.String, nil).
53                 AddFlag("sp", "Specific problem Id", commando.Int, nil).
54                 AddFlag("severity", "Perceived severity", commando.String, nil).
55                 AddFlag("iinfo", "Application identifying info", commando.String, nil).
56                 AddFlag("aai", "Application additional info", commando.String, "-").
57                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
58                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
59                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
60                         postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise)
61                 })
62
63         // Clear an alarm
64         commando.
65                 Register("clear").
66                 SetShortDescription("Raises alarm with given parameters").
67                 AddFlag("moid", "Managed object Id", commando.String, nil).
68                 AddFlag("apid", "Application Id", commando.String, nil).
69                 AddFlag("sp", "Specific problem Id", commando.Int, nil).
70                 AddFlag("iinfo", "Application identifying info", commando.String, nil).
71                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
72                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
73                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
74                         postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear)
75                 })
76
77         // Configure an alarm manager
78         commando.
79                 Register("configure").
80                 SetShortDescription("Configure alarm manager with given parameters").
81                 AddFlag("mal", "max active alarms", commando.Int, nil).
82                 AddFlag("mah", "max alarm history", commando.Int, nil).
83                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
84                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
85                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
86                         postAlarmConfig(flags)
87                 })
88
89         // parse command-line arguments
90         commando.Parse(nil)
91 }
92
93 func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.Alarm) {
94         a.ManagedObjectId, _ = flags["moid"].GetString()
95         a.ApplicationId, _ = flags["apid"].GetString()
96         a.SpecificProblem, _ = flags["sp"].GetInt()
97         a.IdentifyingInfo, _ = flags["iinfo"].GetString()
98
99         if !clear {
100                 s, _ := flags["severity"].GetString()
101                 a.PerceivedSeverity = alarm.Severity(s)
102         }
103
104         if !clear {
105                 a.AdditionalInfo, _ = flags["aai"].GetString()
106         }
107
108         return
109 }
110
111 func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (alarms []alarm.AlarmMessage) {
112         host, _ := flags["host"].GetString()
113         port, _ := flags["port"].GetString()
114         targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/%s", host, port, action)
115         resp, err := http.Get(targetUrl)
116         if err != nil || resp == nil || resp.Body == nil {
117                 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
118                 return alarms
119         }
120
121         defer resp.Body.Close()
122
123         body, err := ioutil.ReadAll(resp.Body)
124         if err != nil {
125                 fmt.Println("ioutil.ReadAll failed: %v", err)
126                 return alarms
127         }
128
129         json.Unmarshal([]byte(body), &alarms)
130         return alarms
131 }
132
133 func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm.AlarmAction) {
134         host, _ := flags["host"].GetString()
135         port, _ := flags["port"].GetString()
136         targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port)
137
138         m := alarm.AlarmMessage{Alarm: a, AlarmAction: action}
139         jsonData, err := json.Marshal(m)
140         if err != nil {
141                 fmt.Println("json.Marshal failed: %v", err)
142                 return
143         }
144
145         resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
146         if err != nil || resp == nil {
147                 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
148                 return
149         }
150 }
151
152 func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) {
153         t := table.NewWriter()
154         t.SetOutputMirror(os.Stdout)
155         if isHistory {
156                 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"})
157         } else {
158                 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"})
159         }
160
161         for _, a := range alarms {
162                 alarmTime := time.Unix(0, a.AlarmTime).Format("02/01/2006, 15:04:05")
163                 if isHistory {
164                         t.AppendRows([]table.Row{
165                                 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime},
166                         })
167                 } else {
168                         t.AppendRows([]table.Row{
169                                 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime},
170                         })
171                 }
172         }
173
174         t.SetStyle(table.StyleColoredBright)
175         t.Render()
176 }
177
178 func postAlarmConfig(flags map[string]commando.FlagValue) {
179         host, _ := flags["host"].GetString()
180         port, _ := flags["port"].GetString()
181         maxactivealarms, _ := flags["mal"].GetInt()
182         maxalarmhistory, _ := flags["mah"].GetInt()
183         targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/config", host, port)
184
185         m := alarm.AlarmConfigParams{MaxActiveAlarms: maxactivealarms, MaxAlarmHistory: maxalarmhistory}
186         jsonData, err := json.Marshal(m)
187         if err != nil {
188                 fmt.Println("json.Marshal failed: %v", err)
189                 return
190         }
191
192         resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
193         if err != nil || resp == nil {
194                 fmt.Println("Couldn't fetch post alarm configuration due to error: %v", err)
195                 return
196         }
197 }