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