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).
38 #cgo LDFLAGS: -lrmr_si
44 // InitAlarm is the init routine which returns a new alarm instance.
45 // The MO and APP identities are given as a parameters.
46 // The identities are used when raising/clearing alarms, unless provided by the applications.
47 func InitAlarm(mo, id string) (*RICAlarm, error) {
51 managerUrl: ALARM_MANAGER_HTTP_URL,
54 if os.Getenv("ALARM_MANAGER_URL") != "" {
55 r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
58 if os.Getenv("ALARM_IF_RMR") != "" {
65 // Create a new Alarm instance
66 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
68 ManagedObjectId: r.moId,
69 ApplicationId: r.appId,
71 PerceivedSeverity: severity,
72 AdditionalInfo: ainfo,
73 IdentifyingInfo: iinfo,
77 // Create a new AlarmMessage instance
78 func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
79 alarmTime := time.Now().UnixNano() / 1000
80 return AlarmMessage{a, alarmAction, alarmTime}
83 func (r *RICAlarm) SetManagedObjectId(mo string) {
87 func (r *RICAlarm) SetApplicationId(app string) {
92 func (r *RICAlarm) Raise(a Alarm) error {
94 defer r.mutex.Unlock()
96 m := r.NewAlarmMessage(a, AlarmActionRaise)
97 return r.sendAlarmUpdateReq(m)
101 func (r *RICAlarm) Clear(a Alarm) error {
103 defer r.mutex.Unlock()
105 m := r.NewAlarmMessage(a, AlarmActionClear)
106 return r.sendAlarmUpdateReq(m)
109 // Re-raise a RIC alarm
110 func (r *RICAlarm) Reraise(a Alarm) error {
112 defer r.mutex.Unlock()
114 m := r.NewAlarmMessage(a, AlarmActionClear)
115 if err := r.sendAlarmUpdateReq(m); err != nil {
116 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
119 return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
122 // Clear all alarms raised by the application
123 func (r *RICAlarm) ClearAll() error {
125 defer r.mutex.Unlock()
127 a := r.NewAlarm(0, SeverityDefault, "", "")
128 m := r.NewAlarmMessage(a, AlarmActionClearAll)
130 return r.sendAlarmUpdateReq(m)
133 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
134 s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
135 return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
138 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
139 payload, err := json.Marshal(a)
141 log.Println("json.Marshal failed with error: ", err)
144 log.Println("Sending alarm: ", fmt.Sprintf("%s", payload))
146 if r.rmrCtx == nil || !r.rmrReady {
147 url := fmt.Sprintf("%s/%s", r.managerUrl, "ric/v1/alarms")
148 resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
149 if err != nil || resp == nil {
150 return fmt.Errorf("Unable to send alarm: %v", err)
152 log.Printf("Alarm posted to %s [status=%d]", url, resp.StatusCode)
156 datap := C.CBytes(payload)
158 meid := C.CString("ric")
159 defer C.free(unsafe.Pointer(meid))
161 if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
162 log.Println("rmrSend failed with error: ", state)
163 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
168 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
169 if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
170 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
172 if err := json.Unmarshal(payload, &a); err == nil {
176 return errors.New("rmrRcv failed!")
179 func InitRMR(r *RICAlarm) error {
180 // Setup static RT for alarm system
181 endpoint := ALARM_MANAGER_RMR_URL
182 if r.moId == "my-pod" {
183 endpoint = "localhost:4560"
184 } else if r.moId == "my-pod-lib" {
185 endpoint = "localhost:4588"
188 alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", endpoint)
189 alarmRTFile := "/tmp/alarm.rt"
191 if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil {
192 log.Println("ioutil.WriteFile failed with error: ", err)
196 os.Setenv("RMR_SEED_RT", alarmRTFile)
197 os.Setenv("RMR_RTG_SVC", "-1")
199 if ctx := C.rmrInit(); ctx != nil {
205 return errors.New("rmrInit failed!")