3c246db643def23eb43f1c1847d2a87d5162fef4
[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.auth.org.Organization.Identity;
35 import org.onap.aaf.cadi.Access;
36
37 public class ExpireRange {
38     private static final String DELETE = "Delete";
39     public static final String ONE_MONTH = "OneMonth";
40     public static final String TWO_MONTH = "TwoMonth";
41     public static final String TWO_WEEK = "TwoWeek";
42     public static final String ONE_WEEK = "OneWeek";
43     private static final String AAF_BATCH_RANGE = "aaf_batch_range.";
44     public final Map<String,List<Range>> ranges;
45     private final Map<Integer,Date> intervalDates;
46     private static final Date now = new Date();
47     public final Range approveDelete ;
48
49     private Range delRange;
50
51     public ExpireRange(final Access access) {
52         ranges = new HashMap<>();
53         intervalDates = new HashMap<>();
54         int i=0;
55         approveDelete = new Range(DELETE,0,0,0,-1,0,GregorianCalendar.DAY_OF_YEAR,-100);
56         String prop = access.getProperty(AAF_BATCH_RANGE + i,null);
57         if(prop==null && i==0) {
58                 List<Range> lcred = getRangeList("cred");
59                 List<Range> lur = getRangeList("ur");
60                 List<Range> lx509 = getRangeList("x509");
61
62
63                 /*
64                    Range(Name, ReportingLevel, PeopleInterval, AppInterval, Start(Type,Qty) End(Type,Qty) )
65                    Interval of -1 Means "only once"
66                    Interval of 0 means none
67                    Interval > 0 means only X number of Days.
68                 */
69                 delRange = new Range(DELETE,0,0,0,-1,0,GregorianCalendar.WEEK_OF_MONTH,-2);
70                 lur.add(delRange);
71                 lcred.add(delRange);
72                 lx509.add(delRange);
73
74                 lcred.add(new Range(ONE_WEEK ,3,-1,1,0,0,GregorianCalendar.WEEK_OF_MONTH,1));
75                 lcred.add(new Range(TWO_WEEK ,2,-1,-1,GregorianCalendar.WEEK_OF_MONTH,1,GregorianCalendar.WEEK_OF_MONTH,2));
76                 lcred.add(new Range(ONE_MONTH,1,7,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
77                 lcred.add(new Range(TWO_MONTH,1,-1,-1,GregorianCalendar.MONTH,1,GregorianCalendar.MONTH,2));
78
79                 lur.add(  new Range(ONE_MONTH,1,-1,-1,0,0,GregorianCalendar.MONTH,1));
80                 lx509.add(new Range(ONE_MONTH,1,-1,-1,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1));
81             }
82     }
83
84     public Range newFutureRange() {
85         return new Range("Approval",7,7,1,0,0,GregorianCalendar.MONTH,1);
86     }
87
88     public Set<String> names() {
89         Set<String> names = new HashSet<>();
90         for(List<Range> lr : ranges.values()) {
91             for(Range r : lr) {
92                 names.add(r.name);
93             }
94         }
95
96         return names;
97     }
98
99     private synchronized List<Range> getRangeList(final String key) {
100         List<Range> rv = ranges.get(key);
101         if(rv==null) {
102             rv = new ArrayList<>();
103             ranges.put(key, rv);
104         }
105         return rv;
106     }
107
108     public class Range {
109         private final String name;
110         private final int reportingLevel;
111         private final int peopleInterval; // in Days
112         private final int appInterval; // in Days
113         private final Date start;
114         private final Date end;
115         private final Date lowerValid;
116
117         public Range(
118                 final String name, final int reportingLevel,
119                 final int peopleInterval, final int appInterval,
120                 final int startGCType, final int startQty,
121                 final int endGCType,final int endQty) {
122             this.name = name;
123             this.reportingLevel = reportingLevel;
124             this.peopleInterval = peopleInterval;
125             this.appInterval = appInterval;
126             GregorianCalendar gc = new GregorianCalendar();
127             if(startGCType<0) {
128                 gc.set(GregorianCalendar.YEAR, 1);
129             } else {
130                 gc.setTime(now);
131                 gc.add(startGCType, startQty);
132             }
133             start = gc.getTime();
134
135             if(endGCType<0) {
136                 gc.set(GregorianCalendar.YEAR, 1);
137             } else {
138                 gc.setTime(now);
139                 gc.add(endGCType, endQty);
140             }
141             end = gc.getTime();
142
143
144             if(endGCType<0) {
145                 gc.set(GregorianCalendar.YEAR, -1);
146             } else {
147                 gc.setTime(now);
148                 gc.add(endGCType, endQty * -1);
149             }
150             lowerValid = gc.getTime();
151
152         }
153
154         public String name() {
155             return name;
156         }
157
158         public int reportingLevel() {
159             return reportingLevel;
160         }
161
162         public boolean needsContact(Date lnd, Identity identity) {
163             final int interval;
164             if(identity==null || identity.isPerson()) {
165                 interval = peopleInterval;
166             } else {
167                 interval = appInterval;
168             }
169             if(interval == 0) {
170                 return false;
171             } else if(interval < 0) { // "-1 = only once "
172                 return (lnd==null || lnd.before(lowerValid));
173             } else {
174                 Date rv = intervalDates.get(interval);
175                 if(rv==null) {
176                     GregorianCalendar gc = new GregorianCalendar();
177                     gc.setTime(now);
178                     gc.add(GregorianCalendar.DAY_OF_YEAR, -1*interval);
179                     rv = gc.getTime();
180                     intervalDates.put(interval, rv);
181                 }
182                 return rv.after(lnd);
183             }
184         }
185
186         public Date getStart() {
187             return start;
188         }
189
190         public Date getEnd() {
191             return end;
192         }
193
194         public boolean inRange(final Date date) {
195             if(date==null) {
196                 return false;
197             } else {
198                 return date.getTime()>=start.getTime() && date.before(end);
199             }
200         }
201
202     }
203
204     public Range getRange(final String key, final Date date) {
205         Range rv = null;
206         if(date!=null) {
207             List<Range> lr = ranges.get(key);
208             if(lr==null) {
209                 return null;
210             } else {
211                 for(Range r : lr) {
212                     if(r.inRange(date)) {
213                         rv = r;
214                         break;
215                     }
216                 }
217             }
218         }
219         return rv;
220     }
221
222     public Date now() {
223         return now;
224     }
225
226
227 }