Approval Batch, prep better JUnit
[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  * Modifications Copyright © 2018 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  */
22
23 package org.onap.aaf.auth.batch.helpers;
24
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.GregorianCalendar;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33
34 import org.onap.aaf.cadi.Access;
35
36 public class ExpireRange {
37         private static final String AAF_BATCH_RANGE = "aaf_batch_range.";
38         public Map<String,List<Range>> ranges;
39         public final Date now;
40         public String rangeOneMonth = "OneMonth";
41         private Range delRange;
42         
43         public ExpireRange(final Access access) {
44                 now = new Date();
45                 ranges = new HashMap<>();
46                 int i=0;
47                 String prop = access.getProperty(AAF_BATCH_RANGE + i,null);
48                 if(prop==null && i==0) {
49                                 List<Range> lcred = getRangeList("cred");
50                                 List<Range> lur = getRangeList("ur");
51                                 List<Range> lx509 = getRangeList("x509");
52                                 
53                                 delRange = new Range("Delete",0,0,-1,0,GregorianCalendar.WEEK_OF_MONTH,-2);
54                                 lur.add(delRange);
55                                 lcred.add(delRange);
56                                 lx509.add(delRange);
57                                 
58                                 lcred.add(new Range("CredOneWeek",3,1,0,0,GregorianCalendar.WEEK_OF_MONTH,1));
59                                 lcred.add(new Range("CredTwoWeek",2,1,GregorianCalendar.WEEK_OF_MONTH,1,GregorianCalendar.WEEK_OF_MONTH,2));
60                                 lcred.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
61                                 lcred.add(new Range("TwoMonth",1,0,GregorianCalendar.MONTH,1,GregorianCalendar.MONTH,2));
62                                 
63                                 lur.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
64                                 
65                                 lx509.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
66                         }
67         }
68         
69         public Set<String> names() {
70                 Set<String> names = new HashSet<>();
71         for(List<Range> lr : ranges.values()) {
72                 for(Range r : lr) {
73                         names.add(r.name);
74                 }
75         }
76
77                 return names;
78         }
79         
80         private synchronized List<Range> getRangeList(final String key) {
81                 List<Range> rv = ranges.get(key);
82                 if(rv==null) {
83                         rv = new ArrayList<>();
84                         ranges.put(key, rv);
85                 }
86                 return rv;
87         }
88         
89         public class Range {
90                 private final String name;
91                 private final int reportingLevel;
92                 private final int interval; // in Days
93                 private final Date start;
94                 private final Date end;
95                 
96                 public Range(
97                                 final String name, final int reportingLevel, final int interval,  
98                                 final int startGCType, final int startQty,  
99                                 final int endGCType,final int endQty) {
100                         this.name = name;
101                         this.reportingLevel = reportingLevel;
102                         this.interval = interval;
103                         GregorianCalendar gc = new GregorianCalendar();
104                         if(startGCType<0) {
105                                 gc.set(GregorianCalendar.YEAR, 1);
106                         } else {
107                                 gc.setTime(now);
108                                 gc.add(startGCType, startQty);
109                         }
110                         start = gc.getTime();
111                         
112                         if(endGCType<0) {
113                                 gc.set(GregorianCalendar.YEAR, 1);
114                         } else {
115                                 gc.setTime(now);
116                                 gc.add(endGCType, endQty);
117                         }
118                         end = gc.getTime();
119                 }
120                 
121                 public String name() {
122                         return name;
123                 }
124                 
125                 public int reportingLevel() {
126                         return reportingLevel;
127                 }
128
129                 public Date getStart() {
130                         return start;
131                 }
132                 
133                 public Date getEnd() {
134                         return end;
135                 }
136                 
137                 private boolean inRange(final Date date) {
138                         if(date==null) {
139                                 return false;
140                         } else {
141                                 return date.getTime()>=start.getTime() && date.before(end);
142                         }
143                 }
144
145                 public boolean shouldContact(final Date lastContact) {
146                         if(reportingLevel<=0) {
147                                 return false;
148                         } else if(lastContact==null) {
149                                 return true;
150                         } else if(interval==0) {
151                                 return lastContact.before(start);
152                         } else {
153                                 GregorianCalendar gc = new GregorianCalendar();
154                                 gc.setTime(now);
155                                 gc.add(GregorianCalendar.DAY_OF_WEEK, interval);
156                                 return lastContact.before(gc.getTime());
157                         }
158                 }
159         }
160
161         public Range getRange(final String key, final Date date) {
162                 Range rv = null;
163                 if(date!=null) {
164                         List<Range> lr = ranges.get(key);
165                         if(lr==null) {
166                                 return null;
167                         } else {
168                                 for(Range r : lr) {
169                                         if(r.inRange(date)) {
170                                                 rv = r;
171                                                 break;
172                                         }
173                                 }
174                         }
175                 }
176                 return rv;
177         }
178         
179
180 }