Static RT for alarm
[ric-plt/alarm-go.git] / alarm / alarm.go
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 (
24         "encoding/json"
25         "errors"
26         "fmt"
27         "log"
28         "time"
29         "unsafe"
30         "os"
31         "io/ioutil"
32 )
33
34 /*
35 #cgo CFLAGS: -I../
36 #cgo LDFLAGS: -lrmr_si
37
38 #include "utils.h"
39 */
40 import "C"
41
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) {
46         r := &RICAlarm{
47                 moId:  mo,
48                 appId: id,
49         }
50         go InitRMR(r)
51
52         return r, nil
53 }
54
55 // Create a new Alarm instance
56 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
57         return Alarm{
58                 ManagedObjectId:   r.moId,
59                 ApplicationId:     r.appId,
60                 SpecificProblem:   sp,
61                 PerceivedSeverity: severity,
62                 AdditionalInfo:    ainfo,
63                 IdentifyingInfo:   iinfo,
64         }
65 }
66
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}
71 }
72
73 func (r *RICAlarm) SetManagedObjectId(mo string) {
74         r.moId = mo
75 }
76
77 func (r *RICAlarm) SetApplicationId(app string) {
78         r.appId = app
79 }
80
81 // Raise a RIC alarm
82 func (r *RICAlarm) Raise(a Alarm) error {
83         r.mutex.Lock()
84         defer r.mutex.Unlock()
85
86         m := r.NewAlarmMessage(a, AlarmActionRaise)
87         return r.sendAlarmUpdateReq(m)
88 }
89
90 // Clear a RIC alarm
91 func (r *RICAlarm) Clear(a Alarm) error {
92         r.mutex.Lock()
93         defer r.mutex.Unlock()
94
95         m := r.NewAlarmMessage(a, AlarmActionClear)
96         return r.sendAlarmUpdateReq(m)
97 }
98
99 // Re-raise a RIC alarm
100 func (r *RICAlarm) Reraise(a Alarm) error {
101         r.mutex.Lock()
102         defer r.mutex.Unlock()
103
104         m := r.NewAlarmMessage(a, AlarmActionClear)
105         if err := r.sendAlarmUpdateReq(m); err != nil {
106                 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
107         }
108
109         return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
110 }
111
112 // Clear all alarms raised by the application
113 func (r *RICAlarm) ClearAll() error {
114         r.mutex.Lock()
115         defer r.mutex.Unlock()
116
117         a := r.NewAlarm(0, SeverityDefault, "", "")
118         m := r.NewAlarmMessage(a, AlarmActionClearAll)
119
120         return r.sendAlarmUpdateReq(m)
121 }
122
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)
126 }
127
128 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
129         if r.rmrCtx == nil || !r.rmrReady {
130                 return errors.New("RMR no ready yet!")
131         }
132
133         log.Println("Sending alarm: ", r.AlarmString(a))
134         payload, err := json.Marshal(a)
135         if err != nil {
136                 return err
137         }
138
139         datap := C.CBytes(payload)
140         defer C.free(datap)
141         meid := C.CString("ric")
142         defer C.free(unsafe.Pointer(meid))
143
144         if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
145                 log.Println("rmrSend failed with error: ", state)
146                 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
147         }
148         return nil
149 }
150
151 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
152         if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
153                 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
154                 a := AlarmMessage{}
155                 if err := json.Unmarshal(payload, &a); err == nil {
156                         cb(a)
157                 }
158         }
159         return errors.New("rmrRcv failed!")
160 }
161
162 func InitRMR(r *RICAlarm) error {
163         // Setup static RT for alarm system
164         endpoint := "service-ricplt-alarmadapter-rmr.ricplt:4560"
165         if r.moId == "my-pod" {
166                 endpoint = "localhost:4560"
167         } else if r.moId == "my-pod-lib" {
168                 endpoint = "localhost:4588"
169         }
170
171         alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", endpoint)
172         alarmRTFile := "/tmp/alarm.rt"
173
174         if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil {
175                 log.Println("ioutil.WriteFile failed with error: ", err)
176                 return err
177         }
178
179         os.Setenv("RMR_SEED_RT", alarmRTFile)
180         os.Setenv("RMR_RTG_SVC", "-1")
181
182         if ctx := C.rmrInit(); ctx != nil {
183                 r.rmrCtx = ctx
184                 r.rmrReady = true
185                 return nil
186         }
187
188         return errors.New("rmrInit failed!")
189 }