-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmanager.go
More file actions
331 lines (276 loc) · 10.3 KB
/
Copy pathmanager.go
File metadata and controls
331 lines (276 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright 2020, 2021, 2022 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package event
import (
"fmt"
"strconv"
ec "github.com/NearNodeFlash/nnf-ec/pkg/ec"
msgreg "github.com/NearNodeFlash/nnf-ec/pkg/manager-message-registry"
sf "github.com/NearNodeFlash/nnf-ec/pkg/rfsf/pkg/models"
)
// Event Manager:
//
// The Event Manager is an implementation of the Redfish / Swordfish Event Service
// It contains attributes describing the current state of the service such as status
// and retry information.
//
// Subscriptions and Subscribers:
//
// An Event Subscription represents a event destination in a client. Clients may
// subscribe to certain events that they are interested in by specifying the event type.
// When an event occurs, the Event Manager will notify any subscribers that were registerd
// to receive this event. A user supplied context is returned with the event for the
// subscriber (i.e user_arg style)
//
// Messages and Message Registries:
//
// A Message Registry is an array of Messages and their attributes. Each entry is
// defined by a MessageId, and contains a description, severity, and number and type of
// arguments, and a proposed resolution (if any). Message arguments substitute wildcard
// varabiles in the message so the client can return useful information for the user.
// Redfish Events, Errors, and Log Records have a unified format.
//
// Example:
//
// Say a Port Down event has occurred, identified by a fabric switch port.
// {
// "@odata.id": "/redfish/v1/EventService/Events/1",
// "Events": [
// {
// "EventType": "Alert",
// "EventId": "ABCD123",
// "Severity": "Warning",
// "Message": "Fabirc switch port is down",
// "MessageId": "Alert.1.0.FabircSwitchPortDown",
// "MessageArgs": [
// "Rabbit", "0", "Port 0"
// ],
// "OriginOfCondition": {
// "@odata.id": "/redfish/v1/Fabrics/Rabbit/Switches/0/Ports/0"
// }
// }
// ]
// }
//
// There should always be a Message Registry that is paried with a Message ID. In this case
// there would be a Message Registry with the following
//
// {
// "Id": "Alert.1.0.0",
// "RegistryPrefix": "Alert",
// "RegistryVersion": "1.0.0",
// "Messages": {
// "FabircSwitchPortDown": {
// "Description": "A Port is down on Fabirc %1 Switch %2 Port %3",
// "Message": "Port connectivity was lost on Fabric %1 Switch %2 Port %3",
// "Severity": "Critical",
// "NumberOfArgs": 3,
// "Resolution": "Reseat the device located in Port %3"
// }
// }
// }
//
// [end example]
//
// Client Behavior:
//
// When a client receives an event (by creating a subscription with the event service), they
// first use the MessageId to find the corresponding registry, version, and message.
// In the above example, the MessageId is "Alert.1.0.FabircSwitchPortDown". This is broken
// down by the client into three parts
// - Registry Prefix: "Alert"
// - Major / Minor Version: "1.0"
// - Message Identifier: "FabircSwitchPortDown"
//
// The client must then use that information to scan the available registries and locate the
// correct Message Registry. The Event Service contains a link of the registires in use, so
// the client can scan those registries and do a lookup.
//
// There are a bunch of registries already created by DMTF: https://redfish.dmtf.org/registries/
//
// The client then populates the message for the end user by taking the message property and
// substituting in the arguments contained within the event.
var EventManager = manager{}
const (
DefaultDeliveryRetryAttempts = 5
DefaultDeliveryRetryIntervalSeconds = 60
)
type manager struct {
subscriptions []subscription
events Events
// The maximum number of events the Event Manager maintains in the event log. The event log
// will wrap and overwrite prior events if the number of events exceeds this value.
maxEvents int
// The total number of events recorded by the Event Manager since initialization.
numEvents int
deliveryRetryAttempts int
deliveryRetryInvervalSeconds int
}
type subscription struct {
id string
prefixes []string
s Subscription
t sf.EventDestinationV190SubscriptionType
}
func (e *subscription) OdataId() string { return fmt.Sprintf("/redfish/v1/EventService/%s", e.id) }
func (e *subscription) Name() string { return fmt.Sprintf("EventSubscription %s", e.id) }
func (m *manager) Initialize() error {
m.events = make([]Event, MaxNumEvents, MaxNumEvents)
m.maxEvents = MaxNumEvents
m.numEvents = 0
m.deliveryRetryAttempts = DefaultDeliveryRetryAttempts
m.deliveryRetryInvervalSeconds = DefaultDeliveryRetryIntervalSeconds
return nil
}
// Subscribe will add the subscription to the Event Manager. When an event is published to the Event Manager
// (through the Publish() method), the Event Manager will broadcast the event to all registered subscriptions.
func (m *manager) Subscribe(s Subscription) {
m.addSubscription(s, sf.OEM_EDV190ST)
}
// Publish will publish the provided event to all interested subscriptions. It will also add the event
// to the Event Managers list of historic events. The Event must contain a valid MessageId - that is
// to say the event's MessageId must be backed by an entry in the Message Registry.
func (m *manager) Publish(e Event) {
e.Id = strconv.Itoa(m.numEvents)
m.events[m.numEvents%m.maxEvents] = e
m.numEvents++
for _, s := range m.subscriptions {
s.s.EventHandler(e)
}
}
// Publish Resource Event will publish the provided event with the resource details included in the
// event as the Origin of Condition. This is useful for any Redfish/Swordfish model that includes
// an Id or OdataId
func (m *manager) PublishResourceEvent(e Event, r Resource) {
e.OriginOfCondition = r.OdataId()
m.Publish(e)
}
func (m *manager) addSubscription(s Subscription, t sf.EventDestinationV190SubscriptionType) {
var sid = -1
for _, s := range m.subscriptions {
id, _ := strconv.Atoi(s.id)
if sid <= id {
sid = id
}
}
sid = sid + 1
m.subscriptions = append(m.subscriptions, subscription{
id: fmt.Sprintf("%d", sid),
s: s,
t: t,
})
}
func (m *manager) deleteSubscription(s *subscription) {
for subIdx, sub := range m.subscriptions {
if sub.id == s.id {
m.subscriptions = append(m.subscriptions[:subIdx], m.subscriptions[subIdx+1:]...)
break
}
}
}
func (m *manager) findSubscription(id string) *subscription {
for idx := range m.subscriptions {
if m.subscriptions[idx].id == id {
return &m.subscriptions[idx]
}
}
return nil
}
// Get
func (m *manager) Get(model *sf.EventServiceV170EventService) error {
model.Id = "EventService"
model.Name = "Event Service"
model.ServiceEnabled = true
model.DeliveryRetryAttempts = int64(m.deliveryRetryAttempts)
model.DeliveryRetryIntervalSeconds = int64(m.deliveryRetryInvervalSeconds)
model.RegistryPrefixes = make([]string, len(msgreg.RegistryFiles))
for idx, reg := range msgreg.RegistryFiles {
model.RegistryPrefixes[idx] = reg.Model.RegistryPrefix
}
model.Subscriptions = sf.OdataV4IdRef{OdataId: "/redfish/v1/EventService/Subscriptions"}
return nil
}
// EventSubscriptionsGet
func (m *manager) EventSubscriptionsGet(model *sf.EventDestinationCollectionEventDestinationCollection) error {
model.MembersodataCount = int64(len(m.subscriptions))
model.Members = make([]sf.OdataV4IdRef, model.MembersodataCount)
for idx, sub := range m.subscriptions {
model.Members[idx] = sf.OdataV4IdRef{OdataId: sub.OdataId()}
}
return nil
}
// EventSubscriptionsPost
func (m *manager) EventSubscriptionsPost(model *sf.EventDestinationV190EventDestination) error {
if (model.DeliveryRetryPolicy != sf.RETRY_FOREVER_EDV190DRP) && (model.DeliveryRetryPolicy != sf.TERMINATE_AFTER_RETRIES_EDV190DRP) {
return ec.NewErrNotAcceptable().WithCause(fmt.Sprintf("retry policy %s is not supported by the event service", string(model.DeliveryRetryPolicy)))
}
m.addSubscription(RedfishSubscription{
Context: model.Context,
Destination: model.Destination,
DeliveryRetryPolicy: model.DeliveryRetryPolicy,
}, sf.REDFISH_EVENT_EDV190ST)
return m.EventSubscriptionsSubscriptionIdGet(m.subscriptions[len(m.subscriptions)-1].id, model)
}
// EventSubscriptionsSubscriptionIdGet
func (m *manager) EventSubscriptionsSubscriptionIdGet(id string, model *sf.EventDestinationV190EventDestination) error {
s := m.findSubscription(id)
if s == nil {
return ec.NewErrNotFound().WithCause(fmt.Sprintf("subscription %s not found", id))
}
// TODO: The subscription should populate more of the model
model.Id = s.id
model.SubscriptionType = s.t
return nil
}
// EventSubscriptionsSubscriptionIdDelete
func (m *manager) EventSubscriptionsSubscriptionIdDelete(id string) error {
s := m.findSubscription(id)
if s == nil {
return ec.NewErrNotFound().WithCause(fmt.Sprintf("subscription %s not found", id))
}
m.deleteSubscription(s)
return nil
}
// EventsGet
func (m *manager) EventsGet(model *sf.EventCollectionEventCollection) error {
count := m.numEvents
start := 0
if m.numEvents > m.maxEvents {
count = m.maxEvents
start = (m.numEvents % m.maxEvents)
}
model.MembersodataCount = int64(count)
model.Members = make([]sf.OdataV4IdRef, model.MembersodataCount)
for idx := range m.events[:count] {
model.Members[idx] = sf.OdataV4IdRef{OdataId: m.events[(start+idx)%m.maxEvents].OdataId()}
}
return nil
}
// EventsEventIdGet
func (m *manager) EventsEventIdGet(id string, model *sf.EventV161Event) error {
idx, err := strconv.Atoi(id)
if err != nil {
return ec.NewErrBadRequest().WithError(err).WithCause(fmt.Sprintf("event id %s is non-integer type", id))
}
if m.numEvents < idx {
return ec.NewErrNotFound().WithCause(fmt.Sprintf("event id %s not found", id))
}
m.events[idx%m.maxEvents].CopyInto(model)
return nil
}