summaryrefslogtreecommitdiffstats
path: root/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/cached/FileGetter.java
blob: a8c86fa503e4a5ab9d46ac3b5a93d0d11f7e8fd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.aaf.auth.dao.cached;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.onap.aaf.auth.dao.Cached.Getter;
import org.onap.aaf.auth.dao.cass.CredDAO;
import org.onap.aaf.auth.dao.cass.CredDAO.Data;
import org.onap.aaf.auth.layer.Result;
import org.onap.aaf.cadi.Access;
import org.onap.aaf.cadi.Access.Level;
import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.Hash;
import org.onap.aaf.cadi.PropAccess;
import org.onap.aaf.cadi.util.CSV;

public class FileGetter {
	private static final String AAF_FILEGETTER = "aaf_filegetter";
	public static boolean isLoaded = false;
	private static FileGetter singleton;

	private Map<String,List<CredDAO.Data>> data; 
	private SimpleDateFormat sdf;
	private FileGetter(Access access) {
		if(access!=null) {
			String filename = access.getProperty(AAF_FILEGETTER,null);
			if((filename!=null)&&(!isLoaded)) { 
					data = new TreeMap<>();
					sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+SSSS");
					CSV csv = new CSV(access, filename).setDelimiter('|');
					try {
						access.log(Level.INIT, "Loading Filebased Cred from",filename);
			    		csv.visit(row -> {
			    			if(row.size()<1) {
			    				access.log(Level.INIT, "Bad Row");
			    			}
			    			int type;
			    			  try {
			    				  type =Integer.parseInt(row.get(1));
			    			  } catch(Exception e) {
			    				  access.log(Level.INIT, e, "skipping ", row.get(0));
			    				  return;
			    			  }
			    			if(CredDAO.CERT_SHA256_RSA == type) {
			    				return;
			    			}
			    			CredDAO.Data cdd = new CredDAO.Data();
			    			cdd.id=row.get(0);
			    			cdd.type = type;
			    			try {
								cdd.expires = sdf.parse(row.get(2));
								cdd.cred = ByteBuffer.wrap(Hash.fromHex(row.get(3)));
								cdd.notes= row.get(4);
								cdd.ns = row.get(5);
								cdd.other = Integer.parseInt(row.get(6));
								if(row.size()>8) {
									cdd.tag = row.get(8);
								} else {
									cdd.tag = "";
								}
								List<CredDAO.Data> lcdd = data.get(cdd.id);
								if(lcdd == null) {
									lcdd = new ArrayList<>();
									data.put(cdd.id, lcdd);
								}
								lcdd.add(cdd);
								
							} catch (ParseException e) {
								access.log(Level.INIT, e);
							}
			    			
			    		});
			    		access.printf(Level.INIT, "Filebased Cred finished...");
						isLoaded = true;
					} catch( CadiException | IOException e) {
						access.log(Level.ERROR, e);
					}
				
			}
		}
	}

	public static synchronized FileGetter singleton(Access access) {
		if(singleton==null) {
			singleton = new FileGetter(access);
		}
		return singleton;
		
	}
	public Getter<CredDAO.Data> getter(String id) {
		return new FGetter(id);
	}
	private static List<CredDAO.Data> EMPTY = new ArrayList<>(); 
	public class FGetter implements Getter<CredDAO.Data> {
		private final List<CredDAO.Data> lcdd; 	
		public FGetter(final String id) {
			lcdd = data.get(id);
		}
		@Override
		public Result<List<Data>> get() {
			return Result.ok(lcdd==null?EMPTY:lcdd);
		}
	}
	
	public static void main(String[] args) {
		PropAccess access = new PropAccess(args);
		access.setProperty(AAF_FILEGETTER,"/Users/jg1555/cred.dat");
		FileGetter fg = FileGetter.singleton(access);
		
		for(String id : new String[] {"m01891@aaf.att.com","bogus"}) {
			Getter<CredDAO.Data> g = fg.getter(id);
			Result<List<CredDAO.Data>> r = g.get();
			if(r.isOKhasData()) {
				for(CredDAO.Data cdd : r.value) {
					System.out.println(cdd);
				}
			}
		}
	}
}