2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
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"`
43 type JobHandler interface {
50 allJobs = make(map[string]map[string]JobInfo)
54 Handler = newJobHandlerImpl()
57 type jobHandlerImpl struct{}
59 func newJobHandlerImpl() *jobHandlerImpl {
60 return &jobHandlerImpl{}
63 func (jh *jobHandlerImpl) AddJob(ji JobInfo) error {
64 if jobs, ok := allJobs[ji.InfoTypeIdentity]; ok {
65 if _, ok := jobs[ji.InfoJobIdentity]; ok {
68 jobs[ji.InfoJobIdentity] = ji
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 {
81 if strings.Contains(path, ".json") {
82 if jobType, err := getType(path); err == nil {
83 types = append(types, jobType)
94 func GetSupportedTypes() []string {
95 supportedTypes := []string{}
96 for k := range allJobs {
97 supportedTypes = append(supportedTypes, k)
102 func AddJob(job JobInfo) error {
103 return Handler.AddJob(job)
106 func getType(path string) (*Type, error) {
107 fileName := filepath.Base(path)
108 typeName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
110 if typeSchema, err := os.ReadFile(path); err == nil {
113 Schema: string(typeSchema),
115 if _, ok := allJobs[typeName]; !ok {
116 allJobs[typeName] = make(map[string]JobInfo)
118 return &typeInfo, nil