Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin should log all standard log properties in syslog format for update #1074

Merged
merged 5 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib-rest-client/pmbhandle/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ package pmbhandle

import (
"bytes"
"context"
"encoding/json"
"net/http"

"github.com/ODIM-Project/ODIM/lib-utilities/common"
l "github.com/ODIM-Project/ODIM/lib-utilities/logs"

"github.com/ODIM-Project/ODIM/lib-utilities/config"
)

// ContactPlugin is used to send a request to plugin to add a resource
func ContactPlugin(url, method, token string, odataID string, body interface{}, collaboratedInfo map[string]string) (*http.Response, error) {
func ContactPlugin(ctx context.Context, url, method, token string, odataID string, body interface{}, collaboratedInfo map[string]string) (*http.Response, error) {
jsonStr, err := json.Marshal(body)
if err != nil {
return nil, err
Expand All @@ -36,7 +38,7 @@ func ContactPlugin(url, method, token string, odataID string, body interface{},
l.Log.Error(err.Error())
return nil, err
}

req = CreateHeader(req, ctx)
// indicate to close the request created
req.Close = true

Expand Down Expand Up @@ -72,3 +74,22 @@ func ContactPlugin(url, method, token string, odataID string, body interface{},

return resp, nil
}

// CreateHeader is used to get data from context and set it to header for http request call
func CreateHeader(req *http.Request, ctx context.Context) *http.Request {
if ctx.Value("transactionid") != nil {
transactionId := ctx.Value("transactionid").(string)
actionId := ctx.Value("actionid").(string)
actionName := ctx.Value("actionname").(string)
threadId := ctx.Value("threadid").(string)
threadName := ctx.Value("threadname").(string)
processName := ctx.Value("processname").(string)
req.Header.Set(common.TransactionID, transactionId)
req.Header.Set(common.ActionID, actionId)
req.Header.Set(common.ActionName, actionName)
req.Header.Set(common.ThreadID, threadId)
req.Header.Set(common.ThreadName, threadName)
req.Header.Set(common.ProcessName, processName)
}
return req
}
6 changes: 3 additions & 3 deletions svc-aggregation/agcommon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (phc *PluginHealthCheckInterface) getAllServers(pluginID string) ([]agmodel
}

// ContactPlugin is for sending requests to a plugin.
func ContactPlugin(req agmodel.PluginContactRequest, serverName string) (*http.Response, error) {
func ContactPlugin(ctx context.Context, req agmodel.PluginContactRequest, serverName string) (*http.Response, error) {
req.LoginCredential = map[string]string{}
//ToDo: Variable "LoginCredentials" to be changed
req.LoginCredential["ServerName"] = serverName
Expand All @@ -375,7 +375,7 @@ func ContactPlugin(req agmodel.PluginContactRequest, serverName string) (*http.R
"Password": string(req.Plugin.Password),
}
reqURL := fmt.Sprintf("https://%s/ODIM/v1/Sessions", net.JoinHostPort(req.Plugin.IP, req.Plugin.Port))
response, err := pmbhandle.ContactPlugin(reqURL, http.MethodPost, "", "", payload, nil)
response, err := pmbhandle.ContactPlugin(ctx, reqURL, http.MethodPost, "", "", payload, nil)
if err != nil || (response != nil && response.StatusCode != http.StatusOK) {
return nil,
fmt.Errorf("failed to get session token from %s: %s: %+v", req.Plugin.ID, err.Error(), response)
Expand All @@ -386,7 +386,7 @@ func ContactPlugin(req agmodel.PluginContactRequest, serverName string) (*http.R
req.LoginCredential["Password"] = string(req.Plugin.Password)
}
reqURL := fmt.Sprintf("https://%s%s", net.JoinHostPort(req.Plugin.IP, req.Plugin.Port), req.URL)
return pmbhandle.ContactPlugin(reqURL, req.HTTPMethodType, req.Token, "", req.PostBody, req.LoginCredential)
return pmbhandle.ContactPlugin(ctx, reqURL, req.HTTPMethodType, req.Token, "", req.PostBody, req.LoginCredential)
}

// GetDeviceSubscriptionDetails is for getting device event susbcription details
Expand Down
4 changes: 2 additions & 2 deletions svc-aggregation/agcommon/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func TestcontactPlugin(t *testing.T) {

contactRequest.ContactClient = mockContactClient
contactRequest.Plugin = plugin
_, err = ContactPlugin(contactRequest, "")
_, err = ContactPlugin(context.TODO(), contactRequest, "")
assert.NotNil(t, err, "There should be an error")
}

Expand All @@ -622,7 +622,7 @@ func TestContactPlugin_XAuth(t *testing.T) {

contactRequest.ContactClient = mockContactClient
contactRequest.Plugin = plugin
_, err = ContactPlugin(contactRequest, "")
_, err = ContactPlugin(context.TODO(), contactRequest, "")
assert.NotNil(t, err, "There should be an error")
}

Expand Down
2 changes: 1 addition & 1 deletion svc-aggregation/rpc/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func mockDeviceData(uuid string, device agmodel.Target) error {
return nil
}

func mockContactClient(url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
func mockContactClient(ctx context.Context, url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
var bData agmodel.SaveSystem
bBytes, _ := json.Marshal(body)
json.Unmarshal(bBytes, &bData)
Expand Down
2 changes: 1 addition & 1 deletion svc-aggregation/system/addcompute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func mockSubscribeEMB(pluginID string, list []string) error {
return nil
}

func mockContactClientForDuplicate(url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
func mockContactClientForDuplicate(ctx context.Context, url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
var bData agmodel.SaveSystem
bBytes, _ := json.Marshal(body)
json.Unmarshal(bBytes, &bData)
Expand Down
24 changes: 12 additions & 12 deletions svc-aggregation/system/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type WildCard struct {
Values []string
}

//Device struct to define the response from plugin for UUID
// Device struct to define the response from plugin for UUID
type Device struct {
ServerIP string `json:"ServerIP"`
Username string `json:"Username"`
Expand All @@ -75,7 +75,7 @@ type Device struct {

// ExternalInterface struct holds the function pointers all outboud services
type ExternalInterface struct {
ContactClient func(string, string, string, string, interface{}, map[string]string) (*http.Response, error)
ContactClient func(context.Context, string, string, string, string, interface{}, map[string]string) (*http.Response, error)
Auth func(string, []string, []string) (response.RPC, error)
GetSessionUserName func(string) (string, error)
CreateChildTask func(string, string) (string, error)
Expand Down Expand Up @@ -123,7 +123,7 @@ type getResourceRequest struct {
LoginCredentials map[string]string
ParentOID string
OID string
ContactClient func(string, string, string, string, interface{}, map[string]string) (*http.Response, error)
ContactClient func(context.Context, string, string, string, string, interface{}, map[string]string) (*http.Response, error)
OemFlag bool
Plugin agmodel.Plugin
TaskRequest string
Expand Down Expand Up @@ -151,15 +151,15 @@ type respHolder struct {
InventoryData map[string]interface{}
}

//AddResourceRequest is payload of adding a resource
// AddResourceRequest is payload of adding a resource
type AddResourceRequest struct {
ManagerAddress string `json:"ManagerAddress"`
UserName string `json:"UserName"`
Password string `json:"Password"`
ConnectionMethod *ConnectionMethod `json:"ConnectionMethod"`
}

//ConnectionMethod struct definition for @odata.id
// ConnectionMethod struct definition for @odata.id
type ConnectionMethod struct {
OdataID string `json:"@odata.id"`
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func fillTaskData(taskID, targetURI, request string, resp response.RPC, taskStat
}
}

//genError generates error response so as to reduce boiler plate code
// genError generates error response so as to reduce boiler plate code
func genError(ctx context.Context, errorMessage string, respPtr *response.RPC, httpStatusCode int32, StatusMessage string, header map[string]string) {
respPtr.StatusCode = httpStatusCode
respPtr.StatusMessage = StatusMessage
Expand Down Expand Up @@ -299,11 +299,11 @@ func UpdateTaskData(taskData common.TaskData) error {

func contactPlugin(ctx context.Context, req getResourceRequest, errorMessage string) ([]byte, string, responseStatus, error) {
var resp responseStatus
pluginResp, err := callPlugin(req)
pluginResp, err := callPlugin(ctx, req)
if err != nil {
if req.StatusPoll {
if req.GetPluginStatus(ctx, req.Plugin) {
pluginResp, err = callPlugin(req)
pluginResp, err = callPlugin(ctx, req)
}
}
if err != nil {
Expand Down Expand Up @@ -420,7 +420,7 @@ func (h *respHolder) getAllSystemInfo(ctx context.Context, taskID string, progre
return computeSystemID, resourceURI, progress, nil
}

//Registries Discovery function
// Registries Discovery function
func (h *respHolder) getAllRegistries(ctx context.Context, taskID string, progress int32, alottedWork int32, req getResourceRequest) int32 {

// Get all available file names in the registry store directory in a list
Expand Down Expand Up @@ -1107,16 +1107,16 @@ func removeRetrievalLinks(retrievalLinks map[string]bool, parentoid string, reso
return
}

func callPlugin(req getResourceRequest) (*http.Response, error) {
func callPlugin(ctx context.Context, req getResourceRequest) (*http.Response, error) {
var oid string
for key, value := range getTranslationURL(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(reqURL, req.HTTPMethodType, "", oid, req.DeviceInfo, req.LoginCredentials)
return req.ContactClient(ctx, reqURL, req.HTTPMethodType, "", oid, req.DeviceInfo, req.LoginCredentials)
}
return req.ContactClient(reqURL, req.HTTPMethodType, req.Token, oid, req.DeviceInfo, nil)
return req.ContactClient(ctx, reqURL, req.HTTPMethodType, req.Token, oid, req.DeviceInfo, nil)
}

func updateManagerName(data []byte, pluginID string) []byte {
Expand Down
2 changes: 1 addition & 1 deletion svc-aggregation/system/deleteaggregationsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func getDataFromDB(table, key string, db common.DbType) (string, error) {
return data, nil
}

func mockContactClientForDelete(url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
func mockContactClientForDelete(ctx context.Context, url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
if url == "https://localhost:9092/ODIM/v1/Status" || (strings.Contains(url, "/ODIM/v1/Status") && credentials["UserName"] == "noStatusUser") {
body := `{"MessageId": "` + response.Success + `"}`
return &http.Response{
Expand Down
2 changes: 1 addition & 1 deletion svc-aggregation/system/pluginHealthCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func sendPluginStartupRequest(ctx context.Context, plugin agmodel.Plugin, startu
contactRequest.URL = "/ODIM/v1/Startup"
contactRequest.HTTPMethodType = http.MethodPost
contactRequest.PostBody = startupData
response, err := agcommon.ContactPlugin(contactRequest, serverName)
response, err := agcommon.ContactPlugin(ctx, contactRequest, serverName)
if err != nil || (response != nil && response.StatusCode != http.StatusOK) {
l.LogWithFields(ctx).Errorf("failed to send startup data to %s(%s): %s: %+v", plugin.ID, plugin.IP, err, response)
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion svc-aggregation/system/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package system

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -117,7 +118,7 @@ func mockIsAuthorized(sessionToken string, privileges, oemPrivileges []string) (
return common.GeneralError(http.StatusOK, response.Success, "", nil, nil), nil
}

func mockContactClient(url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
func mockContactClient(ctx context.Context, url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
if url == "" {
return nil, fmt.Errorf("InvalidRequest")
}
Expand Down
3 changes: 2 additions & 1 deletion svc-aggregation/system/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package system

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -58,7 +59,7 @@ func testSystemIndex(uuid string, indexData map[string]interface{}) error {
return nil

}
func testUpdateContactClient(url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
func testUpdateContactClient(ctx context.Context, url, method, token string, odataID string, body interface{}, credentials map[string]string) (*http.Response, error) {
if url == "" {
return nil, fmt.Errorf("InvalidRequest")
}
Expand Down
33 changes: 17 additions & 16 deletions svc-events/evcommon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package evcommon

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -37,7 +38,7 @@ import (
"github.com/ODIM-Project/ODIM/svc-events/evresponse"
)

//StartUpInteraface Holds the function pointer of external interface functions
// StartUpInteraface Holds the function pointer of external interface functions
type StartUpInteraface struct {
DecryptPassword func([]byte) ([]byte, error)
EMBConsume func(string)
Expand All @@ -64,7 +65,7 @@ type EmbTopic struct {
EMBConsume func(string)
}

//SavedSystems holds the resource details of the saved system
// SavedSystems holds the resource details of the saved system
type SavedSystems struct {
ManagerAddress string
Password []byte
Expand All @@ -73,7 +74,7 @@ type SavedSystems struct {
PluginID string
}

//PluginContactRequest holds the details required to contact the plugin
// PluginContactRequest holds the details required to contact the plugin
type PluginContactRequest struct {
URL string
HTTPMethodType string
Expand All @@ -84,7 +85,7 @@ type PluginContactRequest struct {
Plugin *evmodel.Plugin
}

//StartUpMap holds required data for plugin startup
// StartUpMap holds required data for plugin startup
type StartUpMap struct {
Location string
EventTypes []string
Expand Down Expand Up @@ -128,7 +129,7 @@ func (e *EmbTopic) ConsumeTopic(topicName string) {
// EMBTopics used to store the list of all topics
var EMBTopics EmbTopic

//PluginStartUp is used to call plugin "Startup" only on plugin restart and not on every status check
// PluginStartUp is used to call plugin "Startup" only on plugin restart and not on every status check
var PluginStartUp = false

// GetAllPluginStatus ...
Expand All @@ -140,7 +141,7 @@ func (st *StartUpInteraface) GetAllPluginStatus() {
return
}
for i := 0; i < len(pluginList); i++ {
go st.getPluginStatus(pluginList[i])
go st.getPluginStatus(context.TODO(), pluginList[i]) //TODO: Pass context
}
var pollingTime int
config.TLSConfMutex.RLock()
Expand All @@ -151,7 +152,7 @@ func (st *StartUpInteraface) GetAllPluginStatus() {

}

func (st *StartUpInteraface) getPluginStatus(plugin evmodel.Plugin) {
func (st *StartUpInteraface) getPluginStatus(ctx context.Context, plugin evmodel.Plugin) {
PluginsMap := make(map[string]bool)
StartUpResourceBatchSize := config.Data.PluginStatusPolling.StartUpResouceBatchSize
config.TLSConfMutex.RLock()
Expand Down Expand Up @@ -189,15 +190,15 @@ func (st *StartUpInteraface) getPluginStatus(plugin evmodel.Plugin) {
}
for {
if len(allServers) < StartUpResourceBatchSize {
err = st.callPluginStartUp(allServers, pluginID)
err = st.callPluginStartUp(ctx, allServers, pluginID)
if err != nil {
l.Log.Error("Error While trying call plugin startup" +
pluginID + err.Error())
}
break
}
batchServers := allServers[:StartUpResourceBatchSize]
err = st.callPluginStartUp(batchServers, pluginID)
err = st.callPluginStartUp(ctx, batchServers, pluginID)
if err != nil {
l.Log.Error("Error While trying call plugin startup" + pluginID + err.Error())
continue
Expand Down Expand Up @@ -272,7 +273,7 @@ func GetPluginStatus(plugin *evmodel.Plugin) bool {
return status
}

func (st *StartUpInteraface) callPluginStartUp(servers []SavedSystems, pluginID string) error {
func (st *StartUpInteraface) callPluginStartUp(ctx context.Context, servers []SavedSystems, pluginID string) error {
var startUpMap []StartUpMap
plugin, errs := st.GetPluginData(pluginID)
if errs != nil {
Expand Down Expand Up @@ -305,7 +306,7 @@ func (st *StartUpInteraface) callPluginStartUp(servers []SavedSystems, pluginID
"Password": string(plugin.Password),
}
contactRequest.URL = "/ODIM/v1/Sessions"
response, err := callPlugin(contactRequest)
response, err := callPlugin(ctx, contactRequest)
if err != nil {
return err
}
Expand All @@ -316,7 +317,7 @@ func (st *StartUpInteraface) callPluginStartUp(servers []SavedSystems, pluginID
"Password": string(plugin.Password),
}
}
response, err := callPlugin(contactRequest)
response, err := callPlugin(ctx, contactRequest)
if err != nil {
return err
}
Expand All @@ -332,15 +333,15 @@ func (st *StartUpInteraface) callPluginStartUp(servers []SavedSystems, pluginID
return updateDeviceSubscriptionLocation(r)
}

func callPlugin(req PluginContactRequest) (*http.Response, error) {
func callPlugin(ctx context.Context, req PluginContactRequest) (*http.Response, error) {
var reqURL = "https://" + req.Plugin.IP + ":" + req.Plugin.Port + req.URL
if strings.EqualFold(req.Plugin.PreferredAuthType, "XAuthToken") {
return pmbhandle.ContactPlugin(reqURL, req.HTTPMethodType, "", "", req.PostBody, nil)
return pmbhandle.ContactPlugin(ctx, reqURL, req.HTTPMethodType, "", "", req.PostBody, nil)
}
if strings.EqualFold(req.Plugin.PreferredAuthType, "BasicAuth") {
return pmbhandle.ContactPlugin(reqURL, req.HTTPMethodType, "", "", req.PostBody, req.LoginCredential)
return pmbhandle.ContactPlugin(ctx, reqURL, req.HTTPMethodType, "", "", req.PostBody, req.LoginCredential)
}
return pmbhandle.ContactPlugin(reqURL, req.HTTPMethodType, req.Token, "", req.PostBody, nil)
return pmbhandle.ContactPlugin(ctx, reqURL, req.HTTPMethodType, req.Token, "", req.PostBody, nil)

}

Expand Down
Loading