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).
34 #cgo LDFLAGS: -lrmr_si
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) {
53 // Create a new Alarm instance
54 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
56 ManagedObjectId: r.moId,
57 ApplicationId: r.appId,
59 PerceivedSeverity: severity,
60 AdditionalInfo: ainfo,
61 IdentifyingInfo: iinfo,
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}
71 func (r *RICAlarm) SetManagedObjectId(mo string) {
75 func (r *RICAlarm) SetApplicationId(app string) {
80 func (r *RICAlarm) Raise(a Alarm) error {
82 defer r.mutex.Unlock()
84 m := r.NewAlarmMessage(a, AlarmActionRaise)
85 return r.sendAlarmUpdateReq(m)
89 func (r *RICAlarm) Clear(a Alarm) error {
91 defer r.mutex.Unlock()
93 m := r.NewAlarmMessage(a, AlarmActionClear)
94 return r.sendAlarmUpdateReq(m)
97 // Re-raise a RIC alarm
98 func (r *RICAlarm) Reraise(a Alarm) error {
100 defer r.mutex.Unlock()
102 m := r.NewAlarmMessage(a, AlarmActionClear)
103 if err := r.sendAlarmUpdateReq(m); err != nil {
104 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
107 return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
110 // Clear all alarms raised by the application
111 func (r *RICAlarm) ClearAll() error {
113 defer r.mutex.Unlock()
115 a := r.NewAlarm(0, SeverityDefault, "", "")
116 m := r.NewAlarmMessage(a, AlarmActionClearAll)
118 return r.sendAlarmUpdateReq(m)
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)
126 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
127 if r.rmrCtx == nil || !r.rmrReady {
128 return errors.New("RMR no ready yet!")
131 log.Println("Sending alarm: ", r.AlarmString(a))
132 payload, err := json.Marshal(a)
137 datap := C.CBytes(payload)
139 meid := C.CString("ric")
140 defer C.free(unsafe.Pointer(meid))
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))
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))
153 if err := json.Unmarshal(payload, &a); err == nil {
157 return errors.New("rmrRcv failed!")
160 func InitRMR(r *RICAlarm) error {
161 if ctx := C.rmrInit(); ctx != nil {
167 return errors.New("rmrInit failed!")