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).
35 #cgo LDFLAGS: -lrmr_nng -lnng
41 // Severity for alarms
44 // Possible values for Severity
46 SeverityUnspecified Severity = "UNSPECIFIED"
47 SeverityCritical Severity = "CRITICAL"
48 SeverityMajor Severity = "MAJOR"
49 SeverityMinor Severity = "MINOR"
50 SeverityWarning Severity = "WARNING"
51 SeverityNormal Severity = "CLEARED"
52 SeverityDefault Severity = "DEFAULT"
55 // Alarm object - see README for more information
57 ManagedObjectId string `json:"managedObjectId"`
58 ApplicationId string `json:"applicationId"`
59 SpecificProblem int `json:"specificProblem"`
60 PerceivedSeverity Severity `json:"perceivedSeverity"`
61 AdditionalInfo string `json:"additionalInfo"`
62 IdentifyingInfo string `json:"identifyingInfo"`
66 type AlarmAction string
68 // Possible values for alarm actions
70 AlarmActionRaise AlarmAction = "RAISE"
71 AlarmActionClear AlarmAction = "CLEAR"
72 AlarmActionReraise AlarmAction = "RERAISE"
73 AlarmActionClearAll AlarmAction = "CLEARALL"
76 type AlarmMessage struct {
82 // RICAlarm is an alarm instance
83 type RICAlarm struct {
90 // InitAlarm is the init routine which returns a new alarm instance.
91 // The MO and APP identities are given as a parameters.
92 // The identities are used when raising/clearing alarms, unless provided by the applications.
93 func InitAlarm(mo, id string) (*RICAlarm, error) {
94 if ctx := C.rmrInit(); ctx != nil {
104 return nil, errors.New("rmrInit failed!")
107 // Create a new Alarm instance
108 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
110 ManagedObjectId: r.moId,
111 ApplicationId: r.appId,
113 PerceivedSeverity: severity,
114 AdditionalInfo: ainfo,
115 IdentifyingInfo: iinfo,
119 // Create a new AlarmMessage instance
120 func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
121 alarmTime := time.Now().UnixNano() / 1000
122 return AlarmMessage{a, alarmAction, alarmTime}
126 func (r *RICAlarm) Raise(a Alarm) error {
128 defer r.mutex.Unlock()
130 m := r.NewAlarmMessage(a, AlarmActionRaise)
131 return r.SendMessage(m)
135 func (r *RICAlarm) Clear(a Alarm) error {
137 defer r.mutex.Unlock()
139 m := r.NewAlarmMessage(a, AlarmActionClear)
140 return r.SendMessage(m)
143 // Re-raise a RIC alarm
144 func (r *RICAlarm) Reraise(a Alarm) error {
146 defer r.mutex.Unlock()
148 m := r.NewAlarmMessage(a, AlarmActionReraise)
149 return r.SendMessage(m)
152 // Clear all alarms raised by the application
153 func (r *RICAlarm) ClearAll() error {
155 defer r.mutex.Unlock()
157 a := r.NewAlarm(0, SeverityDefault, "", "")
158 m := r.NewAlarmMessage(a, AlarmActionClearAll)
160 return r.SendMessage(m)
163 // Internal functions
164 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
165 s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
166 return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
169 func (r *RICAlarm) SendMessage(a AlarmMessage) error {
170 log.Println("Sending alarm:", r.AlarmString(a))
172 payload, err := json.Marshal(a)
177 datap := C.CBytes(payload)
179 meid := C.CString("ric")
180 defer C.free(unsafe.Pointer(meid))
182 if state := C.rmrSend(r.rmrCtx, 1234, datap, C.int(len(payload)), meid); state != C.RMR_OK {
183 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
188 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
189 if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
190 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
192 if err := json.Unmarshal(payload, &a); err == nil {
196 return errors.New("rmrRcv failed!")