blob: d097c7371925bc0bd7b604a045daacf7d89ed6e1 [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{
Mohamed Abukar540ceee2020-09-09 08:07:40 +030049 moId: mo,
50 appId: id,
Abukar Mohamed121e8b62020-09-18 11:41:33 +000051 managerUrl: ALARM_MANAGER_HTTP_URL,
Mohamed Abukar3e038152020-03-04 10:01:45 +020052 }
Mohamed Abukar540ceee2020-09-09 08:07:40 +030053
Abukar Mohamed121e8b62020-09-18 11:41:33 +000054 if os.Getenv("ALARM_MANAGER_URL") != "" {
55 r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
Mohamed Abukar540ceee2020-09-09 08:07:40 +030056 }
57
58 if os.Getenv("ALARM_IF_RMR") != "" {
59 go InitRMR(r)
60 }
Mohamed Abukar3e038152020-03-04 10:01:45 +020061
Mohamed Abukareac44512020-03-31 09:46:04 +030062 return r, nil
Mohamed Abukar3e038152020-03-04 10:01:45 +020063}
64
65// Create a new Alarm instance
66func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
67 return Alarm{
68 ManagedObjectId: r.moId,
69 ApplicationId: r.appId,
70 SpecificProblem: sp,
71 PerceivedSeverity: severity,
72 AdditionalInfo: ainfo,
73 IdentifyingInfo: iinfo,
74 }
75}
76
77// Create a new AlarmMessage instance
78func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
Anssi Mannila2be5ac52020-09-22 12:17:05 +030079 alarmTime := time.Now().UnixNano()
Mohamed Abukar3e038152020-03-04 10:01:45 +020080 return AlarmMessage{a, alarmAction, alarmTime}
81}
82
Mohamed Abukarb2f29a82020-03-17 09:31:55 +020083func (r *RICAlarm) SetManagedObjectId(mo string) {
84 r.moId = mo
85}
86
87func (r *RICAlarm) SetApplicationId(app string) {
88 r.appId = app
89}
90
Mohamed Abukar3e038152020-03-04 10:01:45 +020091// Raise a RIC alarm
92func (r *RICAlarm) Raise(a Alarm) error {
93 r.mutex.Lock()
94 defer r.mutex.Unlock()
95
96 m := r.NewAlarmMessage(a, AlarmActionRaise)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020097 return r.sendAlarmUpdateReq(m)
Mohamed Abukar3e038152020-03-04 10:01:45 +020098}
99
100// Clear a RIC alarm
101func (r *RICAlarm) Clear(a Alarm) error {
102 r.mutex.Lock()
103 defer r.mutex.Unlock()
104
105 m := r.NewAlarmMessage(a, AlarmActionClear)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200106 return r.sendAlarmUpdateReq(m)
Mohamed Abukar3e038152020-03-04 10:01:45 +0200107}
108
109// Re-raise a RIC alarm
110func (r *RICAlarm) Reraise(a Alarm) error {
111 r.mutex.Lock()
112 defer r.mutex.Unlock()
113
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200114 m := r.NewAlarmMessage(a, AlarmActionClear)
115 if err := r.sendAlarmUpdateReq(m); err != nil {
116 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
117 }
118
119 return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
Mohamed Abukar3e038152020-03-04 10:01:45 +0200120}
121
122// Clear all alarms raised by the application
123func (r *RICAlarm) ClearAll() error {
124 r.mutex.Lock()
125 defer r.mutex.Unlock()
126
127 a := r.NewAlarm(0, SeverityDefault, "", "")
128 m := r.NewAlarmMessage(a, AlarmActionClearAll)
129
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200130 return r.sendAlarmUpdateReq(m)
Mohamed Abukar3e038152020-03-04 10:01:45 +0200131}
132
Mohamed Abukar3e038152020-03-04 10:01:45 +0200133func (r *RICAlarm) AlarmString(a AlarmMessage) string {
134 s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
135 return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
136}
137
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200138func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
Mohamed Abukar3e038152020-03-04 10:01:45 +0200139 payload, err := json.Marshal(a)
140 if err != nil {
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300141 log.Println("json.Marshal failed with error: ", err)
Mohamed Abukar3e038152020-03-04 10:01:45 +0200142 return err
143 }
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300144 log.Println("Sending alarm: ", fmt.Sprintf("%s", payload))
Mohamed Abukar3e038152020-03-04 10:01:45 +0200145
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300146 if r.rmrCtx == nil || !r.rmrReady {
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000147 url := fmt.Sprintf("%s/%s", r.managerUrl, "ric/v1/alarms")
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300148 resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
149 if err != nil || resp == nil {
150 return fmt.Errorf("Unable to send alarm: %v", err)
151 }
152 log.Printf("Alarm posted to %s [status=%d]", url, resp.StatusCode)
153 return nil
154 }
155
Mohamed Abukar3e038152020-03-04 10:01:45 +0200156 datap := C.CBytes(payload)
157 defer C.free(datap)
158 meid := C.CString("ric")
159 defer C.free(unsafe.Pointer(meid))
160
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200161 if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
162 log.Println("rmrSend failed with error: ", state)
Mohamed Abukar3e038152020-03-04 10:01:45 +0200163 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
164 }
165 return nil
166}
167
168func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
169 if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
170 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
171 a := AlarmMessage{}
172 if err := json.Unmarshal(payload, &a); err == nil {
173 cb(a)
174 }
175 }
176 return errors.New("rmrRcv failed!")
177}
Mohamed Abukareac44512020-03-31 09:46:04 +0300178
179func InitRMR(r *RICAlarm) error {
Mohamed Abukarccce3af2020-04-10 21:43:21 +0300180 // Setup static RT for alarm system
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000181 endpoint := ALARM_MANAGER_RMR_URL
Mohamed Abukarccce3af2020-04-10 21:43:21 +0300182 if r.moId == "my-pod" {
Mohamed Abukar44513722020-09-22 09:17:24 +0300183 endpoint = "127.0.0.1:4560"
Mohamed Abukarccce3af2020-04-10 21:43:21 +0300184 } else if r.moId == "my-pod-lib" {
Mohamed Abukar44513722020-09-22 09:17:24 +0300185 endpoint = "127.0.0.1:4588"
Mohamed Abukarccce3af2020-04-10 21:43:21 +0300186 }
187
188 alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", endpoint)
189 alarmRTFile := "/tmp/alarm.rt"
190
191 if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil {
192 log.Println("ioutil.WriteFile failed with error: ", err)
193 return err
194 }
195
196 os.Setenv("RMR_SEED_RT", alarmRTFile)
197 os.Setenv("RMR_RTG_SVC", "-1")
198
Mohamed Abukareac44512020-03-31 09:46:04 +0300199 if ctx := C.rmrInit(); ctx != nil {
200 r.rmrCtx = ctx
201 r.rmrReady = true
202 return nil
203 }
204
205 return errors.New("rmrInit failed!")
206}