Newer
Older
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/constantmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/servicebuildermdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/statemdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/utiliymdl/guidmdl"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// CleanBranch returns "main" branch if the branch string is empty after trimming all spaces. if Non-empty returns same string(including spaces along with characters).
func CleanBranch(branch string) string {
if strings.TrimSpace(branch) == "" {
branch = constantmdl.Branch_Main
}
return branch
}
func ConcatenateEntityWithBranch(name, branch string) string {
return name + constantmdl.Branch_Separator + CleanBranch(branch)
}
func setResponseHeader(serviceName string) responseData {
rd := responseData{}
val, ok := GetResponseHeader(serviceName)
if ok {
rd.ResponseHeader = val
}
return rd
}
func executeServiceWithBranch(name, branch string, data []byte, isRestricted, isRoleBased, heavyDataActivity bool, principalObj servicebuildermdl.Principal) (interface{}, *servicebuildermdl.AbstractBusinessLogicHolder, bool, int, error) {
var ab *servicebuildermdl.AbstractBusinessLogicHolder
// for calling queries as queries are not associated with any branch
activity := name
if name != "FetchQueryData" {
activity = ConcatenateEntityWithBranch(name, branch)
}
service, found = roleBasedServices.Get(activity)
service, found = restrictedServices.Get(activity)
service, found = openServices.Get(activity)
loggermdl.LogError("Service Not Found: " + activity)
return nil, ab, isCompressed, errormdl.SERVICENOTFOUND, errormdl.Wrap("Service Not Found: " + name)
// FetchQueryData & FetchGCData
// We are not validating token for these two activities here.
// we are checking & validating token inside these routes because these routes are used for open, restricted & rolebased actors.
if isRestricted && isRoleBased && name != "FetchQueryData" && name != "FetchGCData" {
if !validateRoleFromToken(principalObj, service.(ServiceCache)) {
loggermdl.LogError("INVALID_ACTOR: " + name)
return nil, ab, isCompressed, errormdl.SERVICENOTFOUND, errormdl.Wrap("INVALID_ACTOR: " + name)
tmpServiceCache := service.(ServiceCache)
serviceCache := tmpServiceCache
if serviceCache.HeavyDataActivity {
if !heavyDataActivity {
return nil, ab, isCompressed, errormdl.SERVICENOTFOUND, errormdl.Wrap("Service is marked as heavy data... kindly use heavymql route")
}
}
if serviceCache.AsyncService {
asyncToken := guidmdl.GetGUID()
data, err := sjson.SetBytes(data, "asyncToken", asyncToken)
if errormdl.CheckErr(err) != nil {
loggermdl.LogError(err)
return nil, nil, isCompressed, errormdl.SJSONERROR, errormdl.CheckErr(err)
go serviceCache.Service(&rs, principalObj)
servingTime := time.Since(start)
go statemdl.UpdateServiceStateWithBranch(name, branch, servingTime, serviceError, isRestricted, isRoleBased)
return asyncToken, nil, isCompressed, errormdl.NOERROR, nil
rs := gjson.ParseBytes(data)
result, ab, serviceError = serviceCache.Service(&rs, principalObj)
servingTime := time.Since(start)
// Record State for every service
go statemdl.UpdateServiceStateWithBranch(name, branch, servingTime, serviceError, isRestricted, isRoleBased)
return result, ab, isCompressed, errormdl.EXPECTATIONFAILED, serviceError
// executeService is same as `executeServiceWithBranch` but works for `main` branch
func executeService(name string, data []byte, isRestricted, isRoleBased, heavyDataActivity bool, principalObj servicebuildermdl.Principal) (interface{}, *servicebuildermdl.AbstractBusinessLogicHolder, bool, int, error) {
return executeServiceWithBranch(name, constantmdl.Branch_Main, data, isRestricted, isRoleBased, heavyDataActivity, principalObj)
}
func validateRoleFromToken(principalObj servicebuildermdl.Principal, service ServiceCache) bool {
// check if group from request is present in groups associated with the service.
for _, g := range service.Groups {
for _, tokenGroup := range principalObj.Groups {
if g == tokenGroup {
return true
}
}
}
return false
}
func (s ServiceCache) preHooksExec(rs *gjson.Result, principalObj *servicebuildermdl.Principal) {
for i := 0; i < len(s.PreHooks); i++ {
var service interface{}
activityName := ConcatenateEntityWithBranch(s.PreHooks[i].ActivityName, s.PreHooks[i].Branch)
var found bool
if s.PreHooks[i].ActorType == "ROLEBASED" {
service, found = roleBasedServices.Get(activityName)
service, found = restrictedServices.Get(activityName)
loggermdl.LogError("Pre Hook Not found: ", activityName, " for actor type: ", s.PreHooks[i].ActorType, " branch:", s.PreHooks[i].Branch)
return
}
tmpServiceCache := service.(ServiceCache)
serviceCache := tmpServiceCache
go serviceCache.Service(rs, *principalObj)
}
}
func (s ServiceCache) postHooksExec(data interface{}, principalObj *servicebuildermdl.Principal) {
return
}
if data != nil {
// Fixed: Panic if data is nil.
objType := reflect.TypeOf(data).String()
if strings.Contains(objType, "map[string]") {
ba, _ := json.Marshal(data)
rs = gjson.ParseBytes(ba)
}
activityName := ConcatenateEntityWithBranch(s.PostHooks[i].ActivityName, s.PostHooks[i].Branch)
var service interface{}
var found bool
if s.PostHooks[i].ActorType == "ROLEBASED" {
service, found = roleBasedServices.Get(activityName)
}
if s.PostHooks[i].ActorType == "RESTRICTED" {
service, found = restrictedServices.Get(activityName)
loggermdl.LogError("Post Hook Not found: ", activityName, " for actor type: ", s.PostHooks[i].ActorType, " Branch:", s.PostHooks[i].Branch)
return
}
tmpServiceCache := service.(ServiceCache)
serviceCache := tmpServiceCache
go serviceCache.Service(&rs, *principalObj)
}
}