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) respondWithError(w http.ResponseWriter, code int, message string) {
34 a.respondWithJSON(w, code, map[string]string{"error": message})
37 func (a *AlarmManager) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
38 w.Header().Set("Content-Type", "application/json")
41 response, _ := json.Marshal(payload)
46 func (a *AlarmManager) GetActiveAlarms(w http.ResponseWriter, r *http.Request) {
47 app.Logger.Info("GetActiveAlarms: %+v", a.activeAlarms)
48 a.respondWithJSON(w, http.StatusOK, a.activeAlarms)
51 func (a *AlarmManager) GetAlarmHistory(w http.ResponseWriter, r *http.Request) {
52 app.Logger.Info("GetAlarmHistory: %+v", a.alarmHistory)
53 a.respondWithJSON(w, http.StatusOK, a.alarmHistory)
56 func (a *AlarmManager) RaiseAlarm(w http.ResponseWriter, r *http.Request) {
57 if err := a.doAction(w, r, true); err != nil {
58 a.respondWithJSON(w, http.StatusOK, err)
62 func (a *AlarmManager) ClearAlarm(w http.ResponseWriter, r *http.Request) {
63 if err := a.doAction(w, r, false); err != nil {
64 a.respondWithJSON(w, http.StatusOK, err)
68 func (a *AlarmManager) SetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
70 app.Logger.Debug("POST arrived for creating alarm definition ")
71 /* If body is nil then return error */
73 app.Logger.Error("POST - body is empty")
74 a.respondWithError(w, http.StatusBadRequest, "No data in request body.")
79 /* Parameters are available. Check if they are valid */
80 var alarmDefinitions RicAlarmDefinitions
81 err := json.NewDecoder(r.Body).Decode(&alarmDefinitions)
83 app.Logger.Error("POST - received alarm definition parameters are invalid - " + err.Error())
84 a.respondWithError(w, http.StatusBadRequest, "Invalid data in request body.")
88 for _, alarmDefinition := range alarmDefinitions.AlarmDefinitions {
89 _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId]
91 app.Logger.Error("POST - alarm definition already exists for %v", alarmDefinition.AlarmId)
93 ricAlarmDefintion := new(alarm.AlarmDefinition)
94 ricAlarmDefintion.AlarmId = alarmDefinition.AlarmId
95 ricAlarmDefintion.AlarmText = alarmDefinition.AlarmText
96 ricAlarmDefintion.EventType = alarmDefinition.EventType
97 ricAlarmDefintion.OperationInstructions = alarmDefinition.OperationInstructions
98 ricAlarmDefintion.RaiseDelay = alarmDefinition.RaiseDelay
99 ricAlarmDefintion.ClearDelay = alarmDefinition.ClearDelay
100 alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion
101 app.Logger.Debug("POST - alarm definition added for alarm id %v", alarmDefinition.AlarmId)
105 a.respondWithJSON(w, http.StatusOK, nil)
109 func (a *AlarmManager) DeleteAlarmDefinition(w http.ResponseWriter, r *http.Request) {
110 pathParams := mux.Vars(r)
111 alarmId, alarmIdok := pathParams["alarmId"]
113 if ialarmId, err := strconv.Atoi(alarmId); err == nil {
114 delete(alarm.RICAlarmDefinitions, ialarmId)
115 app.Logger.Debug("DELETE - alarm definition deleted for alarmId %v", ialarmId)
117 app.Logger.Error("DELETE - alarmId string to int conversion failed %v", alarmId)
118 a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
122 app.Logger.Error("DELETE - alarmId does not exist %v", alarmId)
123 a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
129 func (a *AlarmManager) GetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
130 var ricAlarmDefinitions RicAlarmDefinitions
131 pathParams := mux.Vars(r)
132 alarmId, alarmIdok := pathParams["alarmId"]
134 if ialarmId, err := strconv.Atoi(alarmId); err == nil {
135 alarmDefinition, ok := alarm.RICAlarmDefinitions[ialarmId]
137 app.Logger.Debug("Successfully returned alarm defintion for alarm id %v", ialarmId)
138 a.respondWithJSON(w, http.StatusOK, alarmDefinition)
142 app.Logger.Error("Requested alarm id not found %v", ialarmId)
143 a.respondWithError(w, http.StatusBadRequest, "Non existent alarmId")
147 app.Logger.Error("alarmId string to int conversion failed %v", alarmId)
148 a.respondWithError(w, http.StatusBadRequest, "Invalid alarmId")
152 app.Logger.Debug("GET arrived for all alarm definitions ")
153 for _, alarmDefinition := range alarm.RICAlarmDefinitions {
154 ricAlarmDefinitions.AlarmDefinitions = append(ricAlarmDefinitions.AlarmDefinitions, alarmDefinition)
156 app.Logger.Debug("Successfully returned all alarm definitions")
157 a.respondWithJSON(w, http.StatusOK, ricAlarmDefinitions)
161 func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error {
162 app.Logger.Info("doAction: request received = %t", isRaiseAlarm)
165 app.Logger.Error("Error: Invalid message body!")
170 var m alarm.AlarmMessage
171 if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
172 app.Logger.Error("json.NewDecoder failed: %v", err)
176 if m.Alarm.ManagedObjectId == "" || m.Alarm.ApplicationId == "" || m.AlarmAction == "" {
177 app.Logger.Error("Error: Mandatory parameters missing!")
181 if m.AlarmTime == 0 {
182 m.AlarmTime = time.Now().UnixNano()
185 _, err := a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}})
189 func (a *AlarmManager) HandleViaRmr(d alarm.Alarm, isRaiseAlarm bool) error {
190 alarmClient, err := alarm.InitAlarm(d.ManagedObjectId, d.ApplicationId)
192 app.Logger.Error("json.NewDecoder failed: %v", err)
196 alarmData := alarmClient.NewAlarm(d.SpecificProblem, d.PerceivedSeverity, d.AdditionalInfo, d.IdentifyingInfo)
198 alarmClient.Raise(alarmData)
200 alarmClient.Clear(alarmData)
206 func (a *AlarmManager) SetAlarmConfig(w http.ResponseWriter, r *http.Request) {
207 var m alarm.AlarmConfigParams
208 if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
209 app.Logger.Error("json.NewDecoder failed: %v", err)
211 a.maxActiveAlarms = m.MaxActiveAlarms
212 a.maxAlarmHistory = m.MaxAlarmHistory
213 app.Logger.Debug("new maxActiveAlarms = %v", a.maxActiveAlarms)
214 app.Logger.Debug("new maxAlarmHistory = %v", a.maxAlarmHistory)
215 a.respondWithJSON(w, http.StatusOK, err)
219 func (a *AlarmManager) GetAlarmConfig(w http.ResponseWriter, r *http.Request) {
220 var m alarm.AlarmConfigParams
222 m.MaxActiveAlarms = a.maxActiveAlarms
223 m.MaxAlarmHistory = a.maxAlarmHistory
225 a.respondWithJSON(w, http.StatusOK, m)