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,
52 rmrEndpoint: ALARM_MANAGER_RMR_URL,
56 // http service information (used in case of no rmr connectivity)
58 if os.Getenv("ALARM_MANAGER_URL") != "" {
59 r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
62 if os.Getenv("ALARM_MANAGER_SERVICE_NAME") != "" && os.Getenv("ALARM_MANAGER_SERVICE_PORT") != "" {
63 r.rmrEndpoint = fmt.Sprintf("%s:%s", os.Getenv("ALARM_MANAGER_SERVICE_NAME"), os.Getenv("ALARM_MANAGER_SERVICE_PORT"))
66 if os.Getenv("ALARM_IF_RMR") == "" {
67 if r.moId == "my-pod" {
68 r.rmrEndpoint = "127.0.0.1:4560"
69 } else if r.moId == "my-pod-lib" {
70 r.rmrEndpoint = "127.0.0.1:4588"
79 // Create a new Alarm instance
80 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
82 ManagedObjectId: r.moId,
83 ApplicationId: r.appId,
85 PerceivedSeverity: severity,
86 IdentifyingInfo: iinfo,
87 AdditionalInfo: ainfo,
91 // Create a new AlarmMessage instance
92 func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
93 alarmTime := time.Now().UnixNano()
94 return AlarmMessage{a, alarmAction, alarmTime}
97 func (r *RICAlarm) SetManagedObjectId(mo string) {
101 func (r *RICAlarm) SetApplicationId(app string) {
106 func (r *RICAlarm) Raise(a Alarm) error {
108 defer r.mutex.Unlock()
110 m := r.NewAlarmMessage(a, AlarmActionRaise)
111 return r.sendAlarmUpdateReq(m)
115 func (r *RICAlarm) Clear(a Alarm) error {
117 defer r.mutex.Unlock()
119 m := r.NewAlarmMessage(a, AlarmActionClear)
120 return r.sendAlarmUpdateReq(m)
123 // Re-raise a RIC alarm
124 func (r *RICAlarm) Reraise(a Alarm) error {
126 defer r.mutex.Unlock()
128 m := r.NewAlarmMessage(a, AlarmActionClear)
129 if err := r.sendAlarmUpdateReq(m); err != nil {
130 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
133 return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
136 // Clear all alarms raised by the application
137 func (r *RICAlarm) ClearAll() error {
139 defer r.mutex.Unlock()
141 a := r.NewAlarm(0, SeverityDefault, "", "")
142 m := r.NewAlarmMessage(a, AlarmActionClearAll)
144 return r.sendAlarmUpdateReq(m)
147 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
148 s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
149 return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
152 func (r *RICAlarm) sendAlarmUpdateReqWithHttp(payload []byte) error {
153 url := fmt.Sprintf("%s/%s", r.managerUrl, "ric/v1/alarms")
154 resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
155 if err != nil || resp == nil {
156 return fmt.Errorf("HttpError=Post failed with error: %v", err)
158 log.Printf("Alarm posted to %s [status=%d]", url, resp.StatusCode)
162 func (r *RICAlarm) sendAlarmUpdateReqWithRmr(payload []byte) error {
163 if r.rmrCtx == nil || !r.rmrReady {
164 return fmt.Errorf("RmrError=rmr not ready")
166 datap := C.CBytes(payload)
168 meid := C.CString("ric")
169 defer C.free(unsafe.Pointer(meid))
171 if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
172 return errors.New(fmt.Sprintf("RmrError=rmrSend via %s failed with error: %d", r.rmrEndpoint, state))
174 log.Printf("Alarm sent via rmr to %s", r.rmrEndpoint)
178 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
180 payload, err := json.Marshal(a)
182 log.Println("json.Marshal failed with error: ", err)
185 log.Println("Sending alarm: ", fmt.Sprintf("%s", payload))
190 err = r.sendAlarmUpdateReqWithRmr(payload)
193 // Try http posting if rmr is not done for some reason: rmrSend error, rmr not initialized yet etc.
196 if httperr := r.sendAlarmUpdateReqWithHttp(payload); httperr != nil {
197 err = fmt.Errorf("%s and %s", err.Error(), httperr.Error())
204 log.Printf("Alarm sent error %s", err.Error())
209 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
210 if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
211 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
213 if err := json.Unmarshal(payload, &a); err == nil {
217 return errors.New("rmrRcv failed!")
220 func InitRMR(r *RICAlarm) error {
221 // Setup static RT for alarm system
222 alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", r.rmrEndpoint)
223 alarmRTFile := "/tmp/alarm.rt"
225 if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil {
226 log.Println("ioutil.WriteFile failed with error: ", err)
230 os.Setenv("RMR_SEED_RT", alarmRTFile)
231 os.Setenv("RMR_RTG_SVC", "-1")
233 if ctx := C.rmrInit(); ctx != nil {
239 return errors.New("rmrInit failed!")
242 func (r *RICAlarm) IsRMRReady() bool {