Initial job creation
[nonrtric.git] / dmaap-mediator-producer / internal / jobs / jobs.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package jobs
22
23 import (
24         "os"
25         "path/filepath"
26         "strings"
27 )
28
29 type Type struct {
30         TypeId string
31         Schema string
32 }
33
34 type JobInfo struct {
35         Owner            string `json:"owner"`
36         LastUpdated      string `json:"last_updated"`
37         InfoJobIdentity  string `json:"info_job_identity"`
38         TargetUri        string `json:"target_uri"`
39         InfoJobData      string `json:"info_job_data"`
40         InfoTypeIdentity string `json:"info_type_identity"`
41 }
42
43 type JobHandler interface {
44         AddJob(JobInfo) error
45 }
46
47 var (
48         typeDir = "configs"
49         Handler JobHandler
50         allJobs = make(map[string]map[string]JobInfo)
51 )
52
53 func init() {
54         Handler = newJobHandlerImpl()
55 }
56
57 type jobHandlerImpl struct{}
58
59 func newJobHandlerImpl() *jobHandlerImpl {
60         return &jobHandlerImpl{}
61 }
62
63 func (jh *jobHandlerImpl) AddJob(ji JobInfo) error {
64         if jobs, ok := allJobs[ji.InfoTypeIdentity]; ok {
65                 if _, ok := jobs[ji.InfoJobIdentity]; ok {
66                         // TODO: Update job
67                 } else {
68                         jobs[ji.InfoJobIdentity] = ji
69                 }
70         }
71         return nil
72 }
73
74 func GetTypes() ([]*Type, error) {
75         types := make([]*Type, 0, 1)
76         err := filepath.Walk(typeDir,
77                 func(path string, info os.FileInfo, err error) error {
78                         if err != nil {
79                                 return err
80                         }
81                         if strings.Contains(path, ".json") {
82                                 if jobType, err := getType(path); err == nil {
83                                         types = append(types, jobType)
84                                 }
85                         }
86                         return nil
87                 })
88         if err != nil {
89                 return nil, err
90         }
91         return types, nil
92 }
93
94 func GetSupportedTypes() []string {
95         supportedTypes := []string{}
96         for k := range allJobs {
97                 supportedTypes = append(supportedTypes, k)
98         }
99         return supportedTypes
100 }
101
102 func AddJob(job JobInfo) error {
103         return Handler.AddJob(job)
104 }
105
106 func getType(path string) (*Type, error) {
107         fileName := filepath.Base(path)
108         typeName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
109
110         if typeSchema, err := os.ReadFile(path); err == nil {
111                 typeInfo := Type{
112                         TypeId: typeName,
113                         Schema: string(typeSchema),
114                 }
115                 if _, ok := allJobs[typeName]; !ok {
116                         allJobs[typeName] = make(map[string]JobInfo)
117                 }
118                 return &typeInfo, nil
119         } else {
120                 return nil, err
121         }
122 }