-
Notifications
You must be signed in to change notification settings - Fork 59
/
common.go
338 lines (313 loc) · 12.3 KB
/
common.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
328
329
330
331
332
333
334
335
336
337
338
//(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 scommon ...
package scommon
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/config"
"github.com/ODIM-Project/ODIM/lib-utilities/errors"
l "github.com/ODIM-Project/ODIM/lib-utilities/logs"
"github.com/ODIM-Project/ODIM/svc-systems/smodel"
)
var (
//IOReadAll function pointer for the ioutil.ReadAll
IOReadAll = ioutil.ReadAll
//JSONUnmarshalFunc function pointer for the json.Unmarshal
JSONUnmarshalFunc = json.Unmarshal
)
// Schema is used to define the allowed values for search/filter
type Schema struct {
SearchKeys []map[string]map[string]string `json:"searchKeys"`
ConditionKeys []string `json:"conditionKeys"`
QueryKeys []string `json:"queryKeys"`
}
// SF holds the schema data for search/filter
var SF Schema
// PluginContactRequest hold the request of contact plugin
type PluginContactRequest struct {
Token string
OID string
DeviceInfo interface{}
BasicAuth map[string]string
ContactClient func(context.Context, string, string, string, string, interface{}, map[string]string) (*http.Response, error)
GetPluginStatus func(context.Context, smodel.Plugin) bool
Plugin smodel.Plugin
HTTPMethodType string
}
// ResponseStatus holds the response of Contact Plugin
type ResponseStatus struct {
StatusCode int32
StatusMessage string
}
// ResourceInfoRequest hold the request of getting Resource
type ResourceInfoRequest struct {
URL string
UUID string
SystemID string
ContactClient func(context.Context, string, string, string, string, interface{}, map[string]string) (*http.Response, error)
DevicePassword func([]byte) ([]byte, error)
GetPluginStatus func(context.Context, smodel.Plugin) bool
ResourceName string
}
// GetResourceInfoFromDevice will contact to the and gets the Particual resource info from device
// If saveRequired is set to true the newly collected data will be saved in the DB.
// Some specific cases may not require the data to be stored in DB,
// eg: Delete volume requires reset of the BMC to take its effect. Before a reset, volumes retrieval
// request can provide the deleted volume. We can avoid storing such a data with the use of saveRequired.
func GetResourceInfoFromDevice(ctx context.Context, req ResourceInfoRequest, saveRequired bool) (string, error) {
l.LogWithFields(ctx).Debugf("incoming GetResourceInfoFromDevice request with %s", req.URL)
target, gerr := smodel.GetTarget(req.UUID)
if gerr != nil {
return "", gerr
}
// Get the Plugin info
plugin, gerr := smodel.GetPluginData(target.PluginID)
if gerr != nil {
return "", gerr
}
var contactRequest PluginContactRequest
contactRequest.ContactClient = req.ContactClient
contactRequest.Plugin = plugin
contactRequest.GetPluginStatus = req.GetPluginStatus
if strings.EqualFold(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, _, err := ContactPlugin(ctx, contactRequest, "error while getting the details "+contactRequest.OID+": ")
if err != nil {
return "", err
}
contactRequest.Token = token
} else {
contactRequest.BasicAuth = map[string]string{
"UserName": plugin.Username,
"Password": string(plugin.Password),
}
}
decryptedPasswordByte, err := req.DevicePassword(target.Password)
if err != nil {
// Frame the RPC response body and response Header below
errorMessage := "error while trying to decrypt device password: " + err.Error()
return "", fmt.Errorf(errorMessage)
}
contactRequest.DeviceInfo = map[string]interface{}{
"ManagerAddress": target.ManagerAddress,
"UserName": target.UserName,
"Password": decryptedPasswordByte,
}
//replace the uuid:system id with the system to the @odata.id from request url
contactRequest.OID = strings.Replace(req.URL, req.UUID+"."+req.SystemID, req.SystemID, -1)
contactRequest.HTTPMethodType = http.MethodGet
body, _, _, err := ContactPlugin(ctx, contactRequest, "error while getting the details "+contactRequest.OID+": ")
if err != nil {
return "", err
}
var resourceData map[string]interface{}
err = JSONUnmarshalFunc(body, &resourceData)
if err != nil {
return "", err
}
var resourceName, oidKey string
/* Checking if the retrieved data is allowed to save in DB,
* if not allowed we will not save in DB.
*/
//replacing the uuid while saving the data
//to replace the id of system
var updatedData = strings.Replace(string(body), "/redfish/v1/Systems/", "/redfish/v1/Systems/"+req.UUID+".", -1)
updatedData = strings.Replace(updatedData, "/redfish/v1/systems/", "/redfish/v1/systems/"+req.UUID+".", -1)
// to replace the id in managers
updatedData = strings.Replace(updatedData, "/redfish/v1/Managers/", "/redfish/v1/Managers/"+req.UUID+".", -1)
// to replace id in chassis
updatedData = strings.Replace(updatedData, "/redfish/v1/Chassis/", "/redfish/v1/Chassis/"+req.UUID+".", -1)
if saveRequired && checkRetrievalInfo(contactRequest.OID) {
oidKey = keyFormation(contactRequest.OID, req.SystemID, req.UUID)
var memberFlag bool
if _, ok := resourceData["Members"]; ok {
memberFlag = true
}
if req.ResourceName != "" {
resourceName = req.ResourceName
} else {
// Get the Table name to save the data in db
resourceName = getResourceName(contactRequest.OID, memberFlag)
}
// persist the response with table resourceName and key as system UUID + Oid Needs relook TODO
err = smodel.GenericSave(ctx, []byte(updatedData), resourceName, oidKey)
if err != nil {
return "", err
}
}
return updatedData, nil
}
// keyFormation is to form the key to insert in DB
func keyFormation(oid, systemID, DeviceUUID string) string {
if oid[len(oid)-1:] == "/" {
oid = oid[:len(oid)-1]
}
str := strings.Split(oid, "/")
var key []string
for i, id := range str {
if id == systemID && (strings.EqualFold(str[i-1], "Systems") || strings.EqualFold(str[i-1], "Chassis")) {
key = append(key, DeviceUUID+"."+id)
continue
}
key = append(key, id)
}
return strings.Join(key, "/")
}
// getResourceName fetches the table name for storing the particualar resource
func getResourceName(oDataID string, memberFlag bool) string {
str := strings.Split(oDataID, "/")
if memberFlag {
return str[len(str)-1] + "Collection"
}
if _, err := strconv.Atoi(str[len(str)-2]); err == nil {
return str[len(str)-1]
}
return str[len(str)-2]
}
// ContactPlugin is commons which handles the request and response of Contact Plugin usage
func ContactPlugin(ctx context.Context, req PluginContactRequest, errorMessage string) ([]byte, string, ResponseStatus, error) {
l.LogWithFields(ctx).Debugf("incoming ContactPlugin request with token: %s, OID: %s", req.Token, req.OID)
var resp ResponseStatus
var response *http.Response
var err error
response, err = callPlugin(ctx, req)
if err != nil {
if req.GetPluginStatus(ctx, req.Plugin) {
response, err = callPlugin(ctx, req)
}
if err != nil {
errorMessage = errorMessage + err.Error()
resp.StatusCode = http.StatusInternalServerError
resp.StatusMessage = errors.InternalError
l.LogWithFields(ctx).Error(errorMessage)
return nil, "", resp, fmt.Errorf(errorMessage)
}
}
defer response.Body.Close()
body, err := IOReadAll(response.Body)
if err != nil {
errorMessage := "error while trying to read response body: " + err.Error()
resp.StatusCode = http.StatusInternalServerError
resp.StatusMessage = errors.InternalError
l.LogWithFields(ctx).Error(errorMessage)
return nil, "", resp, fmt.Errorf(errorMessage)
}
l.LogWithFields(ctx).Info("response.StatusCode: " + fmt.Sprintf("%d", response.StatusCode))
resp.StatusCode = int32(response.StatusCode)
if response.StatusCode != http.StatusCreated && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusAccepted {
resp.StatusCode = int32(response.StatusCode)
l.LogWithFields(ctx).Println(errorMessage)
return body, "", resp, fmt.Errorf(errorMessage)
}
data := string(body)
//replacing the resposne with north bound translation URL
for key, value := range config.Data.URLTranslation.NorthBoundURL {
data = strings.Replace(data, key, value, -1)
}
if response.StatusCode == http.StatusAccepted {
return []byte(data), response.Header.Get("Location"), resp, nil
}
return []byte(data), response.Header.Get("X-Auth-Token"), resp, nil
}
func checkRetrievalInfo(oid string) bool {
//skiping the Retrieval if parent oid contains links in other resource of config
for _, resourceName := range config.Data.AddComputeSkipResources.SkipResourceListUnderOthers {
if strings.Contains(oid, resourceName) {
return false
}
}
return true
}
// GetPluginStatus checks the status of given plugin in configured interval
func GetPluginStatus(ctx context.Context, plugin smodel.Plugin) bool {
var pluginStatus = common.PluginStatus{
Method: http.MethodGet,
RequestBody: common.StatusRequest{
Comment: "",
},
ResponseWaitTime: config.Data.PluginStatusPolling.ResponseTimeoutInSecs,
Count: config.Data.PluginStatusPolling.MaxRetryAttempt,
RetryInterval: config.Data.PluginStatusPolling.RetryIntervalInMins,
PluginIP: plugin.IP,
PluginPort: plugin.Port,
PluginUsername: plugin.Username,
PluginUserPassword: string(plugin.Password),
PluginPrefferedAuthType: plugin.PreferredAuthType,
CACertificate: &config.Data.KeyCertConf.RootCACertificate,
}
status, _, _, err := pluginStatus.CheckStatus()
if err != nil && !status {
l.LogWithFields(ctx).Error("Error While getting the status for plugin " + plugin.ID + ": " + err.Error())
return status
}
l.LogWithFields(ctx).Info("Status of plugin" + plugin.ID + strconv.FormatBool(status))
return status
}
func callPlugin(ctx context.Context, req PluginContactRequest) (*http.Response, error) {
var oid string
for key, value := range config.Data.URLTranslation.SouthBoundURL {
oid = strings.Replace(req.OID, key, value, -1)
}
var reqURL = "https://" + req.Plugin.IP + ":" + req.Plugin.Port + oid
if strings.EqualFold(req.Plugin.PreferredAuthType, "BasicAuth") {
return req.ContactClient(ctx, reqURL, req.HTTPMethodType, "", oid, req.DeviceInfo, req.BasicAuth)
}
return req.ContactClient(ctx, reqURL, req.HTTPMethodType, req.Token, oid, req.DeviceInfo, nil)
}
// TrackConfigFileChanges monitors the odim config changes using fsnotfiy
func TrackConfigFileChanges(configFilePath string, errChan chan error) {
eventChan := make(chan interface{})
format := config.Data.LogFormat
go common.TrackConfigFileChanges(configFilePath, eventChan, errChan)
for {
select {
case info := <-eventChan: // new data arrives through eventChan channel
l.Log.Info(info)
config.TLSConfMutex.RLock()
schemaFile, err := ioutil.ReadFile(config.Data.SearchAndFilterSchemaPath)
if err != nil {
l.Log.Error("error while trying to read search/filter schema json" + err.Error())
}
config.TLSConfMutex.RUnlock()
err = json.Unmarshal(schemaFile, &SF)
if err != nil {
l.Log.Error("error while trying to fetch search/filter schema json" + err.Error())
}
if l.Log.Level != config.Data.LogLevel {
l.Log.Info("Log level is updated, new log level is ", config.Data.LogLevel)
l.Log.Logger.SetLevel(config.Data.LogLevel)
}
if format != config.Data.LogFormat {
l.SetFormatter(config.Data.LogFormat)
format = config.Data.LogFormat
l.Log.Info("Log format is updated, new log format is ", config.Data.LogFormat)
}
case err := <-errChan:
l.Log.Error(err)
}
}
}