Added support for EiJob status
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / repository / EiJobs.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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 org.oransc.enrichment.repository;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import org.oransc.enrichment.exceptions.ServiceException;
29
30 /**
31  * Dynamic representation of all existing EI jobs.
32  */
33 public class EiJobs {
34     private Map<String, EiJob> allEiJobs = new HashMap<>();
35
36     private MultiMap<EiJob> jobsByType = new MultiMap<>();
37     private MultiMap<EiJob> jobsByOwner = new MultiMap<>();
38
39     public synchronized void put(EiJob job) {
40         allEiJobs.put(job.id(), job);
41         jobsByType.put(job.typeId(), job.id(), job);
42         jobsByOwner.put(job.owner(), job.id(), job);
43     }
44
45     public synchronized Collection<EiJob> getJobs() {
46         return new Vector<>(allEiJobs.values());
47     }
48
49     public synchronized EiJob getJob(String id) throws ServiceException {
50         EiJob ric = allEiJobs.get(id);
51         if (ric == null) {
52             throw new ServiceException("Could not find EI job: " + id);
53         }
54         return ric;
55     }
56
57     public synchronized Collection<EiJob> getJobsForType(String typeId) {
58         return jobsByType.get(typeId);
59     }
60
61     public synchronized Collection<EiJob> getJobsForType(EiType type) {
62         return jobsByType.get(type.getId());
63     }
64
65     public synchronized Collection<EiJob> getJobsForOwner(String owner) {
66         return jobsByOwner.get(owner);
67     }
68
69     public synchronized EiJob get(String id) {
70         return allEiJobs.get(id);
71     }
72
73     public synchronized EiJob remove(String id) {
74         EiJob job = allEiJobs.get(id);
75         if (job != null) {
76             remove(job);
77         }
78         return job;
79     }
80
81     public synchronized void remove(EiJob job) {
82         this.allEiJobs.remove(job.id());
83         jobsByType.remove(job.typeId(), job.id());
84         jobsByOwner.remove(job.owner(), job.id());
85     }
86
87     public synchronized int size() {
88         return allEiJobs.size();
89     }
90
91     public synchronized void clear() {
92         this.allEiJobs.clear();
93         this.jobsByType.clear();
94         jobsByOwner.clear();
95     }
96
97 }