-
Notifications
You must be signed in to change notification settings - Fork 56
/
reset.go
312 lines (290 loc) · 12.4 KB
/
reset.go
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
//(C) Copyright [2020] Hewlett Packard Enterprise Development LP
//
//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 system
import (
"context"
"encoding/json"
"fmt"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/errors"
l "github.com/ODIM-Project/ODIM/lib-utilities/logs"
aggregatorproto "github.com/ODIM-Project/ODIM/lib-utilities/proto/aggregator"
"github.com/ODIM-Project/ODIM/lib-utilities/response"
"github.com/ODIM-Project/ODIM/svc-aggregation/agmodel"
)
// AggregationResetRequest struct for reset the BMC
type AggregationResetRequest struct {
BatchSize int `json:"BatchSize"`
DelayBetweenBatchesInSeconds int `json:"DelayBetweenBatchesInSeconds"`
ResetType string `json:"ResetType"`
TargetURIs []string `json:"TargetURIs"`
}
// validateRequestFields validate each field in the request against default value of field type
func (validateReq AggregationResetRequest) validateResetRequestFields(reqBody []byte) (string, error) {
if isEmptyRequest(reqBody) {
return "ResetRequest", fmt.Errorf("ResetRequest is empty")
}
if validateReq.ResetType == "" {
return "ResetType", fmt.Errorf("property ResetType missing in the reset request")
}
if len(validateReq.TargetURIs) == 0 {
return "TargetURIs", fmt.Errorf("property TargetURIs missing/no resources found in the reset request")
}
return "", nil
}
func isEmptyRequest(requestBody []byte) bool {
var updateRequest map[string]interface{}
json.Unmarshal(requestBody, &updateRequest)
if len(updateRequest) <= 0 {
return true
}
return false
}
// Reset is for reseting the computer systems mentioned in the request body
func (e *ExternalInterface) Reset(ctx context.Context, taskID string, sessionUserName string, req *aggregatorproto.AggregatorRequest) response.RPC {
var resp response.RPC
var percentComplete int32
targetURI := "/redfish/v1/AggregationService/Actions/AggregationService.Reset/" // this will removed later and passed as input param in req struct
percentComplete = 0
taskInfo := &common.TaskUpdateInfo{Context: ctx, TaskID: taskID, TargetURI: targetURI, UpdateTask: e.UpdateTask, TaskRequest: string(req.RequestBody)}
var resetRequest AggregationResetRequest
if err := json.Unmarshal(req.RequestBody, &resetRequest); err != nil {
errMsg := "Unable to validate request fields: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.MalformedJSON, errMsg, nil, taskInfo)
}
missedProperty, err := resetRequest.validateResetRequestFields(req.RequestBody)
if err != nil {
errMsg := "Unable to validate request fields: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusBadRequest, response.PropertyMissing, errMsg, []interface{}{missedProperty}, taskInfo)
}
// Validating the request JSON properties for case sensitive
invalidProperties, err := common.RequestParamsCaseValidator(req.RequestBody, resetRequest)
if err != nil {
errMsg := "Unable to validate request fields: " + err.Error()
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo)
} else if invalidProperties != "" {
errorMessage := "One or more properties given in the request body are not valid, ensure properties are listed in uppercamelcase "
l.LogWithFields(ctx).Error(errorMessage)
resp := common.GeneralError(http.StatusBadRequest, response.PropertyUnknown, errorMessage, []interface{}{invalidProperties}, taskInfo)
return resp
}
// subTaskChan is a buffered channel with buffer size equal to total number of resources.
// this also helps while cancelling the task. even if the reader is not available for reading
// the channel buffer will collect them and allows gracefull exit for already spanned goroutines.
subTaskChan := make(chan int32, len(resetRequest.TargetURIs))
resp.StatusCode = http.StatusOK
var cancelled, partialResultFlag bool
var wg, writeWG sync.WaitGroup
threadID := 1
ctxt := context.WithValue(ctx, common.ThreadName, common.ResetAggregate)
ctxt = context.WithValue(ctxt, common.ThreadID, strconv.Itoa(threadID))
threadID++
go func() {
for i := 0; i < len(resetRequest.TargetURIs); i++ {
if cancelled == false { // task cancelled check to determine whether to collect status codes.
select {
case statusCode := <-subTaskChan:
if statusCode != http.StatusOK {
partialResultFlag = true
if resp.StatusCode < statusCode {
resp.StatusCode = statusCode
}
}
if i < len(resetRequest.TargetURIs)-1 {
percentComplete = int32(((i + 1) / len(resetRequest.TargetURIs)) * 100)
var task = fillTaskData(taskID, targetURI, string(req.RequestBody), resp, common.Running, common.OK, percentComplete, http.MethodPost)
err := e.UpdateTask(ctx, task)
if err != nil && err.Error() == common.Cancelling {
task = fillTaskData(taskID, targetURI, string(req.RequestBody), resp, common.Cancelled, common.OK, percentComplete, http.MethodPost)
e.UpdateTask(ctx, task)
cancelled = true
}
}
}
}
writeWG.Done()
}
}()
var tempIndex int
for _, resource := range resetRequest.TargetURIs {
wg.Add(1)
writeWG.Add(1)
// tempIndex is for checking batch size, its increment on each iteration
// if its equal to batch size then reinitilise.
// if batch size is 0 then reset all the systems without any kind of batch and ignore the DelayBetweenBatchesInSeconds
tempIndex = tempIndex + 1
if resetRequest.BatchSize == 0 || tempIndex <= resetRequest.BatchSize {
if strings.Contains(resource, "/AggregationService/Aggregates") {
e.aggregateSystems(ctx, resetRequest.ResetType, resource, taskID, string(req.RequestBody), subTaskChan, sessionUserName, resource, resetRequest.ResetType, &wg)
} else {
threadID := 1
resetCtx := context.WithValue(ctxt, common.ThreadName, common.ResetAggregate)
resetCtx = context.WithValue(resetCtx, common.ThreadID, strconv.Itoa(threadID))
go e.resetSystem(resetCtx, taskID, string(req.RequestBody), subTaskChan, sessionUserName, resource, resetRequest.ResetType, &wg)
threadID++
}
}
if tempIndex == resetRequest.BatchSize && resetRequest.BatchSize != 0 {
tempIndex = 0
time.Sleep(time.Second * time.Duration(resetRequest.DelayBetweenBatchesInSeconds))
}
}
wg.Wait()
writeWG.Wait()
taskStatus := common.OK
if partialResultFlag {
taskStatus = common.Warning
}
percentComplete = 100
var args response.Args
if resp.StatusCode != http.StatusOK {
errMsg := "one or more of the reset actions failed. for more information please check SubTasks in URI: /redfish/v1/TaskService/Tasks/" + taskID
l.LogWithFields(ctx).Error(errMsg)
return common.GeneralError(resp.StatusCode, resp.StatusMessage, errMsg, nil, taskInfo)
}
l.LogWithFields(ctx).Info("All reset actions are successfully completed. for more information please check SubTasks in URI: /redfish/v1/TaskService/Tasks/" + taskID)
resp.StatusCode = http.StatusOK
resp.StatusMessage = response.Success
args = response.Args{
Code: resp.StatusMessage,
Message: "Request completed successfully",
}
resp.Body = args.CreateGenericErrorResponse()
var task = fillTaskData(taskID, targetURI, string(req.RequestBody), resp, common.Completed, taskStatus, percentComplete, http.MethodPost)
err = e.UpdateTask(ctx, task)
if err != nil && err.Error() == common.Cancelling {
task = fillTaskData(taskID, targetURI, string(req.RequestBody), resp, common.Cancelled, common.Critical, percentComplete, http.MethodPost)
e.UpdateTask(ctx, task)
runtime.Goexit()
}
return resp
}
func (e *ExternalInterface) aggregateSystems(ctx context.Context, requestType, url, taskID, reqBody string, subTaskChan chan<- int32, sessionUserName, element, resetType string, wg *sync.WaitGroup) {
var resp response.RPC
var percentComplete int32
defer wg.Done()
subTaskURI, err := e.CreateChildTask(ctx, sessionUserName, taskID)
if err != nil {
subTaskChan <- http.StatusInternalServerError
l.LogWithFields(ctx).Error("error while trying to create sub task")
return
}
var subTaskID string
strArray := strings.Split(subTaskURI, "/")
if strings.HasSuffix(subTaskURI, "/") {
subTaskID = strArray[len(strArray)-2]
} else {
subTaskID = strArray[len(strArray)-1]
}
taskInfo := &common.TaskUpdateInfo{Context: ctx, TaskID: subTaskID, TargetURI: url, UpdateTask: e.UpdateTask, TaskRequest: reqBody}
aggregate, err1 := agmodel.GetAggregate(url)
if err1 != nil {
percentComplete = 100
errorMessage := err1.Error()
l.LogWithFields(ctx).Error("error getting aggregate : " + errorMessage)
if errors.DBKeyNotFound == err1.ErrNo() {
subTaskChan <- http.StatusNotFound
resp.StatusCode = http.StatusNotFound
common.GeneralError(http.StatusNotFound, response.ResourceNotFound, errorMessage, []interface{}{"Aggregate", url}, taskInfo)
return
}
subTaskChan <- http.StatusInternalServerError
resp.StatusCode = http.StatusInternalServerError
common.GeneralError(http.StatusInternalServerError, response.ResourceNotFound, errorMessage, []interface{}{"Aggregate", url}, taskInfo)
return
}
// subTaskChan is a buffered channel with buffer size equal to total number of elements.
// this also helps while cancelling the task. even if the reader is not available for reading
// the channel buffer will collect them and allows gracefull exit for already spanned goroutines.
subTaskChan1 := make(chan int32, len(aggregate.Elements))
resp.StatusCode = http.StatusOK
var cancelled, partialResultFlag bool
threadID := 1
ctxt := context.WithValue(ctx, common.ThreadName, common.SubTaskStatusUpdate)
ctxt = context.WithValue(ctxt, common.ThreadID, strconv.Itoa(threadID))
threadID++
var wg1, writeWG sync.WaitGroup
go func() {
for i := 0; i < len(aggregate.Elements); i++ {
if !cancelled { // task cancelled check to determine whether to collect status codes.
select {
case statusCode := <-subTaskChan1:
if statusCode != http.StatusOK {
partialResultFlag = true
if resp.StatusCode < statusCode {
resp.StatusCode = statusCode
}
}
if i < len(aggregate.Elements)-1 {
percentComplete = int32(((i + 1) / len(aggregate.Elements)) * 100)
var task = fillTaskData(subTaskID, url, reqBody, resp, common.Running, common.OK, percentComplete, http.MethodPost)
err := e.UpdateTask(ctx, task)
if err != nil && err.Error() == common.Cancelling {
task = fillTaskData(subTaskID, url, reqBody, resp, common.Cancelled, common.OK, percentComplete, http.MethodPost)
e.UpdateTask(ctx, task)
cancelled = true
}
}
}
}
writeWG.Done()
}
}()
for _, element := range aggregate.Elements {
wg1.Add(1)
writeWG.Add(1)
threadID := 1
resetCtxt := context.WithValue(ctxt, common.ThreadName, common.ResetSystem)
resetCtxt = context.WithValue(resetCtxt, common.ThreadID, strconv.Itoa(threadID))
go e.resetSystem(resetCtxt, subTaskID, reqBody, subTaskChan1, sessionUserName, element.OdataID, requestType, &wg1)
threadID++
}
wg1.Wait()
writeWG.Wait()
subTaskChan <- int32(resp.StatusCode)
taskStatus := common.OK
if partialResultFlag {
taskStatus = common.Warning
}
percentComplete = 100
var args response.Args
if resp.StatusCode != http.StatusOK {
subTaskChan <- resp.StatusCode
common.GeneralError(resp.StatusCode, resp.StatusMessage, "", []interface{}{"Aggregate", url}, taskInfo)
return
}
resp.StatusCode = http.StatusOK
resp.StatusMessage = response.Success
args = response.Args{
Code: resp.StatusMessage,
Message: "Request completed successfully",
}
resp.Body = args.CreateGenericErrorResponse()
var task = fillTaskData(subTaskID, url, reqBody, resp, common.Completed, taskStatus, percentComplete, http.MethodPost)
err = e.UpdateTask(ctx, task)
if err != nil && err.Error() == common.Cancelling {
common.GeneralError(http.StatusNotFound, common.Cancelled, "", []interface{}{"Aggregate", url}, taskInfo)
return
}
return
}