NONRTRIC - Implement DMaaP mediator producer service in Java
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / repository / Jobs.java
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 org.oran.dmaapadapter.repository;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Vector;
29
30 import org.oran.dmaapadapter.clients.AsyncRestClient;
31 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
32 import org.oran.dmaapadapter.configuration.ApplicationConfig;
33 import org.oran.dmaapadapter.exceptions.ServiceException;
34 import org.oran.dmaapadapter.repository.Job.Parameters;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class Jobs {
42     public interface Observer {
43         void onJobbAdded(Job job);
44
45         void onJobRemoved(Job job);
46     }
47
48     private static final Logger logger = LoggerFactory.getLogger(Jobs.class);
49
50     private Map<String, Job> allJobs = new HashMap<>();
51     private MultiMap<Job> jobsByType = new MultiMap<>();
52     private final AsyncRestClientFactory restclientFactory;
53     private final List<Observer> observers = new ArrayList<>();
54
55     public Jobs(@Autowired ApplicationConfig applicationConfig) {
56         restclientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
57     }
58
59     public synchronized Job getJob(String id) throws ServiceException {
60         Job job = allJobs.get(id);
61         if (job == null) {
62             throw new ServiceException("Could not find job: " + id);
63         }
64         return job;
65     }
66
67     public synchronized Job get(String id) {
68         return allJobs.get(id);
69     }
70
71     public void addJob(String id, String callbackUrl, InfoType type, String owner, String lastUpdated,
72             Parameters parameters) {
73         AsyncRestClient consumerRestClient = type.isUseHttpProxy() //
74                 ? restclientFactory.createRestClientUseHttpProxy(callbackUrl) //
75                 : restclientFactory.createRestClientNoHttpProxy(callbackUrl);
76         Job job = new Job(id, callbackUrl, type, owner, lastUpdated, parameters, consumerRestClient);
77         this.put(job);
78         synchronized (observers) {
79             this.observers.forEach(obs -> obs.onJobbAdded(job));
80         }
81     }
82
83     public void addObserver(Observer obs) {
84         synchronized (observers) {
85             this.observers.add(obs);
86         }
87     }
88
89     private synchronized void put(Job job) {
90         logger.debug("Put job: {}", job.getId());
91         allJobs.put(job.getId(), job);
92         jobsByType.put(job.getType().getId(), job.getId(), job);
93     }
94
95     public synchronized Iterable<Job> getAll() {
96         return new Vector<>(allJobs.values());
97     }
98
99     public synchronized Job remove(String id) {
100         Job job = allJobs.get(id);
101         if (job != null) {
102             remove(job);
103         }
104         return job;
105     }
106
107     public void remove(Job job) {
108         synchronized (this) {
109             this.allJobs.remove(job.getId());
110             jobsByType.remove(job.getType().getId(), job.getId());
111         }
112         synchronized (observers) {
113             this.observers.forEach(obs -> obs.onJobRemoved(job));
114         }
115     }
116
117     public synchronized int size() {
118         return allJobs.size();
119     }
120
121     public synchronized Collection<Job> getJobsForType(InfoType type) {
122         return jobsByType.get(type.getId());
123     }
124
125     public synchronized void clear() {
126         allJobs.clear();
127         jobsByType.clear();
128     }
129 }