Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 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" |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 25 | "net/http" |
| 26 | "strconv" |
| 27 | "time" |
Mohamed Abukar | a5c8299 | 2021-06-06 13:45:30 +0000 | [diff] [blame] | 28 | |
| 29 | "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm" |
| 30 | app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 31 | "github.com/gorilla/mux" |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 34 | func (a *AlarmManager) InjectRoutes() { |
| 35 | app.Resource.InjectRoute("/ric/v1/alarms", a.RaiseAlarm, "POST") |
| 36 | app.Resource.InjectRoute("/ric/v1/alarms", a.ClearAlarm, "DELETE") |
| 37 | app.Resource.InjectRoute("/ric/v1/alarms/active", a.GetActiveAlarms, "GET") |
| 38 | app.Resource.InjectRoute("/ric/v1/alarms/history", a.GetAlarmHistory, "GET") |
| 39 | app.Resource.InjectRoute("/ric/v1/alarms/config", a.SetAlarmConfig, "POST") |
| 40 | app.Resource.InjectRoute("/ric/v1/alarms/config", a.GetAlarmConfig, "GET") |
| 41 | app.Resource.InjectRoute("/ric/v1/alarms/define", a.SetAlarmDefinition, "POST") |
| 42 | app.Resource.InjectRoute("/ric/v1/alarms/define/{alarmId}", a.DeleteAlarmDefinition, "DELETE") |
| 43 | app.Resource.InjectRoute("/ric/v1/alarms/define", a.GetAlarmDefinition, "GET") |
| 44 | app.Resource.InjectRoute("/ric/v1/alarms/define/{alarmId}", a.GetAlarmDefinition, "GET") |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 45 | |
| 46 | app.Resource.InjectRoute("/ric/v1/symptomdata", a.SymptomDataHandler, "GET") |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 47 | } |
| 48 | |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 49 | func (a *AlarmManager) respondWithError(w http.ResponseWriter, code int, message string) { |
| 50 | a.respondWithJSON(w, code, map[string]string{"error": message}) |
| 51 | } |
| 52 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 53 | func (a *AlarmManager) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 54 | w.Header().Set("Content-Type", "application/json") |
| 55 | w.WriteHeader(code) |
| 56 | if payload != nil { |
| 57 | response, _ := json.Marshal(payload) |
| 58 | w.Write(response) |
| 59 | } |
| 60 | } |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 61 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 62 | func (a *AlarmManager) GetActiveAlarms(w http.ResponseWriter, r *http.Request) { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 63 | app.Logger.Info("GetActiveAlarms: %+v", a.activeAlarms) |
| 64 | a.respondWithJSON(w, http.StatusOK, a.activeAlarms) |
| 65 | } |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 66 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 67 | func (a *AlarmManager) GetAlarmHistory(w http.ResponseWriter, r *http.Request) { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 68 | app.Logger.Info("GetAlarmHistory: %+v", a.alarmHistory) |
| 69 | a.respondWithJSON(w, http.StatusOK, a.alarmHistory) |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 72 | func (a *AlarmManager) RaiseAlarm(w http.ResponseWriter, r *http.Request) { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 73 | if err := a.doAction(w, r, true); err != nil { |
| 74 | a.respondWithJSON(w, http.StatusOK, err) |
| 75 | } |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 78 | func (a *AlarmManager) ClearAlarm(w http.ResponseWriter, r *http.Request) { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 79 | if err := a.doAction(w, r, false); err != nil { |
| 80 | a.respondWithJSON(w, http.StatusOK, err) |
| 81 | } |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 82 | } |
| 83 | |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 84 | func (a *AlarmManager) SetAlarmDefinition(w http.ResponseWriter, r *http.Request) { |
| 85 | |
| 86 | app.Logger.Debug("POST arrived for creating alarm definition ") |
| 87 | /* If body is nil then return error */ |
| 88 | if r.Body == nil { |
| 89 | app.Logger.Error("POST - body is empty") |
| 90 | a.respondWithError(w, http.StatusBadRequest, "No data in request body.") |
| 91 | return |
| 92 | } |
| 93 | defer r.Body.Close() |
| 94 | |
| 95 | /* Parameters are available. Check if they are valid */ |
| 96 | var alarmDefinitions RicAlarmDefinitions |
| 97 | err := json.NewDecoder(r.Body).Decode(&alarmDefinitions) |
| 98 | if err != nil { |
| 99 | app.Logger.Error("POST - received alarm definition parameters are invalid - " + err.Error()) |
| 100 | a.respondWithError(w, http.StatusBadRequest, "Invalid data in request body.") |
| 101 | return |
| 102 | } |
| 103 | |
| 104 | for _, alarmDefinition := range alarmDefinitions.AlarmDefinitions { |
| 105 | _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] |
| 106 | if exists { |
| 107 | app.Logger.Error("POST - alarm definition already exists for %v", alarmDefinition.AlarmId) |
| 108 | } else { |
| 109 | ricAlarmDefintion := new(alarm.AlarmDefinition) |
| 110 | ricAlarmDefintion.AlarmId = alarmDefinition.AlarmId |
| 111 | ricAlarmDefintion.AlarmText = alarmDefinition.AlarmText |
| 112 | ricAlarmDefintion.EventType = alarmDefinition.EventType |
| 113 | ricAlarmDefintion.OperationInstructions = alarmDefinition.OperationInstructions |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 114 | ricAlarmDefintion.RaiseDelay = alarmDefinition.RaiseDelay |
| 115 | ricAlarmDefintion.ClearDelay = alarmDefinition.ClearDelay |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 116 | ricAlarmDefintion.TimeToLive = alarmDefinition.TimeToLive |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 117 | alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 118 | app.Logger.Debug("POST - alarm definition added for alarm id %v", alarmDefinition.AlarmId) |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | a.respondWithJSON(w, http.StatusOK, nil) |
| 123 | return |
| 124 | } |
| 125 | |
| 126 | func (a *AlarmManager) DeleteAlarmDefinition(w http.ResponseWriter, r *http.Request) { |
| 127 | pathParams := mux.Vars(r) |
| 128 | alarmId, alarmIdok := pathParams["alarmId"] |
| 129 | if alarmIdok { |
| 130 | if ialarmId, err := strconv.Atoi(alarmId); err == nil { |
| 131 | delete(alarm.RICAlarmDefinitions, ialarmId) |
vipin | 4cedd50 | 2020-09-25 05:58:31 +0000 | [diff] [blame] | 132 | app.Logger.Debug("DELETE - alarm definition deleted for alarmId %v", ialarmId) |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 133 | } else { |
| 134 | app.Logger.Error("DELETE - alarmId string to int conversion failed %v", alarmId) |
| 135 | a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter") |
| 136 | return |
| 137 | } |
| 138 | } else { |
| 139 | app.Logger.Error("DELETE - alarmId does not exist %v", alarmId) |
| 140 | a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter") |
| 141 | return |
| 142 | |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func (a *AlarmManager) GetAlarmDefinition(w http.ResponseWriter, r *http.Request) { |
| 147 | var ricAlarmDefinitions RicAlarmDefinitions |
| 148 | pathParams := mux.Vars(r) |
| 149 | alarmId, alarmIdok := pathParams["alarmId"] |
| 150 | if alarmIdok { |
| 151 | if ialarmId, err := strconv.Atoi(alarmId); err == nil { |
| 152 | alarmDefinition, ok := alarm.RICAlarmDefinitions[ialarmId] |
| 153 | if ok { |
| 154 | app.Logger.Debug("Successfully returned alarm defintion for alarm id %v", ialarmId) |
| 155 | a.respondWithJSON(w, http.StatusOK, alarmDefinition) |
| 156 | return |
| 157 | |
| 158 | } else { |
| 159 | app.Logger.Error("Requested alarm id not found %v", ialarmId) |
| 160 | a.respondWithError(w, http.StatusBadRequest, "Non existent alarmId") |
| 161 | return |
| 162 | } |
| 163 | } else { |
| 164 | app.Logger.Error("alarmId string to int conversion failed %v", alarmId) |
| 165 | a.respondWithError(w, http.StatusBadRequest, "Invalid alarmId") |
| 166 | return |
| 167 | } |
| 168 | } else { |
| 169 | app.Logger.Debug("GET arrived for all alarm definitions ") |
| 170 | for _, alarmDefinition := range alarm.RICAlarmDefinitions { |
| 171 | ricAlarmDefinitions.AlarmDefinitions = append(ricAlarmDefinitions.AlarmDefinitions, alarmDefinition) |
| 172 | } |
| 173 | app.Logger.Debug("Successfully returned all alarm definitions") |
| 174 | a.respondWithJSON(w, http.StatusOK, ricAlarmDefinitions) |
| 175 | } |
| 176 | } |
| 177 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 178 | func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 179 | app.Logger.Info("doAction: request received = %t", isRaiseAlarm) |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 180 | |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 181 | if r.Body == nil { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 182 | app.Logger.Error("Error: Invalid message body!") |
| 183 | return nil |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 184 | } |
| 185 | defer r.Body.Close() |
| 186 | |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 187 | var m alarm.AlarmMessage |
| 188 | if err := json.NewDecoder(r.Body).Decode(&m); err != nil { |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 189 | app.Logger.Error("json.NewDecoder failed: %v", err) |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 190 | return err |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 193 | if m.Alarm.ManagedObjectId == "" || m.Alarm.ApplicationId == "" || m.AlarmAction == "" { |
| 194 | app.Logger.Error("Error: Mandatory parameters missing!") |
| 195 | return nil |
| 196 | } |
| 197 | |
| 198 | if m.AlarmTime == 0 { |
Anssi Mannila | 2be5ac5 | 2020-09-22 12:17:05 +0300 | [diff] [blame] | 199 | m.AlarmTime = time.Now().UnixNano() |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 200 | } |
| 201 | |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 202 | _, err := a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}}) |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 203 | return err |
| 204 | } |
| 205 | |
Mohamed Abukar | 44be37f | 2021-06-14 09:17:27 +0300 | [diff] [blame] | 206 | // HandleViaRmr is defined but not used as of now |
| 207 | /* |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 208 | func (a *AlarmManager) HandleViaRmr(d alarm.Alarm, isRaiseAlarm bool) error { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 209 | alarmClient, err := alarm.InitAlarm(d.ManagedObjectId, d.ApplicationId) |
| 210 | if err != nil { |
| 211 | app.Logger.Error("json.NewDecoder failed: %v", err) |
| 212 | return err |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | alarmData := alarmClient.NewAlarm(d.SpecificProblem, d.PerceivedSeverity, d.AdditionalInfo, d.IdentifyingInfo) |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 216 | if isRaiseAlarm { |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 217 | alarmClient.Raise(alarmData) |
| 218 | } else { |
| 219 | alarmClient.Clear(alarmData) |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 220 | } |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 221 | |
| 222 | return nil |
Mohamed Abukar | 44be37f | 2021-06-14 09:17:27 +0300 | [diff] [blame] | 223 | }*/ |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 224 | |
| 225 | func (a *AlarmManager) SetAlarmConfig(w http.ResponseWriter, r *http.Request) { |
| 226 | var m alarm.AlarmConfigParams |
| 227 | if err := json.NewDecoder(r.Body).Decode(&m); err != nil { |
| 228 | app.Logger.Error("json.NewDecoder failed: %v", err) |
| 229 | } else { |
| 230 | a.maxActiveAlarms = m.MaxActiveAlarms |
| 231 | a.maxAlarmHistory = m.MaxAlarmHistory |
| 232 | app.Logger.Debug("new maxActiveAlarms = %v", a.maxActiveAlarms) |
| 233 | app.Logger.Debug("new maxAlarmHistory = %v", a.maxAlarmHistory) |
| 234 | a.respondWithJSON(w, http.StatusOK, err) |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | func (a *AlarmManager) GetAlarmConfig(w http.ResponseWriter, r *http.Request) { |
| 239 | var m alarm.AlarmConfigParams |
| 240 | |
| 241 | m.MaxActiveAlarms = a.maxActiveAlarms |
| 242 | m.MaxAlarmHistory = a.maxAlarmHistory |
| 243 | |
| 244 | a.respondWithJSON(w, http.StatusOK, m) |
| 245 | return |
| 246 | } |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 247 | |
| 248 | func (a *AlarmManager) SymptomDataHandler(w http.ResponseWriter, r *http.Request) { |
| 249 | baseDir := "/tmp/symptomdata/" |
Mohamed Abukar | a5c8299 | 2021-06-06 13:45:30 +0000 | [diff] [blame] | 250 | if err := app.Util.CreateDir(baseDir); err != nil { |
| 251 | app.Resource.SendSymptomDataError(w, r, "CreateDir failed: "+err.Error()) |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 252 | return |
| 253 | } |
| 254 | |
| 255 | if b, err := json.Marshal(a.activeAlarms); err == nil { |
Mohamed Abukar | a5c8299 | 2021-06-06 13:45:30 +0000 | [diff] [blame] | 256 | if err := app.Util.WriteToFile(baseDir+"active_alarms.json", string(b)); err != nil { |
| 257 | app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error()) |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 258 | return |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if b, err := json.Marshal(a.alarmHistory); err == nil { |
Mohamed Abukar | a5c8299 | 2021-06-06 13:45:30 +0000 | [diff] [blame] | 263 | if err := app.Util.WriteToFile(baseDir+"alarm_history.json", string(b)); err != nil { |
| 264 | app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error()) |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 265 | return |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | var ac alarm.AlarmConfigParams |
| 270 | ac.MaxActiveAlarms = a.maxActiveAlarms |
| 271 | ac.MaxAlarmHistory = a.maxAlarmHistory |
| 272 | |
| 273 | if b, err := json.Marshal(ac); err == nil { |
Mohamed Abukar | a5c8299 | 2021-06-06 13:45:30 +0000 | [diff] [blame] | 274 | if err := app.Util.WriteToFile(baseDir+"alarm_config.json", string(b)); err != nil { |
| 275 | app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error()) |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 276 | return |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | var ad RicAlarmDefinitions |
| 281 | for _, alarmDefinition := range alarm.RICAlarmDefinitions { |
| 282 | ad.AlarmDefinitions = append(ad.AlarmDefinitions, alarmDefinition) |
| 283 | } |
| 284 | if b, err := json.Marshal(ad); err == nil { |
Mohamed Abukar | a5c8299 | 2021-06-06 13:45:30 +0000 | [diff] [blame] | 285 | if err := app.Util.WriteToFile(baseDir+"alarm_defs.json", string(b)); err != nil { |
| 286 | app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error()) |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 287 | return |
| 288 | } |
| 289 | } |
| 290 | |
Mohamed Abukar | a5c8299 | 2021-06-06 13:45:30 +0000 | [diff] [blame] | 291 | app.Resource.SendSymptomDataFile(w, r, baseDir, "symptomdata.zip") |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 292 | } |