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