LN0739_FM_FR8: relaxing the active alarm and alarm history restrictions
[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         "net/http"
26         "time"
27
28         "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
29         app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
30 )
31
32 func (a *AlarmManager) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
33         w.Header().Set("Content-Type", "application/json")
34         w.WriteHeader(code)
35         if payload != nil {
36                 response, _ := json.Marshal(payload)
37                 w.Write(response)
38         }
39 }
40
41 func (a *AlarmManager) GetActiveAlarms(w http.ResponseWriter, r *http.Request) {
42         app.Logger.Info("GetActiveAlarms: %+v", a.activeAlarms)
43         a.respondWithJSON(w, http.StatusOK, a.activeAlarms)
44 }
45
46 func (a *AlarmManager) GetAlarmHistory(w http.ResponseWriter, r *http.Request) {
47         app.Logger.Info("GetAlarmHistory: %+v", a.alarmHistory)
48         a.respondWithJSON(w, http.StatusOK, a.alarmHistory)
49 }
50
51 func (a *AlarmManager) RaiseAlarm(w http.ResponseWriter, r *http.Request) {
52         if err := a.doAction(w, r, true); err != nil {
53                 a.respondWithJSON(w, http.StatusOK, err)
54         }
55 }
56
57 func (a *AlarmManager) ClearAlarm(w http.ResponseWriter, r *http.Request) {
58         if err := a.doAction(w, r, false); err != nil {
59                 a.respondWithJSON(w, http.StatusOK, err)
60         }
61 }
62
63 func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error {
64         app.Logger.Info("doAction: request received = %t", isRaiseAlarm)
65
66         if r.Body == nil {
67                 app.Logger.Error("Error: Invalid message body!")
68                 return nil
69         }
70         defer r.Body.Close()
71
72         var m alarm.AlarmMessage
73         if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
74                 app.Logger.Error("json.NewDecoder failed: %v", err)
75                 return err
76         }
77
78         if m.Alarm.ManagedObjectId == "" || m.Alarm.ApplicationId == "" || m.AlarmAction == "" {
79                 app.Logger.Error("Error: Mandatory parameters missing!")
80                 return nil
81         }
82
83         if m.AlarmTime == 0 {
84                 m.AlarmTime = time.Now().UnixNano()
85         }
86
87         _, err := a.ProcessAlarm(&m)
88         return err
89 }
90
91 func (a *AlarmManager) HandleViaRmr(d alarm.Alarm, isRaiseAlarm bool) error {
92         alarmClient, err := alarm.InitAlarm(d.ManagedObjectId, d.ApplicationId)
93         if err != nil {
94                 app.Logger.Error("json.NewDecoder failed: %v", err)
95                 return err
96         }
97
98         alarmData := alarmClient.NewAlarm(d.SpecificProblem, d.PerceivedSeverity, d.AdditionalInfo, d.IdentifyingInfo)
99         if isRaiseAlarm {
100                 alarmClient.Raise(alarmData)
101         } else {
102                 alarmClient.Clear(alarmData)
103         }
104
105         return nil
106 }
107
108 func (a *AlarmManager) SetAlarmConfig(w http.ResponseWriter, r *http.Request) {
109         var m alarm.AlarmConfigParams
110         if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
111                 app.Logger.Error("json.NewDecoder failed: %v", err)
112         } else {
113                 a.maxActiveAlarms = m.MaxActiveAlarms
114                 a.maxAlarmHistory = m.MaxAlarmHistory
115                 app.Logger.Debug("new maxActiveAlarms = %v", a.maxActiveAlarms)
116                 app.Logger.Debug("new maxAlarmHistory = %v", a.maxAlarmHistory)
117                 a.respondWithJSON(w, http.StatusOK, err)
118         }
119 }
120
121 func (a *AlarmManager) GetAlarmConfig(w http.ResponseWriter, r *http.Request) {
122         var m alarm.AlarmConfigParams
123
124         m.MaxActiveAlarms = a.maxActiveAlarms
125         m.MaxAlarmHistory = a.maxAlarmHistory
126
127         a.respondWithJSON(w, http.StatusOK, m)
128         return
129 }