Update README, etc
[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 )
31
32 /*
33 #cgo CFLAGS: -I../
34 #cgo LDFLAGS: -lrmr_nng -lnng
35
36 #include "utils.h"
37 */
38 import "C"
39
40 // InitAlarm is the init routine which returns a new alarm instance.
41 // The MO and APP identities are given as a parameters.
42 // The identities are used when raising/clearing alarms, unless provided by the applications.
43 func InitAlarm(mo, id string) (*RICAlarm, error) {
44         if ctx := C.rmrInit(); ctx != nil {
45                 r := &RICAlarm{
46                         moId:   mo,
47                         appId:  id,
48                         rmrCtx: ctx,
49                 }
50
51                 return r, nil
52         }
53
54         return nil, errors.New("rmrInit failed!")
55 }
56
57 // Create a new Alarm instance
58 func (r *RICAlarm) NewAlarm(sp int, severity Severity, ainfo, iinfo string) Alarm {
59         return Alarm{
60                 ManagedObjectId:   r.moId,
61                 ApplicationId:     r.appId,
62                 SpecificProblem:   sp,
63                 PerceivedSeverity: severity,
64                 AdditionalInfo:    ainfo,
65                 IdentifyingInfo:   iinfo,
66         }
67 }
68
69 // Create a new AlarmMessage instance
70 func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
71         alarmTime := time.Now().UnixNano() / 1000
72         return AlarmMessage{a, alarmAction, alarmTime}
73 }
74
75 func (r *RICAlarm) SetManagedObjectId(mo string) {
76         r.moId = mo
77 }
78
79 func (r *RICAlarm) SetApplicationId(app string) {
80         r.appId = app
81 }
82
83 // Raise a RIC alarm
84 func (r *RICAlarm) Raise(a Alarm) error {
85         r.mutex.Lock()
86         defer r.mutex.Unlock()
87
88         m := r.NewAlarmMessage(a, AlarmActionRaise)
89         return r.sendAlarmUpdateReq(m)
90 }
91
92 // Clear a RIC alarm
93 func (r *RICAlarm) Clear(a Alarm) error {
94         r.mutex.Lock()
95         defer r.mutex.Unlock()
96
97         m := r.NewAlarmMessage(a, AlarmActionClear)
98         return r.sendAlarmUpdateReq(m)
99 }
100
101 // Re-raise a RIC alarm
102 func (r *RICAlarm) Reraise(a Alarm) error {
103         r.mutex.Lock()
104         defer r.mutex.Unlock()
105
106         m := r.NewAlarmMessage(a, AlarmActionClear)
107         if err := r.sendAlarmUpdateReq(m); err != nil {
108                 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
109         }
110
111         return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
112 }
113
114 // Clear all alarms raised by the application
115 func (r *RICAlarm) ClearAll() error {
116         r.mutex.Lock()
117         defer r.mutex.Unlock()
118
119         a := r.NewAlarm(0, SeverityDefault, "", "")
120         m := r.NewAlarmMessage(a, AlarmActionClearAll)
121
122         return r.sendAlarmUpdateReq(m)
123 }
124
125 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
126         s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
127         return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
128 }
129
130 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
131         log.Println("Sending alarm: ", r.AlarmString(a))
132
133         payload, err := json.Marshal(a)
134         if err != nil {
135                 return err
136         }
137
138         datap := C.CBytes(payload)
139         defer C.free(datap)
140         meid := C.CString("ric")
141         defer C.free(unsafe.Pointer(meid))
142
143         if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
144                 log.Println("rmrSend failed with error: ", state)
145                 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
146         }
147         return nil
148 }
149
150 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
151         if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
152                 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
153                 a := AlarmMessage{}
154                 if err := json.Unmarshal(payload, &a); err == nil {
155                         cb(a)
156                 }
157         }
158         return errors.New("rmrRcv failed!")
159 }