NONRTRIC - Implement DMaaP mediator producer service in Java
[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         public Parameters() {}
42
43         public Parameters(String filter, BufferTimeout bufferTimeout) {
44             this.filter = filter;
45             this.bufferTimeout = bufferTimeout;
46         }
47     }
48
49     @Gson.TypeAdapters
50     public static class BufferTimeout {
51         public BufferTimeout(int maxSize, int maxTimeMiliseconds) {
52             this.maxSize = maxSize;
53             this.maxTimeMiliseconds = maxTimeMiliseconds;
54         }
55
56         public BufferTimeout() {}
57
58         @Getter
59         private int maxSize;
60
61         private int maxTimeMiliseconds;
62
63         public Duration getMaxTime() {
64             return Duration.ofMillis(maxTimeMiliseconds);
65         }
66     }
67
68     @Getter
69     private final String id;
70
71     @Getter
72     private final String callbackUrl;
73
74     @Getter
75     private final InfoType type;
76
77     @Getter
78     private final String owner;
79
80     @Getter
81     private final Parameters parameters;
82
83     @Getter
84     private final String lastUpdated;
85
86     private final Pattern jobDataFilter;
87
88     @Getter
89     private final AsyncRestClient consumerRestClient;
90
91     public Job(String id, String callbackUrl, InfoType type, String owner, String lastUpdated, Parameters parameters,
92             AsyncRestClient consumerRestClient) {
93         this.id = id;
94         this.callbackUrl = callbackUrl;
95         this.type = type;
96         this.owner = owner;
97         this.lastUpdated = lastUpdated;
98         this.parameters = parameters;
99         if (parameters != null && parameters.filter != null) {
100             jobDataFilter = Pattern.compile(parameters.filter);
101         } else {
102             jobDataFilter = null;
103         }
104         this.consumerRestClient = consumerRestClient;
105     }
106
107     public boolean isFilterMatch(String data) {
108         if (jobDataFilter == null) {
109             return true;
110         }
111         Matcher matcher = jobDataFilter.matcher(data);
112         return matcher.find();
113     }
114
115     public boolean isBuffered() {
116         return parameters != null && parameters.bufferTimeout != null && parameters.bufferTimeout.maxSize > 0
117                 && parameters.bufferTimeout.maxTimeMiliseconds > 0;
118     }
119
120 }