Batch work and client
[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         public static final String ONE_MONTH = "OneMonth";
38         public static final String TWO_MONTH = "TwoMonth";
39         public static final String TWO_WEEK = "TwoWeek";
40         public static final String ONE_WEEK = "OneWeek";
41         private static final String AAF_BATCH_RANGE = "aaf_batch_range.";
42         public Map<String,List<Range>> ranges;
43         public final Date now;
44
45         private Range delRange;
46         
47         public ExpireRange(final Access access) {
48                 now = new Date();
49                 ranges = new HashMap<>();
50                 int i=0;
51                 String prop = access.getProperty(AAF_BATCH_RANGE + i,null);
52                 if(prop==null && i==0) {
53                                 List<Range> lcred = getRangeList("cred");
54                                 List<Range> lur = getRangeList("ur");
55                                 List<Range> lx509 = getRangeList("x509");
56                                 
57                                 delRange = new Range("Delete",0,0,-1,0,GregorianCalendar.WEEK_OF_MONTH,-2);
58                                 lur.add(delRange);
59                                 lcred.add(delRange);
60                                 lx509.add(delRange);
61                                 
62                                 lcred.add(new Range(ONE_WEEK,3,1,0,0,GregorianCalendar.WEEK_OF_MONTH,1));
63                                 lcred.add(new Range(TWO_WEEK,2,1,GregorianCalendar.WEEK_OF_MONTH,1,GregorianCalendar.WEEK_OF_MONTH,2));
64                                 lcred.add(new Range(ONE_MONTH,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
65                                 lcred.add(new Range(TWO_MONTH,1,0,GregorianCalendar.MONTH,1,GregorianCalendar.MONTH,2));
66                                 
67                                 lur.add(new Range(ONE_MONTH,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
68                                 
69                                 lx509.add(new Range(ONE_MONTH,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
70                         }
71         }
72         
73         public Set<String> names() {
74                 Set<String> names = new HashSet<>();
75         for(List<Range> lr : ranges.values()) {
76                 for(Range r : lr) {
77                         names.add(r.name);
78                 }
79         }
80
81                 return names;
82         }
83         
84         private synchronized List<Range> getRangeList(final String key) {
85                 List<Range> rv = ranges.get(key);
86                 if(rv==null) {
87                         rv = new ArrayList<>();
88                         ranges.put(key, rv);
89                 }
90                 return rv;
91         }
92         
93         public class Range {
94                 private final String name;
95                 private final int reportingLevel;
96                 private final int interval; // in Days
97                 private final Date start;
98                 private final Date end;
99                 
100                 public Range(
101                                 final String name, final int reportingLevel, final int interval,  
102                                 final int startGCType, final int startQty,  
103                                 final int endGCType,final int endQty) {
104                         this.name = name;
105                         this.reportingLevel = reportingLevel;
106                         this.interval = interval;
107                         GregorianCalendar gc = new GregorianCalendar();
108                         if(startGCType<0) {
109                                 gc.set(GregorianCalendar.YEAR, 1);
110                         } else {
111                                 gc.setTime(now);
112                                 gc.add(startGCType, startQty);
113                         }
114                         start = gc.getTime();
115                         
116                         if(endGCType<0) {
117                                 gc.set(GregorianCalendar.YEAR, 1);
118                         } else {
119                                 gc.setTime(now);
120                                 gc.add(endGCType, endQty);
121                         }
122                         end = gc.getTime();
123                 }
124                 
125                 public String name() {
126                         return name;
127                 }
128                 
129                 public int reportingLevel() {
130                         return reportingLevel;
131                 }
132
133                 public Date getStart() {
134                         return start;
135                 }
136                 
137                 public Date getEnd() {
138                         return end;
139                 }
140                 
141                 private boolean inRange(final Date date) {
142                         if(date==null) {
143                                 return false;
144                         } else {
145                                 return date.getTime()>=start.getTime() && date.before(end);
146                         }
147                 }
148
149                 public boolean shouldContact(final Date lastContact) {
150                         if(reportingLevel<=0) {
151                                 return false;
152                         } else if(lastContact==null) {
153                                 return true;
154                         } else if(interval==0) {
155                                 return lastContact.before(start);
156                         } else {
157                                 GregorianCalendar gc = new GregorianCalendar();
158                                 gc.setTime(now);
159                                 gc.add(GregorianCalendar.DAY_OF_WEEK, interval);
160                                 return lastContact.before(gc.getTime());
161                         }
162                 }
163         }
164
165         public Range getRange(final String key, final Date date) {
166                 Range rv = null;
167                 if(date!=null) {
168                         List<Range> lr = ranges.get(key);
169                         if(lr==null) {
170                                 return null;
171                         } else {
172                                 for(Range r : lr) {
173                                         if(r.inRange(date)) {
174                                                 rv = r;
175                                                 break;
176                                         }
177                                 }
178                         }
179                 }
180                 return rv;
181         }
182         
183
184 }