Refactor Go code
[nonrtric.git] / dmaap-mediator-producer / internal / jobs / jobs.go
index 10eaf68..7b21b00 100644 (file)
 package jobs
 
 import (
+       "encoding/json"
+       "fmt"
        "os"
-       "path/filepath"
-       "strings"
+       "sync"
+
+       log "github.com/sirupsen/logrus"
+       "oransc.org/nonrtric/dmaapmediatorproducer/internal/config"
+       "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
 )
 
-type Type struct {
-       TypeId string
-       Schema string
+type TypeData struct {
+       TypeId        string `json:"id"`
+       DMaaPTopicURL string `json:"dmaapTopicUrl"`
+       Jobs          map[string]JobInfo
 }
 
 type JobInfo struct {
-       Owner            string `json:"owner"`
-       LastUpdated      string `json:"last_updated"`
-       InfoJobIdentity  string `json:"info_job_identity"`
-       TargetUri        string `json:"target_uri"`
-       InfoJobData      string `json:"info_job_data"`
-       InfoTypeIdentity string `json:"info_type_identity"`
+       Owner            string      `json:"owner"`
+       LastUpdated      string      `json:"last_updated"`
+       InfoJobIdentity  string      `json:"info_job_identity"`
+       TargetUri        string      `json:"target_uri"`
+       InfoJobData      interface{} `json:"info_job_data"`
+       InfoTypeIdentity string      `json:"info_type_identity"`
+}
+
+type JobTypeHandler interface {
+       GetTypes() ([]config.TypeDefinition, error)
+       GetSupportedTypes() []string
 }
 
 type JobHandler interface {
        AddJob(JobInfo) error
+       DeleteJob(jobId string)
 }
 
-var (
-       typeDir = "configs"
-       Handler JobHandler
-       allJobs = make(map[string]map[string]JobInfo)
-)
+type JobHandlerImpl struct {
+       mu               sync.Mutex
+       configFile       string
+       allTypes         map[string]TypeData
+       pollClient       restclient.HTTPClient
+       distributeClient restclient.HTTPClient
+}
 
-func init() {
-       Handler = newJobHandlerImpl()
+func NewJobHandlerImpl(typeConfigFilePath string, pollClient restclient.HTTPClient, distributeClient restclient.HTTPClient) *JobHandlerImpl {
+       return &JobHandlerImpl{
+               configFile:       typeConfigFilePath,
+               allTypes:         make(map[string]TypeData),
+               pollClient:       pollClient,
+               distributeClient: distributeClient,
+       }
 }
 
-type jobHandlerImpl struct{}
+func (jh *JobHandlerImpl) AddJob(ji JobInfo) error {
+       jh.mu.Lock()
+       defer jh.mu.Unlock()
+       if err := jh.validateJobInfo(ji); err == nil {
+               jobs := jh.allTypes[ji.InfoTypeIdentity].Jobs
+               jobs[ji.InfoJobIdentity] = ji
+               log.Debug("Added job: ", ji)
+               return nil
+       } else {
+               return err
+       }
+}
 
-func newJobHandlerImpl() *jobHandlerImpl {
-       return &jobHandlerImpl{}
+func (jh *JobHandlerImpl) DeleteJob(jobId string) {
+       jh.mu.Lock()
+       defer jh.mu.Unlock()
+       for _, typeData := range jh.allTypes {
+               delete(typeData.Jobs, jobId)
+       }
+       log.Debug("Deleted job: ", jobId)
 }
 
-func (jh *jobHandlerImpl) AddJob(ji JobInfo) error {
-       if jobs, ok := allJobs[ji.InfoTypeIdentity]; ok {
-               if _, ok := jobs[ji.InfoJobIdentity]; ok {
-                       // TODO: Update job
-               } else {
-                       jobs[ji.InfoJobIdentity] = ji
-               }
+func (jh *JobHandlerImpl) validateJobInfo(ji JobInfo) error {
+       if _, ok := jh.allTypes[ji.InfoTypeIdentity]; !ok {
+               return fmt.Errorf("type not supported: %v", ji.InfoTypeIdentity)
+       }
+       if ji.InfoJobIdentity == "" {
+               return fmt.Errorf("missing required job identity: %v", ji)
+       }
+       // Temporary for when there are only REST callbacks needed
+       if ji.TargetUri == "" {
+               return fmt.Errorf("missing required target URI: %v", ji)
        }
        return nil
 }
 
-func GetTypes() ([]*Type, error) {
-       types := make([]*Type, 0, 1)
-       err := filepath.Walk(typeDir,
-               func(path string, info os.FileInfo, err error) error {
-                       if err != nil {
-                               return err
-                       }
-                       if strings.Contains(path, ".json") {
-                               if jobType, err := getType(path); err == nil {
-                                       types = append(types, jobType)
-                               }
-                       }
-                       return nil
-               })
+func (jh *JobHandlerImpl) GetTypes() ([]config.TypeDefinition, error) {
+       jh.mu.Lock()
+       defer jh.mu.Unlock()
+       typeDefsByte, err := os.ReadFile(jh.configFile)
+       if err != nil {
+               return nil, err
+       }
+       typeDefs := struct {
+               Types []config.TypeDefinition `json:"types"`
+       }{}
+       err = json.Unmarshal(typeDefsByte, &typeDefs)
        if err != nil {
                return nil, err
        }
-       return types, nil
+       for _, typeDef := range typeDefs.Types {
+               jh.allTypes[typeDef.Id] = TypeData{
+                       TypeId:        typeDef.Id,
+                       DMaaPTopicURL: typeDef.DmaapTopicURL,
+                       Jobs:          make(map[string]JobInfo),
+               }
+       }
+       return typeDefs.Types, nil
 }
 
-func GetSupportedTypes() []string {
+func (jh *JobHandlerImpl) GetSupportedTypes() []string {
+       jh.mu.Lock()
+       defer jh.mu.Unlock()
        supportedTypes := []string{}
-       for k := range allJobs {
+       for k := range jh.allTypes {
                supportedTypes = append(supportedTypes, k)
        }
        return supportedTypes
 }
 
-func AddJob(job JobInfo) error {
-       return Handler.AddJob(job)
+func (jh *JobHandlerImpl) RunJobs(mRAddress string) {
+       for {
+               jh.pollAndDistributeMessages(mRAddress)
+       }
 }
 
-func getType(path string) (*Type, error) {
-       fileName := filepath.Base(path)
-       typeName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
-
-       if typeSchema, err := os.ReadFile(path); err == nil {
-               typeInfo := Type{
-                       TypeId: typeName,
-                       Schema: string(typeSchema),
+func (jh *JobHandlerImpl) pollAndDistributeMessages(mRAddress string) {
+       jh.mu.Lock()
+       defer jh.mu.Unlock()
+       for typeId, typeInfo := range jh.allTypes {
+               log.Debugf("Processing jobs for type: %v", typeId)
+               messagesBody, error := restclient.Get(fmt.Sprintf("%v/%v", mRAddress, typeInfo.DMaaPTopicURL), jh.pollClient)
+               if error != nil {
+                       log.Warnf("Error getting data from MR. Cause: %v", error)
+                       continue
                }
-               if _, ok := allJobs[typeName]; !ok {
-                       allJobs[typeName] = make(map[string]JobInfo)
+               jh.distributeMessages(messagesBody, typeInfo)
+       }
+}
+
+func (jh *JobHandlerImpl) distributeMessages(messages []byte, typeInfo TypeData) {
+       if len(messages) > 2 {
+               for _, jobInfo := range typeInfo.Jobs {
+                       go jh.sendMessagesToConsumer(messages, jobInfo)
                }
-               return &typeInfo, nil
-       } else {
-               return nil, err
        }
 }
+
+func (jh *JobHandlerImpl) sendMessagesToConsumer(messages []byte, jobInfo JobInfo) {
+       log.Debugf("Processing job: %v", jobInfo.InfoJobIdentity)
+       if postErr := restclient.Post(jobInfo.TargetUri, messages, jh.distributeClient); postErr != nil {
+               log.Warnf("Error posting data for job: %v. Cause: %v", jobInfo, postErr)
+       }
+}
+
+func (jh *JobHandlerImpl) clearAll() {
+       jh.allTypes = make(map[string]TypeData)
+}