7a09083f3bf53993f141ba06cca1e95825556b96
[ric-plt/alarm-go.git] / manager / cmd / restapi.go
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"
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"
28         "net/http"
29         "strconv"
30         "time"
31 )
32
33 func (a *AlarmManager) respondWithError(w http.ResponseWriter, code int, message string) {
34         a.respondWithJSON(w, code, map[string]string{"error": message})
35 }
36
37 func (a *AlarmManager) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
38         w.Header().Set("Content-Type", "application/json")
39         w.WriteHeader(code)
40         if payload != nil {
41                 response, _ := json.Marshal(payload)
42                 w.Write(response)
43         }
44 }
45
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)
49 }
50
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)
54 }
55
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)
59         }
60 }
61
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)
65         }
66 }
67
68 func (a *AlarmManager) SetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
69
70         app.Logger.Debug("POST arrived for creating alarm definition ")
71         /* If body is nil then return error */
72         if r.Body == nil {
73                 app.Logger.Error("POST - body is empty")
74                 a.respondWithError(w, http.StatusBadRequest, "No data in request body.")
75                 return
76         }
77         defer r.Body.Close()
78
79         /* Parameters are available. Check if they are valid */
80         var alarmDefinitions RicAlarmDefinitions
81         err := json.NewDecoder(r.Body).Decode(&alarmDefinitions)
82         if err != nil {
83                 app.Logger.Error("POST - received alarm definition  parameters are invalid - " + err.Error())
84                 a.respondWithError(w, http.StatusBadRequest, "Invalid data in request body.")
85                 return
86         }
87
88         for _, alarmDefinition := range alarmDefinitions.AlarmDefinitions {
89                 _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId]
90                 if exists {
91                         app.Logger.Error("POST - alarm definition already exists for %v", alarmDefinition.AlarmId)
92                 } else {
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                         alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion
99                 }
100         }
101
102         a.respondWithJSON(w, http.StatusOK, nil)
103         return
104 }
105
106 func (a *AlarmManager) DeleteAlarmDefinition(w http.ResponseWriter, r *http.Request) {
107         pathParams := mux.Vars(r)
108         alarmId, alarmIdok := pathParams["alarmId"]
109         if alarmIdok {
110                 if ialarmId, err := strconv.Atoi(alarmId); err == nil {
111                         delete(alarm.RICAlarmDefinitions, ialarmId)
112                         app.Logger.Debug("DELETE - alarm definition deleted for alarmId %v", ialarmId)
113                 } else {
114                         app.Logger.Error("DELETE - alarmId string to int conversion failed %v", alarmId)
115                         a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
116                         return
117                 }
118         } else {
119                 app.Logger.Error("DELETE - alarmId does not exist %v", alarmId)
120                 a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
121                 return
122
123         }
124 }
125
126 func (a *AlarmManager) GetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
127         var ricAlarmDefinitions RicAlarmDefinitions
128         pathParams := mux.Vars(r)
129         alarmId, alarmIdok := pathParams["alarmId"]
130         if alarmIdok {
131                 if ialarmId, err := strconv.Atoi(alarmId); err == nil {
132                         alarmDefinition, ok := alarm.RICAlarmDefinitions[ialarmId]
133                         if ok {
134                                 app.Logger.Debug("Successfully returned alarm defintion for alarm id %v", ialarmId)
135                                 a.respondWithJSON(w, http.StatusOK, alarmDefinition)
136                                 return
137
138                         } else {
139                                 app.Logger.Error("Requested alarm id not found %v", ialarmId)
140                                 a.respondWithError(w, http.StatusBadRequest, "Non existent alarmId")
141                                 return
142                         }
143                 } else {
144                         app.Logger.Error("alarmId string to int conversion failed %v", alarmId)
145                         a.respondWithError(w, http.StatusBadRequest, "Invalid alarmId")
146                         return
147                 }
148         } else {
149                 app.Logger.Debug("GET arrived for all alarm definitions ")
150                 for _, alarmDefinition := range alarm.RICAlarmDefinitions {
151                         ricAlarmDefinitions.AlarmDefinitions = append(ricAlarmDefinitions.AlarmDefinitions, alarmDefinition)
152                 }
153                 app.Logger.Debug("Successfully returned all alarm definitions")
154                 a.respondWithJSON(w, http.StatusOK, ricAlarmDefinitions)
155         }
156 }
157
158 func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error {
159         app.Logger.Info("doAction: request received = %t", isRaiseAlarm)
160
161         if r.Body == nil {
162                 app.Logger.Error("Error: Invalid message body!")
163                 return nil
164         }
165         defer r.Body.Close()
166
167         var m alarm.AlarmMessage
168         if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
169                 app.Logger.Error("json.NewDecoder failed: %v", err)
170                 return err
171         }
172
173         if m.Alarm.ManagedObjectId == "" || m.Alarm.ApplicationId == "" || m.AlarmAction == "" {
174                 app.Logger.Error("Error: Mandatory parameters missing!")
175                 return nil
176         }
177
178         if m.AlarmTime == 0 {
179                 m.AlarmTime = time.Now().UnixNano()
180         }
181
182         _, err := a.ProcessAlarm(&m)
183         return err
184 }
185
186 func (a *AlarmManager) HandleViaRmr(d alarm.Alarm, isRaiseAlarm bool) error {
187         alarmClient, err := alarm.InitAlarm(d.ManagedObjectId, d.ApplicationId)
188         if err != nil {
189                 app.Logger.Error("json.NewDecoder failed: %v", err)
190                 return err
191         }
192
193         alarmData := alarmClient.NewAlarm(d.SpecificProblem, d.PerceivedSeverity, d.AdditionalInfo, d.IdentifyingInfo)
194         if isRaiseAlarm {
195                 alarmClient.Raise(alarmData)
196         } else {
197                 alarmClient.Clear(alarmData)
198         }
199
200         return nil
201 }
202
203 func (a *AlarmManager) SetAlarmConfig(w http.ResponseWriter, r *http.Request) {
204         var m alarm.AlarmConfigParams
205         if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
206                 app.Logger.Error("json.NewDecoder failed: %v", err)
207         } else {
208                 a.maxActiveAlarms = m.MaxActiveAlarms
209                 a.maxAlarmHistory = m.MaxAlarmHistory
210                 app.Logger.Debug("new maxActiveAlarms = %v", a.maxActiveAlarms)
211                 app.Logger.Debug("new maxAlarmHistory = %v", a.maxAlarmHistory)
212                 a.respondWithJSON(w, http.StatusOK, err)
213         }
214 }
215
216 func (a *AlarmManager) GetAlarmConfig(w http.ResponseWriter, r *http.Request) {
217         var m alarm.AlarmConfigParams
218
219         m.MaxActiveAlarms = a.maxActiveAlarms
220         m.MaxAlarmHistory = a.maxAlarmHistory
221
222         a.respondWithJSON(w, http.StatusOK, m)
223         return
224 }