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