a8c86fa503e4a5ab9d46ac3b5a93d0d11f7e8fd8
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cached / FileGetter.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.dao.cached;
23
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.TreeMap;
32
33 import org.onap.aaf.auth.dao.Cached.Getter;
34 import org.onap.aaf.auth.dao.cass.CredDAO;
35 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.cadi.Access;
38 import org.onap.aaf.cadi.Access.Level;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.Hash;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.util.CSV;
43
44 public class FileGetter {
45         private static final String AAF_FILEGETTER = "aaf_filegetter";
46         public static boolean isLoaded = false;
47         private static FileGetter singleton;
48
49         private Map<String,List<CredDAO.Data>> data; 
50         private SimpleDateFormat sdf;
51         private FileGetter(Access access) {
52                 if(access!=null) {
53                         String filename = access.getProperty(AAF_FILEGETTER,null);
54                         if((filename!=null)&&(!isLoaded)) { 
55                                         data = new TreeMap<>();
56                                         sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+SSSS");
57                                         CSV csv = new CSV(access, filename).setDelimiter('|');
58                                         try {
59                                                 access.log(Level.INIT, "Loading Filebased Cred from",filename);
60                                         csv.visit(row -> {
61                                                 if(row.size()<1) {
62                                                         access.log(Level.INIT, "Bad Row");
63                                                 }
64                                                 int type;
65                                                   try {
66                                                           type =Integer.parseInt(row.get(1));
67                                                   } catch(Exception e) {
68                                                           access.log(Level.INIT, e, "skipping ", row.get(0));
69                                                           return;
70                                                   }
71                                                 if(CredDAO.CERT_SHA256_RSA == type) {
72                                                         return;
73                                                 }
74                                                 CredDAO.Data cdd = new CredDAO.Data();
75                                                 cdd.id=row.get(0);
76                                                 cdd.type = type;
77                                                 try {
78                                                                 cdd.expires = sdf.parse(row.get(2));
79                                                                 cdd.cred = ByteBuffer.wrap(Hash.fromHex(row.get(3)));
80                                                                 cdd.notes= row.get(4);
81                                                                 cdd.ns = row.get(5);
82                                                                 cdd.other = Integer.parseInt(row.get(6));
83                                                                 if(row.size()>8) {
84                                                                         cdd.tag = row.get(8);
85                                                                 } else {
86                                                                         cdd.tag = "";
87                                                                 }
88                                                                 List<CredDAO.Data> lcdd = data.get(cdd.id);
89                                                                 if(lcdd == null) {
90                                                                         lcdd = new ArrayList<>();
91                                                                         data.put(cdd.id, lcdd);
92                                                                 }
93                                                                 lcdd.add(cdd);
94                                                                 
95                                                         } catch (ParseException e) {
96                                                                 access.log(Level.INIT, e);
97                                                         }
98                                                 
99                                         });
100                                         access.printf(Level.INIT, "Filebased Cred finished...");
101                                                 isLoaded = true;
102                                         } catch( CadiException | IOException e) {
103                                                 access.log(Level.ERROR, e);
104                                         }
105                                 
106                         }
107                 }
108         }
109
110         public static synchronized FileGetter singleton(Access access) {
111                 if(singleton==null) {
112                         singleton = new FileGetter(access);
113                 }
114                 return singleton;
115                 
116         }
117         public Getter<CredDAO.Data> getter(String id) {
118                 return new FGetter(id);
119         }
120         private static List<CredDAO.Data> EMPTY = new ArrayList<>(); 
121         public class FGetter implements Getter<CredDAO.Data> {
122                 private final List<CredDAO.Data> lcdd;  
123                 public FGetter(final String id) {
124                         lcdd = data.get(id);
125                 }
126                 @Override
127                 public Result<List<Data>> get() {
128                         return Result.ok(lcdd==null?EMPTY:lcdd);
129                 }
130         }
131         
132         public static void main(String[] args) {
133                 PropAccess access = new PropAccess(args);
134                 access.setProperty(AAF_FILEGETTER,"/Users/jg1555/cred.dat");
135                 FileGetter fg = FileGetter.singleton(access);
136                 
137                 for(String id : new String[] {"m01891@aaf.att.com","bogus"}) {
138                         Getter<CredDAO.Data> g = fg.getter(id);
139                         Result<List<CredDAO.Data>> r = g.get();
140                         if(r.isOKhasData()) {
141                                 for(CredDAO.Data cdd : r.value) {
142                                         System.out.println(cdd);
143                                 }
144                         }
145                 }
146         }
147 }
148