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).
29 "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
30 app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
31 "github.com/gorilla/mux"
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")
46 app.Resource.InjectRoute("/ric/v1/symptomdata", a.SymptomDataHandler, "GET")
49 func (a *AlarmManager) respondWithError(w http.ResponseWriter, code int, message string) {
50 a.respondWithJSON(w, code, map[string]string{"error": message})
53 func (a *AlarmManager) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
54 w.Header().Set("Content-Type", "application/json")
57 response, _ := json.Marshal(payload)
62 func (a *AlarmManager) GetActiveAlarms(w http.ResponseWriter, r *http.Request) {
63 app.Logger.Info("GetActiveAlarms: %+v", a.activeAlarms)
64 a.respondWithJSON(w, http.StatusOK, a.activeAlarms)
67 func (a *AlarmManager) GetAlarmHistory(w http.ResponseWriter, r *http.Request) {
68 app.Logger.Info("GetAlarmHistory: %+v", a.alarmHistory)
69 a.respondWithJSON(w, http.StatusOK, a.alarmHistory)
72 func (a *AlarmManager) RaiseAlarm(w http.ResponseWriter, r *http.Request) {
73 if err := a.doAction(w, r, true); err != nil {
74 a.respondWithJSON(w, http.StatusOK, err)
78 func (a *AlarmManager) ClearAlarm(w http.ResponseWriter, r *http.Request) {
79 if err := a.doAction(w, r, false); err != nil {
80 a.respondWithJSON(w, http.StatusOK, err)
84 func (a *AlarmManager) SetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
86 app.Logger.Debug("POST arrived for creating alarm definition ")
87 /* If body is nil then return error */
89 app.Logger.Error("POST - body is empty")
90 a.respondWithError(w, http.StatusBadRequest, "No data in request body.")
95 /* Parameters are available. Check if they are valid */
96 var alarmDefinitions RicAlarmDefinitions
97 err := json.NewDecoder(r.Body).Decode(&alarmDefinitions)
99 app.Logger.Error("POST - received alarm definition parameters are invalid - " + err.Error())
100 a.respondWithError(w, http.StatusBadRequest, "Invalid data in request body.")
104 for _, alarmDefinition := range alarmDefinitions.AlarmDefinitions {
105 _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId]
107 app.Logger.Error("POST - alarm definition already exists for %v", alarmDefinition.AlarmId)
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
114 ricAlarmDefintion.RaiseDelay = alarmDefinition.RaiseDelay
115 ricAlarmDefintion.ClearDelay = alarmDefinition.ClearDelay
116 ricAlarmDefintion.TimeToLive = alarmDefinition.TimeToLive
117 alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion
118 app.Logger.Debug("POST - alarm definition added for alarm id %v", alarmDefinition.AlarmId)
122 a.respondWithJSON(w, http.StatusOK, nil)
126 func (a *AlarmManager) DeleteAlarmDefinition(w http.ResponseWriter, r *http.Request) {
127 pathParams := mux.Vars(r)
128 alarmId, alarmIdok := pathParams["alarmId"]
130 if ialarmId, err := strconv.Atoi(alarmId); err == nil {
131 delete(alarm.RICAlarmDefinitions, ialarmId)
132 app.Logger.Debug("DELETE - alarm definition deleted for alarmId %v", ialarmId)
134 app.Logger.Error("DELETE - alarmId string to int conversion failed %v", alarmId)
135 a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
139 app.Logger.Error("DELETE - alarmId does not exist %v", alarmId)
140 a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
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"]
151 if ialarmId, err := strconv.Atoi(alarmId); err == nil {
152 alarmDefinition, ok := alarm.RICAlarmDefinitions[ialarmId]
154 app.Logger.Debug("Successfully returned alarm defintion for alarm id %v", ialarmId)
155 a.respondWithJSON(w, http.StatusOK, alarmDefinition)
159 app.Logger.Error("Requested alarm id not found %v", ialarmId)
160 a.respondWithError(w, http.StatusBadRequest, "Non existent alarmId")
164 app.Logger.Error("alarmId string to int conversion failed %v", alarmId)
165 a.respondWithError(w, http.StatusBadRequest, "Invalid alarmId")
169 app.Logger.Debug("GET arrived for all alarm definitions ")
170 for _, alarmDefinition := range alarm.RICAlarmDefinitions {
171 ricAlarmDefinitions.AlarmDefinitions = append(ricAlarmDefinitions.AlarmDefinitions, alarmDefinition)
173 app.Logger.Debug("Successfully returned all alarm definitions")
174 a.respondWithJSON(w, http.StatusOK, ricAlarmDefinitions)
178 func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error {
179 app.Logger.Info("doAction: request received = %t", isRaiseAlarm)
182 app.Logger.Error("Error: Invalid message body!")
187 var m alarm.AlarmMessage
188 if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
189 app.Logger.Error("json.NewDecoder failed: %v", err)
193 if m.Alarm.ManagedObjectId == "" || m.Alarm.ApplicationId == "" || m.AlarmAction == "" {
194 app.Logger.Error("Error: Mandatory parameters missing!")
198 if m.AlarmTime == 0 {
199 m.AlarmTime = time.Now().UnixNano()
202 _, err := a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}})
206 // HandleViaRmr is defined but not used as of now
208 func (a *AlarmManager) HandleViaRmr(d alarm.Alarm, isRaiseAlarm bool) error {
209 alarmClient, err := alarm.InitAlarm(d.ManagedObjectId, d.ApplicationId)
211 app.Logger.Error("json.NewDecoder failed: %v", err)
215 alarmData := alarmClient.NewAlarm(d.SpecificProblem, d.PerceivedSeverity, d.AdditionalInfo, d.IdentifyingInfo)
217 alarmClient.Raise(alarmData)
219 alarmClient.Clear(alarmData)
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)
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)
238 func (a *AlarmManager) GetAlarmConfig(w http.ResponseWriter, r *http.Request) {
239 var m alarm.AlarmConfigParams
241 m.MaxActiveAlarms = a.maxActiveAlarms
242 m.MaxAlarmHistory = a.maxAlarmHistory
244 a.respondWithJSON(w, http.StatusOK, m)
248 func (a *AlarmManager) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
249 baseDir := "/tmp/symptomdata/"
250 if err := app.Util.CreateDir(baseDir); err != nil {
251 app.Resource.SendSymptomDataError(w, r, "CreateDir failed: "+err.Error())
255 if b, err := json.MarshalIndent(a.activeAlarms, "", " "); err == nil {
256 if err := app.Util.WriteToFile(baseDir+"active_alarms.json", string(b)); err != nil {
257 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
262 if b, err := json.MarshalIndent(a.alarmHistory, "", " "); err == nil {
263 if err := app.Util.WriteToFile(baseDir+"alarm_history.json", string(b)); err != nil {
264 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
269 if alerts, err := a.GetAlerts(); err == nil {
270 if b, err := json.MarshalIndent(alerts, "", " "); err == nil {
271 if err := app.Util.WriteToFile(baseDir+"alerts.json", string(b)); err != nil {
272 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
278 var ac alarm.AlarmConfigParams
279 ac.MaxActiveAlarms = a.maxActiveAlarms
280 ac.MaxAlarmHistory = a.maxAlarmHistory
282 if b, err := json.MarshalIndent(ac, "", " "); err == nil {
283 if err := app.Util.WriteToFile(baseDir+"alarm_config.json", string(b)); err != nil {
284 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
289 var ad RicAlarmDefinitions
290 for _, alarmDefinition := range alarm.RICAlarmDefinitions {
291 ad.AlarmDefinitions = append(ad.AlarmDefinitions, alarmDefinition)
293 if b, err := json.MarshalIndent(ad, "", " "); err == nil {
294 if err := app.Util.WriteToFile(baseDir+"alarm_defs.json", string(b)); err != nil {
295 app.Resource.SendSymptomDataError(w, r, "writeToFile failed: "+err.Error())
300 app.Resource.SendSymptomDataFile(w, r, baseDir, "symptomdata.zip")