2 * Copyright (c) 2020 AT&T Intellectual Property.
3 * Copyright (c) 2020 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
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"
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"
39 type AlarmAdapter struct {
44 activeAlarms []alarm.Alarm
48 // Temp alarm constants & definitions
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
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",
68 NewAlarmAdapter("", 0).Run(true)
71 func NewAlarmAdapter(amHost string, alertInterval int) *AlarmAdapter {
72 if alertInterval == 0 {
73 alertInterval = viper.GetInt("promAlertManager.alertInterval")
77 amHost = viper.GetString("promAlertManager.address")
83 amBaseUrl: viper.GetString("promAlertManager.baseUrl"),
84 amSchemes: []string{viper.GetString("promAlertManager.schemes")},
85 alertInterval: alertInterval,
86 activeAlarms: make([]alarm.Alarm, 0),
90 func (a *AlarmAdapter) Run(sdlcheck bool) {
91 app.Logger.SetMdc("alarmAdapter", fmt.Sprintf("%s:%s", Version, Hash))
92 app.SetReadyCB(func(d interface{}) { a.rmrReady = true }, true)
93 app.Resource.InjectStatusCb(a.StatusCB)
95 app.Resource.InjectRoute("/ric/v1/alarm", a.GetActiveAlarms, "GET")
96 app.Resource.InjectRoute("/ric/v1/alarm", a.GenerateAlarm, "POST")
98 // Start background timer for re-raising alerts
99 go a.StartAlertTimer()
101 app.RunWithParams(a, sdlcheck)
104 func (a *AlarmAdapter) StartAlertTimer() {
105 tick := time.Tick(time.Duration(a.alertInterval) * time.Millisecond)
107 for _, m := range a.activeAlarms {
108 app.Logger.Info("Re-raising alarm: %v", m)
109 a.PostAlert(a.GenerateAlertLabels(m))
114 func (a *AlarmAdapter) Consume(rp *app.RMRParams) (err error) {
115 app.Logger.Info("Message received!")
117 defer app.Rmr.Free(rp.Mbuf)
119 case alarm.RIC_ALARM_UPDATE:
122 app.Logger.Info("Unknown Message Type '%d', discarding", rp.Mtype)
128 func (a *AlarmAdapter) HandleAlarms(rp *app.RMRParams) (*alert.PostAlertsOK, error) {
129 var m alarm.AlarmMessage
130 if err := json.Unmarshal(rp.Payload, &m); err != nil {
131 app.Logger.Error("json.Unmarshal failed: %v", err)
134 app.Logger.Info("newAlarm: %v", m)
136 if _, ok := alarmDefinitions[m.Alarm.SpecificProblem]; !ok {
137 app.Logger.Warn("Alarm (SP='%d') not recognized, ignoring ...", m.Alarm.SpecificProblem)
141 // Suppress duplicate alarms
142 idx, found := a.IsMatchFound(m.Alarm)
143 if found && m.AlarmAction != alarm.AlarmActionClear {
144 app.Logger.Info("Duplicate alarm ... suppressing!")
148 // Clear alarm if found from active alarm list
149 if m.AlarmAction == alarm.AlarmActionClear {
151 a.activeAlarms = a.RemoveAlarm(a.activeAlarms, idx)
152 app.Logger.Info("Active alarm cleared!")
154 app.Logger.Info("No matching alarm found, ignoring!")
159 // New alarm -> update active alarms and post to Alert Manager
160 if m.AlarmAction == alarm.AlarmActionRaise {
161 a.UpdateActiveAlarms(m.Alarm)
162 return a.PostAlert(a.GenerateAlertLabels(m.Alarm))
168 func (a *AlarmAdapter) IsMatchFound(newAlarm alarm.Alarm) (int, bool) {
169 for i, m := range a.activeAlarms {
170 if m.ManagedObjectId == newAlarm.ManagedObjectId && m.ApplicationId == newAlarm.ApplicationId &&
171 m.SpecificProblem == newAlarm.SpecificProblem && m.IdentifyingInfo == newAlarm.IdentifyingInfo {
178 func (a *AlarmAdapter) RemoveAlarm(alarms []alarm.Alarm, i int) []alarm.Alarm {
179 copy(alarms[i:], alarms[i+1:])
180 return alarms[:len(alarms)-1]
183 func (a *AlarmAdapter) UpdateActiveAlarms(newAlarm alarm.Alarm) {
184 // For now just keep the active alarms in-memory. Use SDL later
185 a.activeAlarms = append(a.activeAlarms, newAlarm)
188 func (a *AlarmAdapter) GenerateAlertLabels(newAlarm alarm.Alarm) (models.LabelSet, models.LabelSet) {
189 amLabels := models.LabelSet{
190 "alertname": alarmDefinitions[newAlarm.SpecificProblem],
191 "severity": string(newAlarm.PerceivedSeverity),
192 "service": fmt.Sprintf("%s:%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId),
193 "system_name": "RIC",
195 amAnnotations := models.LabelSet{
196 "description": newAlarm.IdentifyingInfo,
197 "additional_info": newAlarm.AdditionalInfo,
200 return amLabels, amAnnotations
203 func (a *AlarmAdapter) NewAlertmanagerClient() *client.Alertmanager {
204 cr := clientruntime.New(a.amHost, a.amBaseUrl, a.amSchemes)
205 return client.New(cr, strfmt.Default)
208 func (a *AlarmAdapter) PostAlert(amLabels, amAnnotations models.LabelSet) (*alert.PostAlertsOK, error) {
209 pa := &models.PostableAlert{
211 GeneratorURL: strfmt.URI(""),
214 Annotations: amAnnotations,
216 alertParams := alert.NewPostAlertsParams().WithAlerts(models.PostableAlerts{pa})
218 app.Logger.Info("Posting alerts: labels: %v, annotations: %v", amLabels, amAnnotations)
219 return a.NewAlertmanagerClient().Alert.PostAlerts(alertParams)
222 func (a *AlarmAdapter) StatusCB() bool {
224 app.Logger.Info("RMR not ready yet!")