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).
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"
32 func (a *AlarmAdapter) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
33 w.Header().Set("Content-Type", "application/json")
36 response, _ := json.Marshal(payload)
41 func (a *AlarmAdapter) GetActiveAlarms(w http.ResponseWriter, r *http.Request) {
42 app.Logger.Info("GetActiveAlarms: %+v", a.activeAlarms)
43 a.respondWithJSON(w, http.StatusOK, a.activeAlarms)
46 func (a *AlarmAdapter) GetAlarmHistory(w http.ResponseWriter, r *http.Request) {
47 app.Logger.Info("GetAlarmHistory: %+v", a.alarmHistory)
48 a.respondWithJSON(w, http.StatusOK, a.alarmHistory)
51 func (a *AlarmAdapter) RaiseAlarm(w http.ResponseWriter, r *http.Request) {
52 if err := a.doAction(w, r, true); err != nil {
53 a.respondWithJSON(w, http.StatusOK, err)
57 func (a *AlarmAdapter) ClearAlarm(w http.ResponseWriter, r *http.Request) {
58 if err := a.doAction(w, r, false); err != nil {
59 a.respondWithJSON(w, http.StatusOK, err)
63 func (a *AlarmAdapter) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error {
64 app.Logger.Info("doAction: request received = %t", isRaiseAlarm)
67 app.Logger.Error("Error: Invalid message body!")
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)
78 if m.Alarm.ManagedObjectId == "" || m.Alarm.ApplicationId == "" || m.AlarmAction == "" {
79 app.Logger.Error("Error: Mandatory parameters missing!")
84 m.AlarmTime = time.Now().UnixNano() / 1000
87 _, err := a.ProcessAlarm(&m)
91 func (a *AlarmAdapter) HandleViaRmr(d alarm.Alarm, isRaiseAlarm bool) error {
92 alarmClient, err := alarm.InitAlarm(d.ManagedObjectId, d.ApplicationId)
94 app.Logger.Error("json.NewDecoder failed: %v", err)
98 alarmData := alarmClient.NewAlarm(d.SpecificProblem, d.PerceivedSeverity, d.AdditionalInfo, d.IdentifyingInfo)
100 alarmClient.Raise(alarmData)
102 alarmClient.Clear(alarmData)