Collection syntax change because of Sonar
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / Loader.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;
23
24 import java.io.DataInputStream;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Set;
35
36 import com.datastax.driver.core.Row;
37
38 public abstract class Loader<DATA> {
39         private int keylimit;
40         public Loader(int keylimit) {
41                 this.keylimit = keylimit;
42         }
43         
44         public int keylimit() {
45                 return keylimit;
46         }
47         
48         protected abstract DATA load(DATA data, Row row);
49         protected abstract void key(DATA data, int idx, Object[] obj);
50         protected abstract void body(DATA data, int idx, Object[] obj);
51
52         public final Object[] extract(DATA data, int size, CassDAOImpl.CRUD type) {
53                 Object[] rv=null;
54                 switch(type) {
55                         case delete:
56                                 rv = new Object[keylimit()];
57                                 key(data,0,rv);
58                                 break;
59                         case update:
60                                 rv = new Object[size];
61                                 body(data,0,rv);
62                                 int body = size-keylimit();
63                                 if(body>0) {
64                                     key(data,body,rv);
65                                 }
66                                 break;
67                         default:
68                                 rv = new Object[size];
69                                 key(data,0,rv);
70                                 if(size>keylimit()) {
71                                     body(data,keylimit(),rv);
72                                 }
73                                 break;
74                 }
75                 return rv;
76         }
77         
78         public static void writeString(DataOutputStream os, String s) throws IOException {
79                 if(s==null) {
80                         os.writeInt(-1);
81                 } else {
82                         switch(s.length()) {
83                                 case 0:
84                                         os.writeInt(0);
85                                         break;
86                                 default:
87                                         byte[] bytes = s.getBytes();
88                                         os.writeInt(bytes.length);
89                                         os.write(bytes);
90                         }
91                 }
92         }
93         
94         
95         /**
96          * We use bytes here to set a Maximum
97          * 
98          * @param is
99          * @param MAX
100          * @return
101          * @throws IOException
102          */
103         public static String readString(DataInputStream is, byte[] _buff) throws IOException {
104                 int l = is.readInt();
105                 byte[] buff = _buff;
106                 switch(l) {
107                         case -1: return null;
108                         case  0: return "";
109                         default:
110                                 // Cover case where there is a large string, without always allocating a large buffer.
111                                 if(l>buff.length) {
112                                     buff = new byte[l];
113                                 }
114                                 is.read(buff,0,l);
115                                 return new String(buff,0,l);
116                 }
117         }
118
119         /**
120          * Write a set with proper sizing
121          * 
122          * Note: at the moment, this is just String.  Probably can develop system where types
123          * are supported too... but not now.
124          * 
125          * @param os
126          * @param set
127          * @throws IOException
128          */
129         public static void writeStringSet(DataOutputStream os, Collection<String> set) throws IOException {
130                 if(set==null) {
131                         os.writeInt(-1);
132                 } else {
133                         os.writeInt(set.size());
134                         for(String s : set) {
135                                 writeString(os, s);
136                         }
137                 }
138
139         }
140         
141         public static Set<String> readStringSet(DataInputStream is, byte[] buff) throws IOException {
142                 int l = is.readInt();
143                 if(l<0) {
144                     return null;
145                 }
146                 Set<String> set = new HashSet<>(l);
147                 for(int i=0;i<l;++i) {
148                         set.add(readString(is,buff));
149                 }
150                 return set;
151         }
152         
153         public static List<String> readStringList(DataInputStream is, byte[] buff) throws IOException {
154                 int l = is.readInt();
155                 if(l<0) {
156                     return null;
157                 }
158                 List<String> list = new ArrayList<>(l);
159                 for(int i=0;i<l;++i) {
160                         list.add(Loader.readString(is,buff));
161                 }
162                 return list;
163         }
164
165         /** 
166          * Write a map
167          * @param os
168          * @param map
169          * @throws IOException
170          */
171         public static void writeStringMap(DataOutputStream os, Map<String,String> map) throws IOException {
172                 if(map==null) {
173                         os.writeInt(-1);
174                 } else {
175                         Set<Entry<String, String>> es = map.entrySet();
176                         os.writeInt(es.size());
177                         for(Entry<String,String> e : es) {
178                                 writeString(os, e.getKey());
179                                 writeString(os, e.getValue());
180                         }
181                 }
182
183         }
184
185         public static Map<String,String> readStringMap(DataInputStream is, byte[] buff) throws IOException {
186                 int l = is.readInt();
187                 if(l<0) {
188                     return null;
189                 }
190                 Map<String,String> map = new HashMap<>(l);
191                 for(int i=0;i<l;++i) {
192                         String key = readString(is,buff);
193                         map.put(key,readString(is,buff));
194                 }
195                 return map;
196         }
197         public static void writeHeader(DataOutputStream os, int magic, int version) throws IOException {
198                 os.writeInt(magic);
199                 os.writeInt(version);
200         }
201         
202         public static int readHeader(DataInputStream is, final int magic, final int version) throws IOException {
203                 if(is.readInt()!=magic) {
204                     throw new IOException("Corrupted Data Stream");
205                 }
206                 int v = is.readInt();
207                 if(version<0 || v>version) {
208                     throw new IOException("Unsupported Data Version: " + v);
209                 }
210                 return v;
211         }
212
213 }
214