blob: 36d4ea376247f3aaa47263a6dfa8d1966774ec76 [file] [log] [blame]
Mohamed Abukar3e038152020-03-04 10:01:45 +02001/*
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
21package alarm
22
23import (
Mohamed Abukar540ceee2020-09-09 08:07:40 +030024 "bytes"
Mohamed Abukar3e038152020-03-04 10:01:45 +020025 "encoding/json"
26 "errors"
27 "fmt"
Mohamed Abukar540ceee2020-09-09 08:07:40 +030028 "io/ioutil"
Mohamed Abukar3e038152020-03-04 10:01:45 +020029 "log"
Mohamed Abukar540ceee2020-09-09 08:07:40 +030030 "net/http"
31 "os"
Mohamed Abukar3e038152020-03-04 10:01:45 +020032 "time"
33 "unsafe"
34)
35
36/*
37#cgo CFLAGS: -I../
Mohamed Abukareac44512020-03-31 09:46:04 +030038#cgo LDFLAGS: -lrmr_si
Mohamed Abukar3e038152020-03-04 10:01:45 +020039
40#include "utils.h"
41*/
42import "C"
43
Mohamed Abukar3e038152020-03-04 10:01:45 +020044// InitAlarm is the init routine which returns a new alarm instance.
45// The MO and APP identities are given as a parameters.
46// The identities are used when raising/clearing alarms, unless provided by the applications.
47func InitAlarm(mo, id string) (*RICAlarm, error) {
Mohamed Abukareac44512020-03-31 09:46:04 +030048 r := &RICAlarm{
Juha Hyttinencfb2daa2022-10-03 09:52:01 +030049 moId: mo,
50 appId: id,
51 managerUrl: ALARM_MANAGER_HTTP_URL,
52 rmrEndpoint: ALARM_MANAGER_RMR_URL,
Mohamed Abukar3e038152020-03-04 10:01:45 +020053 }
Mohamed Abukar540ceee2020-09-09 08:07:40 +030054
Juha Hyttinen281d0602021-12-23 10:14:40 +020055 //
56 // http service information (used in case of no rmr connectivity)
57 //
Abukar Mohamed121e8b62020-09-18 11:41:33 +000058 if os.Getenv("ALARM_MANAGER_URL") != "" {
59 r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
Mohamed Abukar540ceee2020-09-09 08:07:40 +030060 }
61
Juha Hyttinencfb2daa2022-10-03 09:52:01 +030062 if os.Getenv("ALARM_MANAGER_SERVICE_NAME") != "" && os.Getenv("ALARM_MANAGER_SERVICE_PORT") != "" {
63 r.rmrEndpoint = fmt.Sprintf("%s:%s", os.Getenv("ALARM_MANAGER_SERVICE_NAME"), os.Getenv("ALARM_MANAGER_SERVICE_PORT"))
Mohamed Abukarf5a8e712020-10-19 16:58:17 +030064 }
vipin6f73fa32020-10-06 06:51:53 +000065
Juha Hyttinencfb2daa2022-10-03 09:52:01 +030066 if os.Getenv("ALARM_IF_RMR") == "" {
67 if r.moId == "my-pod" {
68 r.rmrEndpoint = "127.0.0.1:4560"
69 } else if r.moId == "my-pod-lib" {
70 r.rmrEndpoint = "127.0.0.1:4588"
71 }
72 }
73
74 go InitRMR(r)
75
Mohamed Abukareac44512020-03-31 09:46:04 +030076 return r, nil
Mohamed Abukar3e038152020-03-04 10:01:45 +020077}
78
79// Create a new Alarm instance
80func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
81 return Alarm{
82 ManagedObjectId: r.moId,
83 ApplicationId: r.appId,
84 SpecificProblem: sp,
85 PerceivedSeverity: severity,
Mohamed Abukar3e038152020-03-04 10:01:45 +020086 IdentifyingInfo: iinfo,
Juha Hyttinen281d0602021-12-23 10:14:40 +020087 AdditionalInfo: ainfo,
Mohamed Abukar3e038152020-03-04 10:01:45 +020088 }
89}
90
91// Create a new AlarmMessage instance
92func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
Anssi Mannila2be5ac52020-09-22 12:17:05 +030093 alarmTime := time.Now().UnixNano()
Mohamed Abukar3e038152020-03-04 10:01:45 +020094 return AlarmMessage{a, alarmAction, alarmTime}
95}
96
Mohamed Abukarb2f29a82020-03-17 09:31:55 +020097func (r *RICAlarm) SetManagedObjectId(mo string) {
98 r.moId = mo
99}
100
101func (r *RICAlarm) SetApplicationId(app string) {
102 r.appId = app
103}
104
Mohamed Abukar3e038152020-03-04 10:01:45 +0200105// Raise a RIC alarm
106func (r *RICAlarm) Raise(a Alarm) error {
107 r.mutex.Lock()
108 defer r.mutex.Unlock()
109
110 m := r.NewAlarmMessage(a, AlarmActionRaise)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200111 return r.sendAlarmUpdateReq(m)
Mohamed Abukar3e038152020-03-04 10:01:45 +0200112}
113
114// Clear a RIC alarm
115func (r *RICAlarm) Clear(a Alarm) error {
116 r.mutex.Lock()
117 defer r.mutex.Unlock()
118
119 m := r.NewAlarmMessage(a, AlarmActionClear)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200120 return r.sendAlarmUpdateReq(m)
Mohamed Abukar3e038152020-03-04 10:01:45 +0200121}
122
123// Re-raise a RIC alarm
124func (r *RICAlarm) Reraise(a Alarm) error {
125 r.mutex.Lock()
126 defer r.mutex.Unlock()
127
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200128 m := r.NewAlarmMessage(a, AlarmActionClear)
129 if err := r.sendAlarmUpdateReq(m); err != nil {
130 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
131 }
132
133 return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
Mohamed Abukar3e038152020-03-04 10:01:45 +0200134}
135
136// Clear all alarms raised by the application
137func (r *RICAlarm) ClearAll() error {
138 r.mutex.Lock()
139 defer r.mutex.Unlock()
140
141 a := r.NewAlarm(0, SeverityDefault, "", "")
142 m := r.NewAlarmMessage(a, AlarmActionClearAll)
143
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200144 return r.sendAlarmUpdateReq(m)
Mohamed Abukar3e038152020-03-04 10:01:45 +0200145}
146
Mohamed Abukar3e038152020-03-04 10:01:45 +0200147func (r *RICAlarm) AlarmString(a AlarmMessage) string {
148 s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
149 return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
150}
151
Juha Hyttinencfb2daa2022-10-03 09:52:01 +0300152func (r *RICAlarm) sendAlarmUpdateReqWithHttp(payload []byte) error {
153 url := fmt.Sprintf("%s/%s", r.managerUrl, "ric/v1/alarms")
154 resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
155 if err != nil || resp == nil {
156 return fmt.Errorf("HttpError=Post failed with error: %v", err)
157 }
158 log.Printf("Alarm posted to %s [status=%d]", url, resp.StatusCode)
159 return nil
160}
161
162func (r *RICAlarm) sendAlarmUpdateReqWithRmr(payload []byte) error {
163 if r.rmrCtx == nil || !r.rmrReady {
164 return fmt.Errorf("RmrError=rmr not ready")
165 }
166 datap := C.CBytes(payload)
167 defer C.free(datap)
168 meid := C.CString("ric")
169 defer C.free(unsafe.Pointer(meid))
170
171 if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
Juha Hyttinen4a7716f2022-10-06 09:49:08 +0300172 return errors.New(fmt.Sprintf("RmrError=rmrSend via %s failed with error: %d", r.rmrEndpoint, state))
Juha Hyttinencfb2daa2022-10-03 09:52:01 +0300173 }
174 log.Printf("Alarm sent via rmr to %s", r.rmrEndpoint)
175 return nil
176}
177
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200178func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
Anssi Mannila500878a2020-09-30 08:57:04 +0300179
Mohamed Abukar3e038152020-03-04 10:01:45 +0200180 payload, err := json.Marshal(a)
181 if err != nil {
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300182 log.Println("json.Marshal failed with error: ", err)
Mohamed Abukar3e038152020-03-04 10:01:45 +0200183 return err
184 }
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300185 log.Println("Sending alarm: ", fmt.Sprintf("%s", payload))
Mohamed Abukar3e038152020-03-04 10:01:45 +0200186
Juha Hyttinencfb2daa2022-10-03 09:52:01 +0300187 // --
188 // Try rmr sending
189 // --
190 err = r.sendAlarmUpdateReqWithRmr(payload)
191
192 // --
193 // Try http posting if rmr is not done for some reason: rmrSend error, rmr not initialized yet etc.
194 // --
195 if err != nil {
196 if httperr := r.sendAlarmUpdateReqWithHttp(payload); httperr != nil {
197 err = fmt.Errorf("%s and %s", err.Error(), httperr.Error())
198 } else {
199 err = nil
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300200 }
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300201 }
202
Juha Hyttinencfb2daa2022-10-03 09:52:01 +0300203 if err != nil {
204 log.Printf("Alarm sent error %s", err.Error())
Mohamed Abukar3e038152020-03-04 10:01:45 +0200205 }
Juha Hyttinencfb2daa2022-10-03 09:52:01 +0300206 return err
Mohamed Abukar3e038152020-03-04 10:01:45 +0200207}
208
209func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
210 if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
211 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
212 a := AlarmMessage{}
213 if err := json.Unmarshal(payload, &a); err == nil {
214 cb(a)
215 }
216 }
217 return errors.New("rmrRcv failed!")
218}
Mohamed Abukareac44512020-03-31 09:46:04 +0300219
Juha Hyttinencfb2daa2022-10-03 09:52:01 +0300220func InitRMR(r *RICAlarm) error {
Mohamed Abukarccce3af2020-04-10 21:43:21 +0300221 // Setup static RT for alarm system
Juha Hyttinencfb2daa2022-10-03 09:52:01 +0300222 alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", r.rmrEndpoint)
Mohamed Abukarccce3af2020-04-10 21:43:21 +0300223 alarmRTFile := "/tmp/alarm.rt"
224
225 if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil {
226 log.Println("ioutil.WriteFile failed with error: ", err)
227 return err
228 }
229
230 os.Setenv("RMR_SEED_RT", alarmRTFile)
231 os.Setenv("RMR_RTG_SVC", "-1")
232
Mohamed Abukareac44512020-03-31 09:46:04 +0300233 if ctx := C.rmrInit(); ctx != nil {
234 r.rmrCtx = ctx
235 r.rmrReady = true
236 return nil
237 }
238
239 return errors.New("rmrInit failed!")
240}
Anssi Mannila500878a2020-09-30 08:57:04 +0300241
242func (r *RICAlarm) IsRMRReady() bool {
243 return r.rmrReady
244}