Initial alarm adapter implementation
[ric-plt/alarm-go.git] / adapter / cmd / adapter.go
1 /*
2  *  Copyright (c) 2020 AT&T Intellectual Property.
3  *  Copyright (c) 2020 Nokia.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18  * platform project (RICP).
19  */
20
21 package main
22
23 import (
24         "encoding/json"
25         "fmt"
26         "time"
27
28         clientruntime "github.com/go-openapi/runtime/client"
29         "github.com/go-openapi/strfmt"
30         "github.com/prometheus/alertmanager/api/v2/client"
31         "github.com/prometheus/alertmanager/api/v2/client/alert"
32         "github.com/prometheus/alertmanager/api/v2/models"
33         "github.com/spf13/viper"
34
35         "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
36         app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
37 )
38
39 type AlarmAdapter struct {
40         amHost        string
41         amBaseUrl     string
42         amSchemes     []string
43         alertInterval int
44         activeAlarms  []alarm.Alarm
45         rmrReady      bool
46 }
47
48 // Temp alarm constants & definitions
49 const (
50         RIC_RT_DISTRIBUTION_FAILED     int = 8004
51         CONNECTIVITY_LOST_TO_DBAAS     int = 8005
52         E2_CONNECTIVITY_LOST_TO_GNODEB int = 8006
53         E2_CONNECTIVITY_LOST_TO_ENODEB int = 8007
54 )
55
56 var alarmDefinitions = map[int]string{
57         RIC_RT_DISTRIBUTION_FAILED:     "RIC ROUTING TABLE DISTRIBUTION FAILED",
58         CONNECTIVITY_LOST_TO_DBAAS:     "CONNECTIVITY LOST TO DBAAS",
59         E2_CONNECTIVITY_LOST_TO_GNODEB: "E2 CONNECTIVITY LOST TO G-NODEB",
60         E2_CONNECTIVITY_LOST_TO_ENODEB: "E2 CONNECTIVITY LOST TO E-NODEB",
61 }
62
63 var Version string
64 var Hash string
65
66 // Main function
67 func main() {
68         NewAlarmAdapter(0).Run(true)
69 }
70
71 func NewAlarmAdapter(alertInterval int) *AlarmAdapter {
72         if alertInterval == 0 {
73                 alertInterval = viper.GetInt("promAlertManager.alertInterval")
74         }
75
76         return &AlarmAdapter{
77                 rmrReady:      false,
78                 amHost:        viper.GetString("promAlertManager.address"),
79                 amBaseUrl:     viper.GetString("promAlertManager.baseUrl"),
80                 amSchemes:     []string{viper.GetString("promAlertManager.schemes")},
81                 alertInterval: alertInterval,
82                 activeAlarms:  make([]alarm.Alarm, 0),
83         }
84 }
85
86 func (a *AlarmAdapter) Run(sdlcheck bool) {
87         app.Logger.SetMdc("alarmAdapter", fmt.Sprintf("%s:%s", Version, Hash))
88         app.SetReadyCB(func(d interface{}) { a.rmrReady = true }, true)
89         app.Resource.InjectStatusCb(a.StatusCB)
90
91         // Start background timer for re-raising alerts
92         go a.StartAlertTimer()
93
94         app.RunWithParams(a, sdlcheck)
95 }
96
97 func (a *AlarmAdapter) StartAlertTimer() {
98         tick := time.Tick(time.Duration(a.alertInterval) * time.Millisecond)
99         for range tick {
100                 for _, m := range a.activeAlarms {
101                         app.Logger.Info("Re-raising alarm: %v", m)
102                         a.PostAlert(a.GenerateAlertLabels(m))
103                 }
104         }
105 }
106
107 func (a *AlarmAdapter) Consume(rp *app.RMRParams) (err error) {
108         app.Logger.Info("Message received!")
109
110         defer app.Rmr.Free(rp.Mbuf)
111         switch rp.Mtype {
112         case alarm.RIC_ALARM_UPDATE:
113                 a.HandleAlarms(rp)
114         default:
115                 app.Logger.Info("Unknown Message Type '%d', discarding", rp.Mtype)
116         }
117
118         return nil
119 }
120
121 func (a *AlarmAdapter) HandleAlarms(rp *app.RMRParams) (*alert.PostAlertsOK, error) {
122         var m alarm.AlarmMessage
123         if err := json.Unmarshal(rp.Payload, &m); err != nil {
124                 app.Logger.Error("json.Unmarshal failed: %v", err)
125                 return nil, err
126         }
127         app.Logger.Info("newAlarm: %v", m)
128
129         if _, ok := alarmDefinitions[m.Alarm.SpecificProblem]; !ok {
130                 app.Logger.Warn("Alarm (SP='%d') not recognized, ignoring ...", m.Alarm.SpecificProblem)
131                 return nil, nil
132         }
133
134         // Suppress duplicate alarms
135         idx, found := a.IsMatchFound(m.Alarm)
136         if found && m.AlarmAction != alarm.AlarmActionClear {
137                 app.Logger.Info("Duplicate alarm ... suppressing!")
138                 return nil, nil
139         }
140
141         // Clear alarm if found from active alarm list
142         if m.AlarmAction == alarm.AlarmActionClear {
143                 if found {
144                         a.activeAlarms = a.RemoveAlarm(a.activeAlarms, idx)
145                         app.Logger.Info("Active alarm cleared!")
146                 } else {
147                         app.Logger.Info("No matching alarm found, ignoring!")
148                 }
149                 return nil, nil
150         }
151
152         // New alarm -> update active alarms and post to Alert Manager
153         if m.AlarmAction == alarm.AlarmActionRaise {
154                 a.UpdateActiveAlarms(m.Alarm)
155                 return a.PostAlert(a.GenerateAlertLabels(m.Alarm))
156         }
157
158         return nil, nil
159 }
160
161 func (a *AlarmAdapter) IsMatchFound(newAlarm alarm.Alarm) (int, bool) {
162         for i, m := range a.activeAlarms {
163                 if m.ManagedObjectId == newAlarm.ManagedObjectId && m.ApplicationId == newAlarm.ApplicationId &&
164                         m.SpecificProblem == newAlarm.SpecificProblem && m.IdentifyingInfo == newAlarm.IdentifyingInfo {
165                         return i, true
166                 }
167         }
168         return -1, false
169 }
170
171 func (a *AlarmAdapter) RemoveAlarm(alarms []alarm.Alarm, i int) []alarm.Alarm {
172         copy(alarms[i:], alarms[i+1:])
173         return alarms[:len(alarms)-1]
174 }
175
176 func (a *AlarmAdapter) UpdateActiveAlarms(newAlarm alarm.Alarm) {
177         // For now just keep the active alarms in-memory. Use SDL later
178         a.activeAlarms = append(a.activeAlarms, newAlarm)
179 }
180
181 func (a *AlarmAdapter) GenerateAlertLabels(newAlarm alarm.Alarm) (models.LabelSet, models.LabelSet) {
182         amLabels := models.LabelSet{
183                 "alertname":   alarmDefinitions[newAlarm.SpecificProblem],
184                 "severity":    string(newAlarm.PerceivedSeverity),
185                 "service":     fmt.Sprintf("%s:%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId),
186                 "system_name": "RIC",
187         }
188         amAnnotations := models.LabelSet{
189                 "description":     newAlarm.IdentifyingInfo,
190                 "additional_info": newAlarm.AdditionalInfo,
191         }
192
193         return amLabels, amAnnotations
194 }
195
196 func (a *AlarmAdapter) NewAlertmanagerClient() *client.Alertmanager {
197         cr := clientruntime.New(a.amHost, a.amBaseUrl, a.amSchemes)
198         return client.New(cr, strfmt.Default)
199 }
200
201 func (a *AlarmAdapter) PostAlert(amLabels, amAnnotations models.LabelSet) (*alert.PostAlertsOK, error) {
202         pa := &models.PostableAlert{
203                 Alert: models.Alert{
204                         GeneratorURL: strfmt.URI(""),
205                         Labels:       amLabels,
206                 },
207                 Annotations: amAnnotations,
208         }
209         alertParams := alert.NewPostAlertsParams().WithAlerts(models.PostableAlerts{pa})
210
211         app.Logger.Info("Posting alerts: labels: %v, annotations: %v", amLabels, amAnnotations)
212         return a.NewAlertmanagerClient().Alert.PostAlerts(alertParams)
213 }
214
215 func (a *AlarmAdapter) StatusCB() bool {
216         if !a.rmrReady {
217                 app.Logger.Info("RMR not ready yet!")
218         }
219
220         return a.rmrReady
221 }