5f7521c37fabfb6af267d7c1a8043292913f5995
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / repository / Job.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.time.Duration;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import lombok.Getter;
28
29 import org.immutables.gson.Gson;
30 import org.oran.dmaapadapter.clients.AsyncRestClient;
31
32 public class Job {
33
34     @Gson.TypeAdapters
35     public static class Parameters {
36         @Getter
37         private String filter;
38         @Getter
39         private BufferTimeout bufferTimeout;
40
41         private int maxConcurrency;
42
43         public Parameters() {}
44
45         public Parameters(String filter, BufferTimeout bufferTimeout, int maxConcurrency) {
46             this.filter = filter;
47             this.bufferTimeout = bufferTimeout;
48             this.maxConcurrency = maxConcurrency;
49         }
50
51         public int getMaxConcurrency() {
52             return maxConcurrency == 0 ? 1 : maxConcurrency;
53         }
54     }
55
56     @Gson.TypeAdapters
57     public static class BufferTimeout {
58         public BufferTimeout(int maxSize, long maxTimeMiliseconds) {
59             this.maxSize = maxSize;
60             this.maxTimeMiliseconds = maxTimeMiliseconds;
61         }
62
63         public BufferTimeout() {}
64
65         @Getter
66         private int maxSize;
67
68         private long maxTimeMiliseconds;
69
70         public Duration getMaxTime() {
71             return Duration.ofMillis(maxTimeMiliseconds);
72         }
73     }
74
75     @Getter
76     private final String id;
77
78     @Getter
79     private final String callbackUrl;
80
81     @Getter
82     private final InfoType type;
83
84     @Getter
85     private final String owner;
86
87     @Getter
88     private final Parameters parameters;
89
90     @Getter
91     private final String lastUpdated;
92
93     private final Pattern jobDataFilter;
94
95     @Getter
96     private final AsyncRestClient consumerRestClient;
97
98     public Job(String id, String callbackUrl, InfoType type, String owner, String lastUpdated, Parameters parameters,
99             AsyncRestClient consumerRestClient) {
100         this.id = id;
101         this.callbackUrl = callbackUrl;
102         this.type = type;
103         this.owner = owner;
104         this.lastUpdated = lastUpdated;
105         this.parameters = parameters;
106         if (parameters != null && parameters.filter != null) {
107             jobDataFilter = Pattern.compile(parameters.filter);
108         } else {
109             jobDataFilter = null;
110         }
111         this.consumerRestClient = consumerRestClient;
112     }
113
114     public boolean isFilterMatch(String data) {
115         if (jobDataFilter == null) {
116             return true;
117         }
118         Matcher matcher = jobDataFilter.matcher(data);
119         return matcher.find();
120     }
121
122     public boolean isBuffered() {
123         return parameters != null && parameters.bufferTimeout != null && parameters.bufferTimeout.maxSize > 0
124                 && parameters.bufferTimeout.maxTimeMiliseconds > 0;
125     }
126
127 }