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.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import lombok.Getter;
27
28 import org.immutables.gson.Gson;
29
30 public class Job {
31
32     @Gson.TypeAdapters
33     public static class Parameters {
34         public String filter;
35         public BufferTimeout bufferTimeout;
36
37         public Parameters() {
38         }
39
40         public Parameters(String filter, BufferTimeout bufferTimeout) {
41             this.filter = filter;
42             this.bufferTimeout = bufferTimeout;
43         }
44
45         public static class BufferTimeout {
46             public BufferTimeout(int maxSize, int maxTimeMiliseconds) {
47                 this.maxSize = maxSize;
48                 this.maxTimeMiliseconds = maxTimeMiliseconds;
49             }
50
51             public BufferTimeout() {
52             }
53
54             public int maxSize;
55             public int maxTimeMiliseconds;
56         }
57     }
58
59     @Getter
60     private final String id;
61
62     @Getter
63     private final String callbackUrl;
64
65     @Getter
66     private final InfoType type;
67
68     @Getter
69     private final String owner;
70
71     @Getter
72     private final Parameters parameters;
73
74     @Getter
75     private final String lastUpdated;
76
77     private final Pattern jobDataFilter;
78
79     public Job(String id, String callbackUrl, InfoType type, String owner, String lastUpdated, Parameters parameters) {
80         this.id = id;
81         this.callbackUrl = callbackUrl;
82         this.type = type;
83         this.owner = owner;
84         this.lastUpdated = lastUpdated;
85         this.parameters = parameters;
86         if (parameters != null && parameters.filter != null) {
87             jobDataFilter = Pattern.compile(parameters.filter);
88         } else {
89             jobDataFilter = null;
90         }
91     }
92
93     public boolean isFilterMatch(String data) {
94         if (jobDataFilter == null) {
95             return true;
96         }
97         Matcher matcher = jobDataFilter.matcher(data);
98         return matcher.find();
99     }
100
101     public boolean isBuffered() {
102         return parameters != null && parameters.bufferTimeout != null && parameters.bufferTimeout.maxSize > 0
103                 && parameters.bufferTimeout.maxTimeMiliseconds > 0;
104     }
105
106 }