Add UT for REST interface & code refactor
[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 // Raise a RIC alarm
76 func (r *RICAlarm) Raise(a Alarm) error {
77         r.mutex.Lock()
78         defer r.mutex.Unlock()
79
80         m := r.NewAlarmMessage(a, AlarmActionRaise)
81         return r.sendAlarmUpdateReq(m)
82 }
83
84 // Clear a RIC alarm
85 func (r *RICAlarm) Clear(a Alarm) error {
86         r.mutex.Lock()
87         defer r.mutex.Unlock()
88
89         m := r.NewAlarmMessage(a, AlarmActionClear)
90         return r.sendAlarmUpdateReq(m)
91 }
92
93 // Re-raise a RIC alarm
94 func (r *RICAlarm) Reraise(a Alarm) error {
95         r.mutex.Lock()
96         defer r.mutex.Unlock()
97
98         m := r.NewAlarmMessage(a, AlarmActionClear)
99         if err := r.sendAlarmUpdateReq(m); err != nil {
100                 return errors.New(fmt.Sprintf("Reraise failed: %v", err))
101         }
102
103         return r.sendAlarmUpdateReq(r.NewAlarmMessage(a, AlarmActionRaise))
104 }
105
106 // Clear all alarms raised by the application
107 func (r *RICAlarm) ClearAll() error {
108         r.mutex.Lock()
109         defer r.mutex.Unlock()
110
111         a := r.NewAlarm(0, SeverityDefault, "", "")
112         m := r.NewAlarmMessage(a, AlarmActionClearAll)
113
114         return r.sendAlarmUpdateReq(m)
115 }
116
117 func (r *RICAlarm) AlarmString(a AlarmMessage) string {
118         s := "MOId=%s AppId=%s SP=%d severity=%s IA=%s"
119         return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
120 }
121
122 func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
123         log.Println("Sending alarm: ", r.AlarmString(a))
124
125         payload, err := json.Marshal(a)
126         if err != nil {
127                 return err
128         }
129
130         datap := C.CBytes(payload)
131         defer C.free(datap)
132         meid := C.CString("ric")
133         defer C.free(unsafe.Pointer(meid))
134
135         if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
136                 log.Println("rmrSend failed with error: ", state)
137                 return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
138         }
139         return nil
140 }
141
142 func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
143         if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
144                 payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
145                 a := AlarmMessage{}
146                 if err := json.Unmarshal(payload, &a); err == nil {
147                         cb(a)
148                 }
149         }
150         return errors.New("rmrRcv failed!")
151 }