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).
36 #cgo LDFLAGS: -lrmr_si
42 // InitAlarm is the init routine which returns a new alarm instance.
43 // The MO and APP identities are given as a parameters.
44 // The identities are used when raising/clearing alarms, unless provided by the applications.
45 func InitAlarm(mo, id string) (*RICAlarm, error) {
55 // Create a new Alarm instance
56 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
58 ManagedObjectId: r.moId,
59 ApplicationId: r.appId,
61 PerceivedSeverity: severity,
62 AdditionalInfo: ainfo,
63 IdentifyingInfo: iinfo,
67 // Create a new AlarmMessage instance
68 func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
69 alarmTime := time.Now().UnixNano() / 1000
70 return AlarmMessage{a, alarmAction, alarmTime}
73 func (r *RICAlarm) SetManagedObjectId(mo string) {
77 func (r *RICAlarm) SetApplicationId(app string) {
82 func (r *RICAlarm) Raise(a Alarm) error {
84 defer r.mutex.Unlock()
86 m := r.NewAlarmMessage(a, AlarmActionRaise)
87 return r.sendAlarmUpdateReq(m)
91 func (r *RICAlarm) Clear(a Alarm) error {
93 defer r.mutex.Unlock()
95 m := r.NewAlarmMessage(a, AlarmActionClear)
96 return r.sendAlarmUpdateReq(m)
99 // Re-raise a RIC alarm
100 func (r *RICAlarm) Reraise(a Alarm) error {
102 defer r.mutex.Unlock()
104 m := r.NewAlarmMessage(a, AlarmActionClear)
105 if err := r.sendAlarmUpdateReq(m); err != nil {
106 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
109 return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
112 // Clear all alarms raised by the application
113 func (r *RICAlarm) ClearAll() error {
115 defer r.mutex.Unlock()
117 a := r.NewAlarm(0, SeverityDefault, "", "")
118 m := r.NewAlarmMessage(a, AlarmActionClearAll)
120 return r.sendAlarmUpdateReq(m)
123 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
124 s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
125 return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
128 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
129 if r.rmrCtx == nil || !r.rmrReady {
130 return errors.New("RMR no ready yet!")
133 log.Printf("Alarm message: %+v\n", a)
134 log.Println("Sending alarm: ", r.AlarmString(a))
135 payload, err := json.Marshal(a)
140 log.Println("JSON payload: ", fmt.Sprintf("%s", payload))
141 datap := C.CBytes(payload)
143 meid := C.CString("ric")
144 defer C.free(unsafe.Pointer(meid))
146 if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
147 log.Println("rmrSend failed with error: ", state)
148 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
153 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
154 if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
155 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
157 if err := json.Unmarshal(payload, &a); err == nil {
161 return errors.New("rmrRcv failed!")
164 func InitRMR(r *RICAlarm) error {
165 // Setup static RT for alarm system
166 endpoint := "service-ricplt-alarmadapter-rmr.ricplt:4560"
167 if r.moId == "my-pod" {
168 endpoint = "localhost:4560"
169 } else if r.moId == "my-pod-lib" {
170 endpoint = "localhost:4588"
173 alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", endpoint)
174 alarmRTFile := "/tmp/alarm.rt"
176 if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil {
177 log.Println("ioutil.WriteFile failed with error: ", err)
181 os.Setenv("RMR_SEED_RT", alarmRTFile)
182 os.Setenv("RMR_RTG_SVC", "-1")
184 if ctx := C.rmrInit(); ctx != nil {
190 return errors.New("rmrInit failed!")