-
Notifications
You must be signed in to change notification settings - Fork 58
/
simpleUpdate.go
327 lines (301 loc) · 13 KB
/
simpleUpdate.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//(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 update ...
package update
// ---------------------------------------------------------------------------------------
// IMPORT Section
//
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"runtime"
"strconv"
"strings"
"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/config"
l "github.com/ODIM-Project/ODIM/lib-utilities/logs"
updateproto "github.com/ODIM-Project/ODIM/lib-utilities/proto/update"
"github.com/ODIM-Project/ODIM/lib-utilities/response"
"github.com/ODIM-Project/ODIM/svc-update/ucommon"
)
var (
//RequestParamsCaseValidatorFunc ...
RequestParamsCaseValidatorFunc = common.RequestParamsCaseValidator
//JSONMarshalFunc ...
JSONMarshalFunc = json.Marshal
//StringsEqualFoldFunc ...
StringsEqualFoldFunc = strings.EqualFold
)
// SimpleUpdate function handler for simpe update process
func (e *ExternalInterface) SimpleUpdate(ctx context.Context, taskID string, sessionUserName string, req *updateproto.UpdateRequest) response.RPC {
var resp response.RPC
var percentComplete int32
targetURI := "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate"
taskInfo := &common.TaskUpdateInfo{Context: ctx, TaskID: taskID, TargetURI: targetURI, UpdateTask: e.External.UpdateTask, TaskRequest: string(req.RequestBody)}
var updateRequest SimpleUpdateRequest
err := json.Unmarshal(req.RequestBody, &updateRequest)
if err != nil {
errMsg := "Unable to parse the simple update request" + err.Error()
l.LogWithFields(ctx).Warn(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo)
}
if len(updateRequest.Targets) == 0 {
errMsg := "'Targets' parameter cannot be empty"
l.LogWithFields(ctx).Warn(errMsg)
return common.GeneralError(http.StatusBadRequest, response.PropertyMissing, errMsg, []interface{}{"Targets"}, taskInfo)
}
// Validating the request JSON properties for case sensitive
invalidProperties, err := RequestParamsCaseValidatorFunc(req.RequestBody, updateRequest)
if err != nil {
errMsg := "Unable to validate request parameters: " + err.Error()
l.LogWithFields(ctx).Warn(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).Warn(errorMessage)
response := common.GeneralError(http.StatusBadRequest, response.PropertyUnknown, errorMessage, []interface{}{invalidProperties}, taskInfo)
return response
}
targetList := make(map[string][]string)
targetList, err = sortTargetList(ctx, updateRequest.Targets)
if err != nil {
errorMessage := "SystemUUID not found"
l.LogWithFields(ctx).Warn(errorMessage)
return common.GeneralError(http.StatusNotFound, response.ResourceNotFound, errorMessage, []interface{}{"System", fmt.Sprintf("%v", updateRequest.Targets)}, taskInfo)
}
partialResultFlag := false
subTaskChannel := make(chan int32, len(targetList))
serverURI := ""
for id, target := range targetList {
updateRequest.Targets = target
marshalBody, err := JSONMarshalFunc(updateRequest)
if err != nil {
errMsg := "Unable to parse the simple update request" + err.Error()
l.LogWithFields(ctx).Warn(errMsg)
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo)
}
updateRequestBody := string(marshalBody)
serverURI = "/redfish/v1/Systems/" + id
var threadID int = 1
ctxt := context.WithValue(ctx, common.ThreadName, common.SendRequest)
ctxt = context.WithValue(ctxt, common.ThreadID, strconv.Itoa(threadID))
go e.sendRequest(ctxt, id, taskID, serverURI, updateRequestBody, updateRequest.RedfishOperationApplyTime, subTaskChannel, sessionUserName)
threadID++
}
resp.StatusCode = http.StatusOK
for i := 0; i < len(targetList); i++ {
select {
case statusCode := <-subTaskChannel:
if statusCode != http.StatusOK {
partialResultFlag = true
if resp.StatusCode < statusCode {
resp.StatusCode = statusCode
}
}
if i < len(targetList)-1 {
percentComplete := int32(((i + 1) / len(targetList)) * 100)
var task = fillTaskData(taskID, targetURI, string(req.RequestBody), resp, common.Running, common.OK, percentComplete, http.MethodPost)
err := e.External.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.External.UpdateTask(ctx, task)
runtime.Goexit()
}
}
}
}
taskStatus := common.OK
if partialResultFlag {
taskStatus = common.Warning
}
percentComplete = 100
if resp.StatusCode != http.StatusOK {
errMsg := "One or more of the SimpleUpdate requests failed. for more information please check SubTasks in URI: /redfish/v1/TaskService/Tasks/" + taskID
l.LogWithFields(ctx).Warn(errMsg)
switch resp.StatusCode {
case http.StatusAccepted:
return common.GeneralError(http.StatusAccepted, response.TaskStarted, errMsg, []interface{}{fmt.Sprintf("%v", targetList)}, taskInfo)
case http.StatusUnauthorized:
return common.GeneralError(http.StatusUnauthorized, response.ResourceAtURIUnauthorized, errMsg, []interface{}{fmt.Sprintf("%v", targetList)}, taskInfo)
case http.StatusNotFound:
return common.GeneralError(http.StatusNotFound, response.ResourceNotFound, errMsg, []interface{}{"option", "SimpleUpdate"}, taskInfo)
case http.StatusBadRequest:
return common.GeneralError(http.StatusBadRequest, response.PropertyUnknown, errMsg, []interface{}{"UpdateService.SimpleUpdate"}, taskInfo)
default:
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo)
}
}
l.LogWithFields(ctx).Info("All SimpleUpdate requests successfully completed. for more information please check SubTasks in URI: /redfish/v1/TaskService/Tasks/" + taskID)
resp.StatusMessage = response.Success
resp.StatusCode = http.StatusOK
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.External.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.External.UpdateTask(ctx, task)
runtime.Goexit()
}
return resp
}
func (e *ExternalInterface) sendRequest(ctx context.Context, uuid, taskID, serverURI, updateRequestBody string, applyTime string, subTaskChannel chan<- int32, sessionUserName string) {
var resp response.RPC
subTaskURI, err := e.External.CreateChildTask(ctx, sessionUserName, taskID)
if err != nil {
subTaskChannel <- http.StatusInternalServerError
l.LogWithFields(ctx).Warn("Unable to create sub task: " + err.Error())
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: serverURI, UpdateTask: e.External.UpdateTask, TaskRequest: updateRequestBody}
var percentComplete int32
target, gerr := e.External.GetTarget(uuid)
if gerr != nil {
subTaskChannel <- http.StatusBadRequest
errMsg := gerr.Error()
l.LogWithFields(ctx).Warn(errMsg)
common.GeneralError(http.StatusBadRequest, response.ResourceNotFound, gerr.Error(), []interface{}{"System", uuid}, nil)
return
}
if applyTime == "OnStartUpdateRequest" {
err := e.External.GenericSave(ctx, []byte(updateRequestBody), "SimpleUpdate", uuid)
if err != nil {
subTaskChannel <- http.StatusInternalServerError
errMsg := "Unable to save the simple update request" + err.Error()
l.LogWithFields(ctx).Warn(errMsg)
common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, nil)
return
}
}
updateRequestBody = strings.Replace(string(updateRequestBody), uuid+".", "", -1)
//replacing the reruest url with south bound translation URL
for key, value := range config.Data.URLTranslation.SouthBoundURL {
updateRequestBody = strings.Replace(updateRequestBody, key, value, -1)
}
decryptedPasswordByte, err := e.External.DevicePassword(target.Password)
if err != nil {
subTaskChannel <- http.StatusInternalServerError
errMsg := "Unable to decrypt device password: " + err.Error()
l.LogWithFields(ctx).Warn(errMsg)
common.GeneralError(http.StatusInternalServerError, response.InternalError, errMsg, nil, taskInfo)
return
}
target.Password = decryptedPasswordByte
// Get the Plugin info
plugin, gerr := e.External.GetPluginData(target.PluginID)
if gerr != nil {
subTaskChannel <- http.StatusNotFound
errMsg := "Unable to get plugin data: " + gerr.Error()
l.LogWithFields(ctx).Warn(errMsg)
common.GeneralError(http.StatusNotFound, response.ResourceNotFound, errMsg, []interface{}{"PluginData", target.PluginID}, taskInfo)
return
}
var contactRequest ucommon.PluginContactRequest
contactRequest.ContactClient = e.External.ContactClient
contactRequest.Plugin = plugin
if StringsEqualFoldFunc(plugin.PreferredAuthType, "XAuthToken") {
var err error
contactRequest.HTTPMethodType = http.MethodPost
contactRequest.DeviceInfo = map[string]interface{}{
"UserName": plugin.Username,
"Password": string(plugin.Password),
}
contactRequest.OID = "/ODIM/v1/Sessions"
_, token, getResponse, err := e.External.ContactPlugin(ctx, contactRequest, "error while creating session with the plugin: ")
if err != nil {
subTaskChannel <- getResponse.StatusCode
errMsg := err.Error()
l.LogWithFields(ctx).Warn(errMsg)
common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, taskInfo)
return
}
contactRequest.Token = token
} else {
contactRequest.BasicAuth = map[string]string{
"UserName": plugin.Username,
"Password": string(plugin.Password),
}
}
target.PostBody = []byte(updateRequestBody)
contactRequest.DeviceInfo = target
contactRequest.OID = "/ODIM/v1/UpdateService/Actions/UpdateService.SimpleUpdate"
contactRequest.HTTPMethodType = http.MethodPost
respBody, location, getResponse, err := e.External.ContactPlugin(ctx, contactRequest, "error while performing simple update action: ")
if err != nil {
subTaskChannel <- getResponse.StatusCode
errMsg := err.Error()
l.LogWithFields(ctx).Warn(errMsg)
common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, errMsg, getResponse.MsgArgs, taskInfo)
return
}
if getResponse.StatusCode == http.StatusAccepted {
getResponse, err = e.monitorPluginTask(ctx, subTaskChannel, &monitorTaskRequest{
subTaskID: subTaskID,
serverURI: serverURI,
updateRequestBody: updateRequestBody,
respBody: respBody,
getResponse: getResponse,
taskInfo: taskInfo,
location: location,
pluginRequest: contactRequest,
resp: resp,
})
if err != nil {
return
}
}
resp.StatusCode = http.StatusOK
percentComplete = 100
subTaskChannel <- int32(getResponse.StatusCode)
var task = fillTaskData(subTaskID, serverURI, updateRequestBody, resp, common.Completed, common.OK, percentComplete, http.MethodPost)
err = e.External.UpdateTask(ctx, task)
if err != nil && err.Error() == common.Cancelling {
var task = fillTaskData(subTaskID, serverURI, updateRequestBody, resp, common.Cancelled, common.Critical, percentComplete, http.MethodPost)
e.External.UpdateTask(ctx, task)
}
return
}
func sortTargetList(ctx context.Context, Targets []string) (map[string][]string, error) {
returnList := make(map[string][]string)
for _, individualTarget := range Targets {
// spliting the uuid and system id
requestData := strings.Split(individualTarget, "/")
var requestTarget []string
for _, data := range requestData {
if strings.Contains(data, ".") {
requestTarget = strings.SplitN(data, ".", 2)
}
}
if len(requestTarget) != 2 || requestTarget[1] == "" {
errorMessage := "error: SystemUUID not found"
return returnList, errors.New(errorMessage)
}
uuid := requestTarget[0]
returnList[uuid] = append(returnList[uuid], individualTarget)
}
return returnList, nil
}