AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / helpers / MonthData.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.helpers;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Map.Entry;
32
33 import org.onap.aaf.misc.env.util.Split;
34
35 import java.util.Set;
36 import java.util.TreeMap;
37
38 public class MonthData {
39     public final Map<Integer,Set<Row>> data = 
40                 new TreeMap<Integer,Set<Row>>();
41         private File f;
42     
43     public MonthData(String env) throws IOException {
44         f = new File("Monthly"+env+".dat");
45         
46         if(f.exists()) {
47                 BufferedReader br = new BufferedReader(new FileReader(f));
48                 try {
49                         String line;
50                         String[] split;
51                         while((line=br.readLine())!=null) {
52                                 if(!line.startsWith("#")) {
53                                         split = Split.split(',', line);
54                                         if(split.length==5) {
55                                                 add(Integer.parseInt(split[0]),split[1],
56                                                         Integer.parseInt(split[2]),
57                                                         Integer.parseInt(split[3]),
58                                                         Integer.parseInt(split[4])
59                                                 );
60                                         }
61                                 }
62                         }
63                 } finally {
64                         br.close();
65                 }
66         }
67     }
68     
69     public void add(int yr_mon, String target, long total, long adds, long drops) {
70                 Set<Row> row = data.get(yr_mon);
71                 if(row==null) {
72                         data.put(yr_mon, (row=new HashSet<Row>()));
73                 }
74                 row.add(new Row(target,total,adds,drops));
75         }
76     
77     public boolean notExists(int yr_mon) {
78         return data.get(yr_mon)==null;
79     }
80     
81         public static class Row implements Comparable<Row> {
82         public final String target;
83         public final long total;
84         public final long adds;
85         public final long drops;
86         
87         public Row(String t, long it, long a, long d) {
88                 target = t;
89                 total = it;
90                 adds = a;
91                 drops = d;
92         }
93
94                 @Override
95                 public int compareTo(Row o) {
96                         return target.compareTo(o.target);
97                 }
98                 
99                 public String toString() {
100                         return target + '|' + total + '|' + drops + '|' + adds;
101                 }
102     }
103
104     public void write() throws IOException {
105         if(f.exists()) {
106                 File bu = new File(f.getName()+".bak");
107                 f.renameTo(bu);
108         }
109                 PrintStream ps = new PrintStream(f);
110                 try {
111                         for( Entry<Integer, Set<Row>> rows : data.entrySet()) {
112                                 for(Row row : rows.getValue()) {
113                                         ps.printf("%d,%s,%d,%d,%d\n",rows.getKey(),row.target,row.total,row.adds,row.drops);
114                                 }
115                         }
116                 } finally {
117                         ps.close();
118                 }
119     }
120
121 }