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_nng -lnng
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 if ctx := C.rmrInit(); ctx != nil {
54 return nil, errors.New("rmrInit failed!")
57 // Create a new Alarm instance
58 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
60 ManagedObjectId: r.moId,
61 ApplicationId: r.appId,
63 PerceivedSeverity: severity,
64 AdditionalInfo: ainfo,
65 IdentifyingInfo: iinfo,
69 // Create a new AlarmMessage instance
70 func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
71 alarmTime := time.Now().UnixNano() / 1000
72 return AlarmMessage{a, alarmAction, alarmTime}
76 func (r *RICAlarm) Raise(a Alarm) error {
78 defer r.mutex.Unlock()
80 m := r.NewAlarmMessage(a, AlarmActionRaise)
81 return r.sendAlarmUpdateReq(m)
85 func (r *RICAlarm) Clear(a Alarm) error {
87 defer r.mutex.Unlock()
89 m := r.NewAlarmMessage(a, AlarmActionClear)
90 return r.sendAlarmUpdateReq(m)
93 // Re-raise a RIC alarm
94 func (r *RICAlarm) Reraise(a Alarm) error {
96 defer r.mutex.Unlock()
98 m := r.NewAlarmMessage(a, AlarmActionClear)
99 if err := r.sendAlarmUpdateReq(m); err != nil {
100 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
103 return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
106 // Clear all alarms raised by the application
107 func (r *RICAlarm) ClearAll() error {
109 defer r.mutex.Unlock()
111 a := r.NewAlarm(0, SeverityDefault, "", "")
112 m := r.NewAlarmMessage(a, AlarmActionClearAll)
114 return r.sendAlarmUpdateReq(m)
117 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
118 s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
119 return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
122 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
123 log.Println("Sending alarm: ", r.AlarmString(a))
125 payload, err := json.Marshal(a)
130 datap := C.CBytes(payload)
132 meid := C.CString("ric")
133 defer C.free(unsafe.Pointer(meid))
135 if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
136 log.Println("rmrSend failed with error: ", state)
137 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
142 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
143 if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
144 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
146 if err := json.Unmarshal(payload, &a); err == nil {
150 return errors.New("rmrRcv failed!")