Non-blocking RMR init ...
[ric-plt/alarm-go.git] / alarm / alarm.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 alarm
22
23 import (
24         "encoding/json"
25         "errors"
26         "fmt"
27         "log"
28         "time"
29         "unsafe"
30 )
31
32 /*
33 #cgo CFLAGS: -I../
34 #cgo LDFLAGS: -lrmr_si
35
36 #include "utils.h"
37 */
38 import "C"
39
40 // InitAlarm is the init routine which returns a new alarm instance.
41 // The MO and APP identities are given as a parameters.
42 // The identities are used when raising/clearing alarms, unless provided by the applications.
43 func InitAlarm(mo, id string) (*RICAlarm, error) {
44         r := &RICAlarm{
45                 moId:  mo,
46                 appId: id,
47         }
48         go InitRMR(r)
49
50         return r, nil
51 }
52
53 // Create a new Alarm instance
54 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
55         return Alarm{
56                 ManagedObjectId:   r.moId,
57                 ApplicationId:     r.appId,
58                 SpecificProblem:   sp,
59                 PerceivedSeverity: severity,
60                 AdditionalInfo:    ainfo,
61                 IdentifyingInfo:   iinfo,
62         }
63 }
64
65 // Create a new AlarmMessage instance
66 func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
67         alarmTime := time.Now().UnixNano() / 1000
68         return AlarmMessage{a, alarmAction, alarmTime}
69 }
70
71 func (r *RICAlarm) SetManagedObjectId(mo string) {
72         r.moId = mo
73 }
74
75 func (r *RICAlarm) SetApplicationId(app string) {
76         r.appId = app
77 }
78
79 // Raise a RIC alarm
80 func (r *RICAlarm) Raise(a Alarm) error {
81         r.mutex.Lock()
82         defer r.mutex.Unlock()
83
84         m := r.NewAlarmMessage(a, AlarmActionRaise)
85         return r.sendAlarmUpdateReq(m)
86 }
87
88 // Clear a RIC alarm
89 func (r *RICAlarm) Clear(a Alarm) error {
90         r.mutex.Lock()
91         defer r.mutex.Unlock()
92
93         m := r.NewAlarmMessage(a, AlarmActionClear)
94         return r.sendAlarmUpdateReq(m)
95 }
96
97 // Re-raise a RIC alarm
98 func (r *RICAlarm) Reraise(a Alarm) error {
99         r.mutex.Lock()
100         defer r.mutex.Unlock()
101
102         m := r.NewAlarmMessage(a, AlarmActionClear)
103         if err := r.sendAlarmUpdateReq(m); err != nil {
104                 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
105         }
106
107         return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
108 }
109
110 // Clear all alarms raised by the application
111 func (r *RICAlarm) ClearAll() error {
112         r.mutex.Lock()
113         defer r.mutex.Unlock()
114
115         a := r.NewAlarm(0, SeverityDefault, "", "")
116         m := r.NewAlarmMessage(a, AlarmActionClearAll)
117
118         return r.sendAlarmUpdateReq(m)
119 }
120
121 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
122         s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
123         return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
124 }
125
126 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
127         if r.rmrCtx == nil || !r.rmrReady {
128                 return errors.New("RMR no ready yet!")
129         }
130
131         log.Println("Sending alarm: ", r.AlarmString(a))
132         payload, err := json.Marshal(a)
133         if err != nil {
134                 return err
135         }
136
137         datap := C.CBytes(payload)
138         defer C.free(datap)
139         meid := C.CString("ric")
140         defer C.free(unsafe.Pointer(meid))
141
142         if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
143                 log.Println("rmrSend failed with error: ", state)
144                 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
145         }
146         return nil
147 }
148
149 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
150         if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
151                 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
152                 a := AlarmMessage{}
153                 if err := json.Unmarshal(payload, &a); err == nil {
154                         cb(a)
155                 }
156         }
157         return errors.New("rmrRcv failed!")
158 }
159
160 func InitRMR(r *RICAlarm) error {
161         if ctx := C.rmrInit(); ctx != nil {
162                 r.rmrCtx = ctx
163                 r.rmrReady = true
164                 return nil
165         }
166
167         return errors.New("rmrInit failed!")
168 }