Enrichment Coordination Service
[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.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Vector;
28
29 import org.oransc.enrichment.exceptions.ServiceException;
30
31 /**
32  * Dynamic representation of all EI Jobs in the system.
33  */
34 public class EiJobs {
35     private Map<String, EiJob> allEiJobs = new HashMap<>();
36     private Map<String, Map<String, EiJob>> jobsByType = new HashMap<>();
37
38     public synchronized void put(EiJob job) {
39         allEiJobs.put(job.id(), job);
40         multiMapPut(this.jobsByType, job.typeId(), job);
41     }
42
43     public synchronized Collection<EiJob> getJobs() {
44         return new Vector<>(allEiJobs.values());
45     }
46
47     public synchronized EiJob getJob(String id) throws ServiceException {
48         EiJob ric = allEiJobs.get(id);
49         if (ric == null) {
50             throw new ServiceException("Could not find EI Job: " + id);
51         }
52         return ric;
53     }
54
55     public synchronized Collection<EiJob> getJobsForType(String typeId) {
56         return multiMapGet(this.jobsByType, typeId);
57     }
58
59     public synchronized EiJob get(String id) {
60         return allEiJobs.get(id);
61     }
62
63     public synchronized EiJob remove(String id) {
64         EiJob job = allEiJobs.get(id);
65         if (job != null) {
66             remove(job);
67         }
68         return job;
69     }
70
71     public synchronized void remove(EiJob job) {
72         this.allEiJobs.remove(job.id());
73         multiMapRemove(this.jobsByType, job.typeId(), job);
74     }
75
76     public synchronized int size() {
77         return allEiJobs.size();
78     }
79
80     public synchronized void clear() {
81         this.allEiJobs.clear();
82     }
83
84     private void multiMapPut(Map<String, Map<String, EiJob>> multiMap, String key, EiJob value) {
85         multiMap.computeIfAbsent(key, k -> new HashMap<>()).put(value.id(), value);
86     }
87
88     private void multiMapRemove(Map<String, Map<String, EiJob>> multiMap, String key, EiJob value) {
89         Map<String, EiJob> map = multiMap.get(key);
90         if (map != null) {
91             map.remove(value.id());
92             if (map.isEmpty()) {
93                 multiMap.remove(key);
94             }
95         }
96     }
97
98     private Collection<EiJob> multiMapGet(Map<String, Map<String, EiJob>> multiMap, String key) {
99         Map<String, EiJob> map = multiMap.get(key);
100         if (map == null) {
101             return Collections.emptyList();
102         }
103         return new Vector<>(map.values());
104     }
105
106 }