Merge "Further Batch Refactor"
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / ExpireRange.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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.onap.aaf.auth.batch.helpers;
22
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.GregorianCalendar;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.onap.aaf.cadi.Access;
33 import org.onap.aaf.cadi.PropAccess;
34
35 public class ExpireRange {
36         private static final String AAF_BATCH_RANGE = "aaf_batch_range.";
37         public Map<String,List<Range>> ranges;
38         public final Date now;
39         
40         public ExpireRange(final Access access) {
41                 now = new Date();
42                 ranges = new HashMap<>();
43                 int i=0;
44                 String prop = access.getProperty(AAF_BATCH_RANGE + i,null);
45                 if(prop==null) {
46                         if(i==0) {
47                                 List<Range> lcred = getRangeList("cred");
48                                 List<Range> lur = getRangeList("ur");
49                                 List<Range> lx509 = getRangeList("x509");
50                                 
51                                 Range del = new Range("Delete",0,0,-1,0,GregorianCalendar.WEEK_OF_MONTH,-2);
52                                 lur.add(del);
53                                 lcred.add(del);
54                                 lx509.add(del);
55                                 
56                                 lcred.add(new Range("CredOneWeek",3,1,0,0,GregorianCalendar.WEEK_OF_MONTH,1));
57                                 lcred.add(new Range("CredTwoWeek",2,1,GregorianCalendar.WEEK_OF_MONTH,1,GregorianCalendar.WEEK_OF_MONTH,2));
58                                 lcred.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
59                                 lcred.add(new Range("TwoMonth",1,0,GregorianCalendar.MONTH,1,GregorianCalendar.MONTH,2));
60                                 
61                                 lur.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
62                                 
63                                 lx509.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
64                         }
65                 }
66         }
67         
68         public Set<String> names() {
69                 Set<String> names = new HashSet<>();
70         for(List<Range> lr : ranges.values()) {
71                 for(Range r : lr) {
72                         names.add(r.name);
73                 }
74         }
75
76                 return names;
77         }
78         
79         private synchronized List<Range> getRangeList(final String key) {
80                 List<Range> rv = ranges.get(key);
81                 if(rv==null) {
82                         rv = new ArrayList<>();
83                         ranges.put(key, rv);
84                 }
85                 return rv;
86         }
87         
88         public class Range {
89                 private final String name;
90                 private final int reportingLevel;
91                 private final int interval; // in Days
92                 private final Date start;
93                 private final Date end;
94                 
95                 public Range(
96                                 final String name, final int reportingLevel, final int interval,  
97                                 final int startGCType, final int startQty,  
98                                 final int endGCType,final int endQty) {
99                         this.name = name;
100                         this.reportingLevel = reportingLevel;
101                         this.interval = interval;
102                         GregorianCalendar gc = new GregorianCalendar();
103                         if(startGCType<0) {
104                                 gc.set(GregorianCalendar.YEAR, 1);
105                         } else {
106                                 gc.setTime(now);
107                                 gc.add(startGCType, startQty);
108                         }
109                         start = gc.getTime();
110                         
111                         if(endGCType<0) {
112                                 gc.set(GregorianCalendar.YEAR, 1);
113                         } else {
114                                 gc.setTime(now);
115                                 gc.add(endGCType, endQty);
116                         }
117                         end = gc.getTime();
118                 }
119                 
120                 public String name() {
121                         return name;
122                 }
123                 
124                 public int reportingLevel() {
125                         return reportingLevel;
126                 }
127
128                 public Date getStart() {
129                         return start;
130                 }
131                 
132                 public Date getEnd() {
133                         return end;
134                 }
135                 
136                 private boolean inRange(final Date date) {
137                         if(date==null) {
138                                 return false;
139                         } else {
140                                 return date.getTime()>=start.getTime() && date.before(end);
141                         }
142                 }
143
144                 public boolean shouldContact(final Date lastContact) {
145                         if(reportingLevel<=0) {
146                                 return false;
147                         } else if(lastContact==null) {
148                                 return true;
149                         } else if(interval==0) {
150                                 return lastContact.before(start);
151                         } else {
152                                 GregorianCalendar gc = new GregorianCalendar();
153                                 gc.setTime(now);
154                                 gc.add(GregorianCalendar.DAY_OF_WEEK, interval);
155                                 return lastContact.before(gc.getTime());
156                         }
157                 }
158         }
159
160         public Range getRange(final String key, final Date date) {
161                 Range rv = null;
162                 if(date!=null) {
163                         List<Range> lr = ranges.get(key);
164                         if(lr==null) {
165                                 return null;
166                         } else {
167                                 for(Range r : lr) {
168                                         if(r.inRange(date)) {
169                                                 rv = r;
170                                                 break;
171                                         }
172                                 }
173                         }
174                 }
175                 return rv;
176         }
177         
178
179 }