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===================================
36 Owner string `json:"owner"`
37 LastUpdated string `json:"last_updated"`
38 InfoJobIdentity string `json:"info_job_identity"`
39 TargetUri string `json:"target_uri"`
40 InfoJobData string `json:"info_job_data"`
41 InfoTypeIdentity string `json:"info_type_identity"`
44 type JobHandler interface {
51 allJobs = make(map[string]map[string]JobInfo)
55 Handler = newJobHandlerImpl()
58 type jobHandlerImpl struct{}
60 func newJobHandlerImpl() *jobHandlerImpl {
61 return &jobHandlerImpl{}
64 func (jh *jobHandlerImpl) AddJob(ji JobInfo) error {
65 if err := validateJobInfo(ji); err == nil {
66 jobs := allJobs[ji.InfoTypeIdentity]
67 jobs[ji.InfoJobIdentity] = ji
74 func validateJobInfo(ji JobInfo) error {
75 if _, ok := allJobs[ji.InfoTypeIdentity]; !ok {
76 return fmt.Errorf("type not supported: %v", ji.InfoTypeIdentity)
78 if ji.InfoJobIdentity == "" {
79 return fmt.Errorf("missing required job identity: %v", ji)
81 // Temporary for when there are only REST callbacks needed
82 if ji.TargetUri == "" {
83 return fmt.Errorf("missing required target URI: %v", ji)
88 func GetTypes() ([]*Type, error) {
89 types := make([]*Type, 0, 1)
90 err := filepath.Walk(typeDir,
91 func(path string, info os.FileInfo, err error) error {
95 if strings.Contains(path, ".json") {
96 if jobType, err := getType(path); err == nil {
97 types = append(types, jobType)
108 func GetSupportedTypes() []string {
109 supportedTypes := []string{}
110 for k := range allJobs {
111 supportedTypes = append(supportedTypes, k)
113 return supportedTypes
116 func AddJob(job JobInfo) error {
117 return Handler.AddJob(job)
120 func getType(path string) (*Type, error) {
121 fileName := filepath.Base(path)
122 typeName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
124 if typeSchema, err := os.ReadFile(path); err == nil {
127 Schema: string(typeSchema),
129 if _, ok := allJobs[typeName]; !ok {
130 allJobs[typeName] = make(map[string]JobInfo)
132 return &typeInfo, nil
139 allJobs = make(map[string]map[string]JobInfo)