blob: a4668aab6474df3051279fd4d5382f2d05d2d2b2 [file] [log] [blame]
Mohamed Abukar61bdef52020-03-09 16:46:12 +02001/*
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
21package main
22
23import (
24 "encoding/json"
vipin54a3a4f2020-09-23 12:19:58 +000025 "net/http"
26 "strconv"
27 "time"
Mohamed Abukara5c82992021-06-06 13:45:30 +000028
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 Abukar61bdef52020-03-09 16:46:12 +020032)
33
Mohamed Abukar3649fae2020-10-30 23:51:39 +020034func (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 Abukar2a74ea52021-03-07 10:10:58 +020045
46 app.Resource.InjectRoute("/ric/v1/symptomdata", a.SymptomDataHandler, "GET")
Mohamed Abukar3649fae2020-10-30 23:51:39 +020047}
48
vipin54a3a4f2020-09-23 12:19:58 +000049func (a *AlarmManager) respondWithError(w http.ResponseWriter, code int, message string) {
50 a.respondWithJSON(w, code, map[string]string{"error": message})
51}
52
Abukar Mohamed121e8b62020-09-18 11:41:33 +000053func (a *AlarmManager) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
Mohamed Abukar540ceee2020-09-09 08:07:40 +030054 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 Abukaraf0c5702020-03-11 10:29:40 +020061
Abukar Mohamed121e8b62020-09-18 11:41:33 +000062func (a *AlarmManager) GetActiveAlarms(w http.ResponseWriter, r *http.Request) {
Mohamed Abukar540ceee2020-09-09 08:07:40 +030063 app.Logger.Info("GetActiveAlarms: %+v", a.activeAlarms)
64 a.respondWithJSON(w, http.StatusOK, a.activeAlarms)
65}
Mohamed Abukaraf0c5702020-03-11 10:29:40 +020066
Abukar Mohamed121e8b62020-09-18 11:41:33 +000067func (a *AlarmManager) GetAlarmHistory(w http.ResponseWriter, r *http.Request) {
Mohamed Abukar540ceee2020-09-09 08:07:40 +030068 app.Logger.Info("GetAlarmHistory: %+v", a.alarmHistory)
69 a.respondWithJSON(w, http.StatusOK, a.alarmHistory)
Mohamed Abukar61bdef52020-03-09 16:46:12 +020070}
71
Abukar Mohamed121e8b62020-09-18 11:41:33 +000072func (a *AlarmManager) RaiseAlarm(w http.ResponseWriter, r *http.Request) {
Mohamed Abukar540ceee2020-09-09 08:07:40 +030073 if err := a.doAction(w, r, true); err != nil {
74 a.respondWithJSON(w, http.StatusOK, err)
75 }
Mohamed Abukaraf0c5702020-03-11 10:29:40 +020076}
77
Abukar Mohamed121e8b62020-09-18 11:41:33 +000078func (a *AlarmManager) ClearAlarm(w http.ResponseWriter, r *http.Request) {
Mohamed Abukar540ceee2020-09-09 08:07:40 +030079 if err := a.doAction(w, r, false); err != nil {
80 a.respondWithJSON(w, http.StatusOK, err)
81 }
Mohamed Abukaraf0c5702020-03-11 10:29:40 +020082}
83
vipin54a3a4f2020-09-23 12:19:58 +000084func (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 Mannila18fd03c2020-10-29 10:01:00 +0200114 ricAlarmDefintion.RaiseDelay = alarmDefinition.RaiseDelay
115 ricAlarmDefintion.ClearDelay = alarmDefinition.ClearDelay
Mohamed Abukar3649fae2020-10-30 23:51:39 +0200116 ricAlarmDefintion.TimeToLive = alarmDefinition.TimeToLive
vipin54a3a4f2020-09-23 12:19:58 +0000117 alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion
vipin6f73fa32020-10-06 06:51:53 +0000118 app.Logger.Debug("POST - alarm definition added for alarm id %v", alarmDefinition.AlarmId)
vipin54a3a4f2020-09-23 12:19:58 +0000119 }
120 }
121
122 a.respondWithJSON(w, http.StatusOK, nil)
123 return
124}
125
126func (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)
vipin4cedd502020-09-25 05:58:31 +0000132 app.Logger.Debug("DELETE - alarm definition deleted for alarmId %v", ialarmId)
vipin54a3a4f2020-09-23 12:19:58 +0000133 } 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
146func (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 Mohamed121e8b62020-09-18 11:41:33 +0000178func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error {
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300179 app.Logger.Info("doAction: request received = %t", isRaiseAlarm)
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200180
Mohamed Abukar61bdef52020-03-09 16:46:12 +0200181 if r.Body == nil {
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300182 app.Logger.Error("Error: Invalid message body!")
183 return nil
Mohamed Abukar61bdef52020-03-09 16:46:12 +0200184 }
185 defer r.Body.Close()
186
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300187 var m alarm.AlarmMessage
188 if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200189 app.Logger.Error("json.NewDecoder failed: %v", err)
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300190 return err
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200191 }
192
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300193 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 Mannila2be5ac52020-09-22 12:17:05 +0300199 m.AlarmTime = time.Now().UnixNano()
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300200 }
201
Mohamed Abukar105030f2020-10-22 18:08:34 +0300202 _, err := a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}})
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300203 return err
204}
205
Mohamed Abukar44be37f2021-06-14 09:17:27 +0300206// HandleViaRmr is defined but not used as of now
207/*
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000208func (a *AlarmManager) HandleViaRmr(d alarm.Alarm, isRaiseAlarm bool) error {
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300209 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 Abukaraf0c5702020-03-11 10:29:40 +0200213 }
214
215 alarmData := alarmClient.NewAlarm(d.SpecificProblem, d.PerceivedSeverity, d.AdditionalInfo, d.IdentifyingInfo)
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300216 if isRaiseAlarm {
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200217 alarmClient.Raise(alarmData)
218 } else {
219 alarmClient.Clear(alarmData)
Mohamed Abukar61bdef52020-03-09 16:46:12 +0200220 }
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300221
222 return nil
Mohamed Abukar44be37f2021-06-14 09:17:27 +0300223}*/
vipin541eb502020-09-22 12:04:59 +0000224
225func (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
238func (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 Abukar2a74ea52021-03-07 10:10:58 +0200247
248func (a *AlarmManager) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
249 baseDir := "/tmp/symptomdata/"
Mohamed Abukara5c82992021-06-06 13:45:30 +0000250 if err := app.Util.CreateDir(baseDir); err != nil {
251 app.Resource.SendSymptomDataError(w, r, "CreateDir failed: "+err.Error())
Mohamed Abukar2a74ea52021-03-07 10:10:58 +0200252 return
253 }
254
255 if b, err := json.Marshal(a.activeAlarms); err == nil {
Mohamed Abukara5c82992021-06-06 13:45:30 +0000256 if err := app.Util.WriteToFile(baseDir+"active_alarms.json", string(b)); err != nil {
257 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
Mohamed Abukar2a74ea52021-03-07 10:10:58 +0200258 return
259 }
260 }
261
262 if b, err := json.Marshal(a.alarmHistory); err == nil {
Mohamed Abukara5c82992021-06-06 13:45:30 +0000263 if err := app.Util.WriteToFile(baseDir+"alarm_history.json", string(b)); err != nil {
264 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
Mohamed Abukar2a74ea52021-03-07 10:10:58 +0200265 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 Abukara5c82992021-06-06 13:45:30 +0000274 if err := app.Util.WriteToFile(baseDir+"alarm_config.json", string(b)); err != nil {
275 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
Mohamed Abukar2a74ea52021-03-07 10:10:58 +0200276 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 Abukara5c82992021-06-06 13:45:30 +0000285 if err := app.Util.WriteToFile(baseDir+"alarm_defs.json", string(b)); err != nil {
286 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
Mohamed Abukar2a74ea52021-03-07 10:10:58 +0200287 return
288 }
289 }
290
Mohamed Abukara5c82992021-06-06 13:45:30 +0000291 app.Resource.SendSymptomDataFile(w, r, baseDir, "symptomdata.zip")
Mohamed Abukar2a74ea52021-03-07 10:10:58 +0200292}