Merge "LN0739_FM_FR12: support for options to dynamically create the AlarmDefinitions...
[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 type CliAlarmDefinitions struct {
18         AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
19 }
20
21 func main() {
22
23         // configure commando
24         commando.
25                 SetExecutableName("alarm-cli").
26                 SetVersion("1.0.0").
27                 SetDescription("This CLI tool provides management interface to SEP alarm system")
28
29         // Get active alarms
30         commando.
31                 Register("active").
32                 SetShortDescription("Displays the SEP active alarms").
33                 SetDescription("This command displays more information about the SEP active alarms").
34                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
35                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
36                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
37                         displayAlarms(getAlarms(flags, "active"), false)
38                 })
39
40         // Get alarm history
41         commando.
42                 Register("history").
43                 SetShortDescription("Displays the SEP alarm history").
44                 SetDescription("This command displays more information about the SEP alarm history").
45                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
46                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
47                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
48                         displayAlarms(getAlarms(flags, "history"), true)
49                 })
50
51         // Raise an alarm
52         commando.
53                 Register("raise").
54                 SetShortDescription("Raises alarm with given parameters").
55                 AddFlag("moid", "Managed object Id", commando.String, nil).
56                 AddFlag("apid", "Application Id", commando.String, nil).
57                 AddFlag("sp", "Specific problem Id", commando.Int, nil).
58                 AddFlag("severity", "Perceived severity", commando.String, nil).
59                 AddFlag("iinfo", "Application identifying info", commando.String, nil).
60                 AddFlag("aai", "Application additional info", commando.String, "-").
61                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
62                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
63                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
64                         postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise)
65                 })
66
67         // Clear an alarm
68         commando.
69                 Register("clear").
70                 SetShortDescription("Raises alarm with given parameters").
71                 AddFlag("moid", "Managed object Id", commando.String, nil).
72                 AddFlag("apid", "Application Id", commando.String, nil).
73                 AddFlag("sp", "Specific problem Id", commando.Int, nil).
74                 AddFlag("iinfo", "Application identifying info", commando.String, nil).
75                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
76                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
77                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
78                         postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear)
79                 })
80
81         // Configure an alarm manager
82         commando.
83                 Register("configure").
84                 SetShortDescription("Configure alarm manager with given parameters").
85                 AddFlag("mal", "max active alarms", commando.Int, nil).
86                 AddFlag("mah", "max alarm history", commando.Int, nil).
87                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
88                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
89                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
90                         postAlarmConfig(flags)
91                 })
92         // Create alarm defenition
93         commando.
94                 Register("define").
95                 SetShortDescription("Define alarm with given parameters").
96                 AddFlag("aid", "alarm identifier", commando.Int, nil).
97                 AddFlag("atx", "alarm text", commando.String, nil).
98                 AddFlag("ety", "event type", commando.String, nil).
99                 AddFlag("oin", "operation instructions", commando.String, nil).
100                 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
101                 AddFlag("port", "Alarm manager host address", commando.String, "8080").
102                 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
103                         postAlarmDefinition(flags)
104                 })
105
106         // parse command-line arguments
107         commando.Parse(nil)
108 }
109
110 func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.Alarm) {
111         a.ManagedObjectId, _ = flags["moid"].GetString()
112         a.ApplicationId, _ = flags["apid"].GetString()
113         a.SpecificProblem, _ = flags["sp"].GetInt()
114         a.IdentifyingInfo, _ = flags["iinfo"].GetString()
115
116         if !clear {
117                 s, _ := flags["severity"].GetString()
118                 a.PerceivedSeverity = alarm.Severity(s)
119         }
120
121         if !clear {
122                 a.AdditionalInfo, _ = flags["aai"].GetString()
123         }
124
125         return
126 }
127
128 func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (alarms []alarm.AlarmMessage) {
129         host, _ := flags["host"].GetString()
130         port, _ := flags["port"].GetString()
131         targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/%s", host, port, action)
132         resp, err := http.Get(targetUrl)
133         if err != nil || resp == nil || resp.Body == nil {
134                 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
135                 return alarms
136         }
137
138         defer resp.Body.Close()
139
140         body, err := ioutil.ReadAll(resp.Body)
141         if err != nil {
142                 fmt.Println("ioutil.ReadAll failed: %v", err)
143                 return alarms
144         }
145
146         json.Unmarshal([]byte(body), &alarms)
147         return alarms
148 }
149
150 func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm.AlarmAction) {
151         host, _ := flags["host"].GetString()
152         port, _ := flags["port"].GetString()
153         targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port)
154
155         m := alarm.AlarmMessage{Alarm: a, AlarmAction: action}
156         jsonData, err := json.Marshal(m)
157         if err != nil {
158                 fmt.Println("json.Marshal failed: %v", err)
159                 return
160         }
161
162         resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
163         if err != nil || resp == nil {
164                 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
165                 return
166         }
167 }
168
169 func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) {
170         t := table.NewWriter()
171         t.SetOutputMirror(os.Stdout)
172         if isHistory {
173                 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"})
174         } else {
175                 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"})
176         }
177
178         for _, a := range alarms {
179                 alarmTime := time.Unix(0, a.AlarmTime).Format("02/01/2006, 15:04:05")
180                 if isHistory {
181                         t.AppendRows([]table.Row{
182                                 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime},
183                         })
184                 } else {
185                         t.AppendRows([]table.Row{
186                                 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime},
187                         })
188                 }
189         }
190
191         t.SetStyle(table.StyleColoredBright)
192         t.Render()
193 }
194
195 func postAlarmConfig(flags map[string]commando.FlagValue) {
196         host, _ := flags["host"].GetString()
197         port, _ := flags["port"].GetString()
198         maxactivealarms, _ := flags["mal"].GetInt()
199         maxalarmhistory, _ := flags["mah"].GetInt()
200         targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/config", host, port)
201
202         m := alarm.AlarmConfigParams{MaxActiveAlarms: maxactivealarms, MaxAlarmHistory: maxalarmhistory}
203         jsonData, err := json.Marshal(m)
204         if err != nil {
205                 fmt.Println("json.Marshal failed: %v", err)
206                 return
207         }
208
209         resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
210         if err != nil || resp == nil {
211                 fmt.Println("Couldn't fetch post alarm configuration due to error: %v", err)
212                 return
213         }
214 }
215
216 func postAlarmDefinition(flags map[string]commando.FlagValue) {
217         host, _ := flags["host"].GetString()
218         port, _ := flags["port"].GetString()
219         alarmid, _ := flags["aid"].GetInt()
220         alarmtxt, _ := flags["atx"].GetString()
221         etype, _ := flags["ety"].GetString()
222         operation, _ := flags["oin"].GetString()
223         targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define", host, port)
224
225         var alarmdefinition alarm.AlarmDefinition
226         alarmdefinition.AlarmId = alarmid
227         alarmdefinition.AlarmText = alarmtxt
228         alarmdefinition.EventType = etype
229         alarmdefinition.OperationInstructions = operation
230
231         m := CliAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarmdefinition}}
232         jsonData, err := json.Marshal(m)
233         if err != nil {
234                 fmt.Println("json.Marshal failed: %v", err)
235                 return
236         }
237
238         resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
239         if err != nil || resp == nil {
240                 fmt.Println("Couldn't fetch post alarm configuration due to error: %v", err)
241                 return
242         }
243 }