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).
25 "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
26 app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
27 "github.com/gorilla/mux"
33 func (a *AlarmManager) InjectRoutes() {
34 app.Resource.InjectRoute("/ric/v1/alarms", a.RaiseAlarm, "POST")
35 app.Resource.InjectRoute("/ric/v1/alarms", a.ClearAlarm, "DELETE")
36 app.Resource.InjectRoute("/ric/v1/alarms/active", a.GetActiveAlarms, "GET")
37 app.Resource.InjectRoute("/ric/v1/alarms/history", a.GetAlarmHistory, "GET")
38 app.Resource.InjectRoute("/ric/v1/alarms/config", a.SetAlarmConfig, "POST")
39 app.Resource.InjectRoute("/ric/v1/alarms/config", a.GetAlarmConfig, "GET")
40 app.Resource.InjectRoute("/ric/v1/alarms/define", a.SetAlarmDefinition, "POST")
41 app.Resource.InjectRoute("/ric/v1/alarms/define/{alarmId}", a.DeleteAlarmDefinition, "DELETE")
42 app.Resource.InjectRoute("/ric/v1/alarms/define", a.GetAlarmDefinition, "GET")
43 app.Resource.InjectRoute("/ric/v1/alarms/define/{alarmId}", a.GetAlarmDefinition, "GET")
46 func (a *AlarmManager) respondWithError(w http.ResponseWriter, code int, message string) {
47 a.respondWithJSON(w, code, map[string]string{"error": message})
50 func (a *AlarmManager) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
51 w.Header().Set("Content-Type", "application/json")
54 response, _ := json.Marshal(payload)
59 func (a *AlarmManager) GetActiveAlarms(w http.ResponseWriter, r *http.Request) {
60 app.Logger.Info("GetActiveAlarms: %+v", a.activeAlarms)
61 a.respondWithJSON(w, http.StatusOK, a.activeAlarms)
64 func (a *AlarmManager) GetAlarmHistory(w http.ResponseWriter, r *http.Request) {
65 app.Logger.Info("GetAlarmHistory: %+v", a.alarmHistory)
66 a.respondWithJSON(w, http.StatusOK, a.alarmHistory)
69 func (a *AlarmManager) RaiseAlarm(w http.ResponseWriter, r *http.Request) {
70 if err := a.doAction(w, r, true); err != nil {
71 a.respondWithJSON(w, http.StatusOK, err)
75 func (a *AlarmManager) ClearAlarm(w http.ResponseWriter, r *http.Request) {
76 if err := a.doAction(w, r, false); err != nil {
77 a.respondWithJSON(w, http.StatusOK, err)
81 func (a *AlarmManager) SetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
83 app.Logger.Debug("POST arrived for creating alarm definition ")
84 /* If body is nil then return error */
86 app.Logger.Error("POST - body is empty")
87 a.respondWithError(w, http.StatusBadRequest, "No data in request body.")
92 /* Parameters are available. Check if they are valid */
93 var alarmDefinitions RicAlarmDefinitions
94 err := json.NewDecoder(r.Body).Decode(&alarmDefinitions)
96 app.Logger.Error("POST - received alarm definition parameters are invalid - " + err.Error())
97 a.respondWithError(w, http.StatusBadRequest, "Invalid data in request body.")
101 for _, alarmDefinition := range alarmDefinitions.AlarmDefinitions {
102 _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId]
104 app.Logger.Error("POST - alarm definition already exists for %v", alarmDefinition.AlarmId)
106 ricAlarmDefintion := new(alarm.AlarmDefinition)
107 ricAlarmDefintion.AlarmId = alarmDefinition.AlarmId
108 ricAlarmDefintion.AlarmText = alarmDefinition.AlarmText
109 ricAlarmDefintion.EventType = alarmDefinition.EventType
110 ricAlarmDefintion.OperationInstructions = alarmDefinition.OperationInstructions
111 ricAlarmDefintion.RaiseDelay = alarmDefinition.RaiseDelay
112 ricAlarmDefintion.ClearDelay = alarmDefinition.ClearDelay
113 ricAlarmDefintion.TimeToLive = alarmDefinition.TimeToLive
114 alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion
115 app.Logger.Debug("POST - alarm definition added for alarm id %v", alarmDefinition.AlarmId)
119 a.respondWithJSON(w, http.StatusOK, nil)
123 func (a *AlarmManager) DeleteAlarmDefinition(w http.ResponseWriter, r *http.Request) {
124 pathParams := mux.Vars(r)
125 alarmId, alarmIdok := pathParams["alarmId"]
127 if ialarmId, err := strconv.Atoi(alarmId); err == nil {
128 delete(alarm.RICAlarmDefinitions, ialarmId)
129 app.Logger.Debug("DELETE - alarm definition deleted for alarmId %v", ialarmId)
131 app.Logger.Error("DELETE - alarmId string to int conversion failed %v", alarmId)
132 a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
136 app.Logger.Error("DELETE - alarmId does not exist %v", alarmId)
137 a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
143 func (a *AlarmManager) GetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
144 var ricAlarmDefinitions RicAlarmDefinitions
145 pathParams := mux.Vars(r)
146 alarmId, alarmIdok := pathParams["alarmId"]
148 if ialarmId, err := strconv.Atoi(alarmId); err == nil {
149 alarmDefinition, ok := alarm.RICAlarmDefinitions[ialarmId]
151 app.Logger.Debug("Successfully returned alarm defintion for alarm id %v", ialarmId)
152 a.respondWithJSON(w, http.StatusOK, alarmDefinition)
156 app.Logger.Error("Requested alarm id not found %v", ialarmId)
157 a.respondWithError(w, http.StatusBadRequest, "Non existent alarmId")
161 app.Logger.Error("alarmId string to int conversion failed %v", alarmId)
162 a.respondWithError(w, http.StatusBadRequest, "Invalid alarmId")
166 app.Logger.Debug("GET arrived for all alarm definitions ")
167 for _, alarmDefinition := range alarm.RICAlarmDefinitions {
168 ricAlarmDefinitions.AlarmDefinitions = append(ricAlarmDefinitions.AlarmDefinitions, alarmDefinition)
170 app.Logger.Debug("Successfully returned all alarm definitions")
171 a.respondWithJSON(w, http.StatusOK, ricAlarmDefinitions)
175 func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error {
176 app.Logger.Info("doAction: request received = %t", isRaiseAlarm)
179 app.Logger.Error("Error: Invalid message body!")
184 var m alarm.AlarmMessage
185 if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
186 app.Logger.Error("json.NewDecoder failed: %v", err)
190 if m.Alarm.ManagedObjectId == "" || m.Alarm.ApplicationId == "" || m.AlarmAction == "" {
191 app.Logger.Error("Error: Mandatory parameters missing!")
195 if m.AlarmTime == 0 {
196 m.AlarmTime = time.Now().UnixNano()
199 _, err := a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}})
203 func (a *AlarmManager) HandleViaRmr(d alarm.Alarm, isRaiseAlarm bool) error {
204 alarmClient, err := alarm.InitAlarm(d.ManagedObjectId, d.ApplicationId)
206 app.Logger.Error("json.NewDecoder failed: %v", err)
210 alarmData := alarmClient.NewAlarm(d.SpecificProblem, d.PerceivedSeverity, d.AdditionalInfo, d.IdentifyingInfo)
212 alarmClient.Raise(alarmData)
214 alarmClient.Clear(alarmData)
220 func (a *AlarmManager) SetAlarmConfig(w http.ResponseWriter, r *http.Request) {
221 var m alarm.AlarmConfigParams
222 if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
223 app.Logger.Error("json.NewDecoder failed: %v", err)
225 a.maxActiveAlarms = m.MaxActiveAlarms
226 a.maxAlarmHistory = m.MaxAlarmHistory
227 app.Logger.Debug("new maxActiveAlarms = %v", a.maxActiveAlarms)
228 app.Logger.Debug("new maxAlarmHistory = %v", a.maxAlarmHistory)
229 a.respondWithJSON(w, http.StatusOK, err)
233 func (a *AlarmManager) GetAlarmConfig(w http.ResponseWriter, r *http.Request) {
234 var m alarm.AlarmConfigParams
236 m.MaxActiveAlarms = a.maxActiveAlarms
237 m.MaxAlarmHistory = a.maxAlarmHistory
239 a.respondWithJSON(w, http.StatusOK, m)