Merge "Test updates for ECS, PMS and SDNC"
[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         "fmt"
25         "os"
26         "path/filepath"
27         "strings"
28 )
29
30 type Type struct {
31         TypeId string
32         Schema string
33 }
34
35 type JobInfo struct {
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"`
42 }
43
44 type JobHandler interface {
45         AddJob(JobInfo) error
46 }
47
48 var (
49         typeDir = "configs"
50         Handler JobHandler
51         allJobs = make(map[string]map[string]JobInfo)
52 )
53
54 func init() {
55         Handler = newJobHandlerImpl()
56 }
57
58 type jobHandlerImpl struct{}
59
60 func newJobHandlerImpl() *jobHandlerImpl {
61         return &jobHandlerImpl{}
62 }
63
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
68                 return nil
69         } else {
70                 return err
71         }
72 }
73
74 func validateJobInfo(ji JobInfo) error {
75         if _, ok := allJobs[ji.InfoTypeIdentity]; !ok {
76                 return fmt.Errorf("type not supported: %v", ji.InfoTypeIdentity)
77         }
78         if ji.InfoJobIdentity == "" {
79                 return fmt.Errorf("missing required job identity: %v", ji)
80         }
81         // Temporary for when there are only REST callbacks needed
82         if ji.TargetUri == "" {
83                 return fmt.Errorf("missing required target URI: %v", ji)
84         }
85         return nil
86 }
87
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 {
92                         if err != nil {
93                                 return err
94                         }
95                         if strings.Contains(path, ".json") {
96                                 if jobType, err := getType(path); err == nil {
97                                         types = append(types, jobType)
98                                 }
99                         }
100                         return nil
101                 })
102         if err != nil {
103                 return nil, err
104         }
105         return types, nil
106 }
107
108 func GetSupportedTypes() []string {
109         supportedTypes := []string{}
110         for k := range allJobs {
111                 supportedTypes = append(supportedTypes, k)
112         }
113         return supportedTypes
114 }
115
116 func AddJob(job JobInfo) error {
117         return Handler.AddJob(job)
118 }
119
120 func getType(path string) (*Type, error) {
121         fileName := filepath.Base(path)
122         typeName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
123
124         if typeSchema, err := os.ReadFile(path); err == nil {
125                 typeInfo := Type{
126                         TypeId: typeName,
127                         Schema: string(typeSchema),
128                 }
129                 if _, ok := allJobs[typeName]; !ok {
130                         allJobs[typeName] = make(map[string]JobInfo)
131                 }
132                 return &typeInfo, nil
133         } else {
134                 return nil, err
135         }
136 }
137
138 func clearAll() {
139         allJobs = make(map[string]map[string]JobInfo)
140 }