Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 AT&T Intellectual Property. |
| 3 | * Copyright (c) 2020 Nokia. |
| 4 | * |
| 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 |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 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. |
| 16 | * |
| 17 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | * platform project (RICP). |
| 19 | */ |
| 20 | |
| 21 | package alarm |
| 22 | |
| 23 | import ( |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 24 | "bytes" |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 25 | "encoding/json" |
| 26 | "errors" |
| 27 | "fmt" |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 28 | "io/ioutil" |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 29 | "log" |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 30 | "net/http" |
| 31 | "os" |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 32 | "time" |
| 33 | "unsafe" |
| 34 | ) |
| 35 | |
| 36 | /* |
| 37 | #cgo CFLAGS: -I../ |
Mohamed Abukar | eac4451 | 2020-03-31 09:46:04 +0300 | [diff] [blame] | 38 | #cgo LDFLAGS: -lrmr_si |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 39 | |
| 40 | #include "utils.h" |
| 41 | */ |
| 42 | import "C" |
| 43 | |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 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) { |
Mohamed Abukar | eac4451 | 2020-03-31 09:46:04 +0300 | [diff] [blame] | 48 | r := &RICAlarm{ |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 49 | moId: mo, |
| 50 | appId: id, |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 51 | managerUrl: ALARM_MANAGER_HTTP_URL, |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 52 | } |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 53 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 54 | if os.Getenv("ALARM_MANAGER_URL") != "" { |
| 55 | r.managerUrl = os.Getenv("ALARM_MANAGER_URL") |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 56 | } |
| 57 | |
Anssi Mannila | 500878a | 2020-09-30 08:57:04 +0300 | [diff] [blame] | 58 | if os.Getenv("ALARM_IF_RMR") == "" { |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame^] | 59 | go InitRMR(r, "") |
| 60 | } else { |
| 61 | go InitRMR(r, ALARM_MANAGER_RMR_URL) |
| 62 | } |
| 63 | |
Mohamed Abukar | eac4451 | 2020-03-31 09:46:04 +0300 | [diff] [blame] | 64 | return r, nil |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Create a new Alarm instance |
| 68 | func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm { |
| 69 | return Alarm{ |
| 70 | ManagedObjectId: r.moId, |
| 71 | ApplicationId: r.appId, |
| 72 | SpecificProblem: sp, |
| 73 | PerceivedSeverity: severity, |
| 74 | AdditionalInfo: ainfo, |
| 75 | IdentifyingInfo: iinfo, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Create a new AlarmMessage instance |
| 80 | func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage { |
Anssi Mannila | 2be5ac5 | 2020-09-22 12:17:05 +0300 | [diff] [blame] | 81 | alarmTime := time.Now().UnixNano() |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 82 | return AlarmMessage{a, alarmAction, alarmTime} |
| 83 | } |
| 84 | |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame] | 85 | func (r *RICAlarm) SetManagedObjectId(mo string) { |
| 86 | r.moId = mo |
| 87 | } |
| 88 | |
| 89 | func (r *RICAlarm) SetApplicationId(app string) { |
| 90 | r.appId = app |
| 91 | } |
| 92 | |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 93 | // Raise a RIC alarm |
| 94 | func (r *RICAlarm) Raise(a Alarm) error { |
| 95 | r.mutex.Lock() |
| 96 | defer r.mutex.Unlock() |
| 97 | |
| 98 | m := r.NewAlarmMessage(a, AlarmActionRaise) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 99 | return r.sendAlarmUpdateReq(m) |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Clear a RIC alarm |
| 103 | func (r *RICAlarm) Clear(a Alarm) error { |
| 104 | r.mutex.Lock() |
| 105 | defer r.mutex.Unlock() |
| 106 | |
| 107 | m := r.NewAlarmMessage(a, AlarmActionClear) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 108 | return r.sendAlarmUpdateReq(m) |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Re-raise a RIC alarm |
| 112 | func (r *RICAlarm) Reraise(a Alarm) error { |
| 113 | r.mutex.Lock() |
| 114 | defer r.mutex.Unlock() |
| 115 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 116 | m := r.NewAlarmMessage(a, AlarmActionClear) |
| 117 | if err := r.sendAlarmUpdateReq(m); err != nil { |
| 118 | return errors.New(fmt.Sprintf("Reraise failed: %v", err)) |
| 119 | } |
| 120 | |
| 121 | return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise)) |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Clear all alarms raised by the application |
| 125 | func (r *RICAlarm) ClearAll() error { |
| 126 | r.mutex.Lock() |
| 127 | defer r.mutex.Unlock() |
| 128 | |
| 129 | a := r.NewAlarm(0, SeverityDefault, "", "") |
| 130 | m := r.NewAlarmMessage(a, AlarmActionClearAll) |
| 131 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 132 | return r.sendAlarmUpdateReq(m) |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 135 | func (r *RICAlarm) AlarmString(a AlarmMessage) string { |
| 136 | s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s" |
| 137 | return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo) |
| 138 | } |
| 139 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 140 | func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error { |
Anssi Mannila | 500878a | 2020-09-30 08:57:04 +0300 | [diff] [blame] | 141 | |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 142 | payload, err := json.Marshal(a) |
| 143 | if err != nil { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 144 | log.Println("json.Marshal failed with error: ", err) |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 145 | return err |
| 146 | } |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 147 | log.Println("Sending alarm: ", fmt.Sprintf("%s", payload)) |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 148 | |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 149 | if r.rmrCtx == nil || !r.rmrReady { |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 150 | url := fmt.Sprintf("%s/%s", r.managerUrl, "ric/v1/alarms") |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 151 | resp, err := http.Post(url, "application/json", bytes.NewReader(payload)) |
| 152 | if err != nil || resp == nil { |
| 153 | return fmt.Errorf("Unable to send alarm: %v", err) |
| 154 | } |
| 155 | log.Printf("Alarm posted to %s [status=%d]", url, resp.StatusCode) |
| 156 | return nil |
| 157 | } |
| 158 | |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 159 | datap := C.CBytes(payload) |
| 160 | defer C.free(datap) |
| 161 | meid := C.CString("ric") |
| 162 | defer C.free(unsafe.Pointer(meid)) |
| 163 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 164 | if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK { |
| 165 | log.Println("rmrSend failed with error: ", state) |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 166 | return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state)) |
| 167 | } |
Anssi Mannila | 500878a | 2020-09-30 08:57:04 +0300 | [diff] [blame] | 168 | |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 169 | return nil |
| 170 | } |
| 171 | |
| 172 | func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error { |
| 173 | if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil { |
| 174 | payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len)) |
| 175 | a := AlarmMessage{} |
| 176 | if err := json.Unmarshal(payload, &a); err == nil { |
| 177 | cb(a) |
| 178 | } |
| 179 | } |
| 180 | return errors.New("rmrRcv failed!") |
| 181 | } |
Mohamed Abukar | eac4451 | 2020-03-31 09:46:04 +0300 | [diff] [blame] | 182 | |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame^] | 183 | func InitRMR(r *RICAlarm, endpoint string) error { |
Mohamed Abukar | ccce3af | 2020-04-10 21:43:21 +0300 | [diff] [blame] | 184 | // Setup static RT for alarm system |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame^] | 185 | if endpoint == "" { |
| 186 | if r.moId == "my-pod" { |
| 187 | endpoint = "127.0.0.1:4560" |
| 188 | } else if r.moId == "my-pod-lib" { |
| 189 | endpoint = "127.0.0.1:4588" |
| 190 | } |
| 191 | } |
Mohamed Abukar | ccce3af | 2020-04-10 21:43:21 +0300 | [diff] [blame] | 192 | |
| 193 | alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", endpoint) |
| 194 | alarmRTFile := "/tmp/alarm.rt" |
| 195 | |
| 196 | if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil { |
| 197 | log.Println("ioutil.WriteFile failed with error: ", err) |
| 198 | return err |
| 199 | } |
| 200 | |
| 201 | os.Setenv("RMR_SEED_RT", alarmRTFile) |
| 202 | os.Setenv("RMR_RTG_SVC", "-1") |
| 203 | |
Mohamed Abukar | eac4451 | 2020-03-31 09:46:04 +0300 | [diff] [blame] | 204 | if ctx := C.rmrInit(); ctx != nil { |
| 205 | r.rmrCtx = ctx |
| 206 | r.rmrReady = true |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | return errors.New("rmrInit failed!") |
| 211 | } |
Anssi Mannila | 500878a | 2020-09-30 08:57:04 +0300 | [diff] [blame] | 212 | |
| 213 | func (r *RICAlarm) IsRMRReady() bool { |
| 214 | return r.rmrReady |
| 215 | } |