Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cass / LocateDAO.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.cass;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.DataInputStream;
26 import java.io.DataOutputStream;
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
32 import java.util.UUID;
33
34 import org.onap.aaf.auth.dao.AbsCassDAO;
35 import org.onap.aaf.auth.dao.Bytification;
36 import org.onap.aaf.auth.dao.CassDAOImpl;
37 import org.onap.aaf.auth.dao.Loader;
38 import org.onap.aaf.auth.dao.Streamer;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.auth.layer.Result;
41 import org.onap.aaf.misc.env.APIException;
42
43 import com.datastax.driver.core.Cluster;
44 import com.datastax.driver.core.Row;
45
46 /**
47  * LocateDAO manages credentials. 
48  * @author Jonathan
49  * Date: 10/11/17
50  */
51 public class LocateDAO extends CassDAOImpl<AuthzTrans,LocateDAO.Data> {
52     public static final String TABLE = "locate";
53     private AbsCassDAO<AuthzTrans, Data>.PSInfo psName;
54     
55     public LocateDAO(AuthzTrans trans, Cluster cluster, String keyspace) throws APIException, IOException {
56         super(trans, LocateDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
57         init(trans);
58     }
59
60     public LocateDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> adao) throws APIException, IOException {
61         super(trans, LocateDAO.class.getSimpleName(), adao, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
62         init(trans);
63     }
64     
65     public static final int KEYLIMIT = 3;
66     public static class Data implements Bytification {
67         
68         public String                    name;
69         public String                    hostname;
70         public int                        port;
71         public int                        major;
72         public int                        minor;
73         public int                        patch;
74         public int                        pkg;
75         public float                        latitude;
76         public float                        longitude;
77         public String                    protocol;
78         private Set<String>                subprotocol;
79         public UUID                        port_key; // Note: Keep Port_key LAST at all times, because we shorten the UPDATE to leave Port_key Alone during reregistration.
80
81       // Getters
82         public Set<String> subprotocol(boolean mutable) {
83             if (subprotocol == null) {
84                 subprotocol = new HashSet<>();
85             } else if (mutable && !(subprotocol instanceof HashSet)) {
86                 subprotocol = new HashSet<>(subprotocol);
87             }
88             return subprotocol;
89         }
90         
91         @Override
92         public ByteBuffer bytify() throws IOException {
93             ByteArrayOutputStream baos = new ByteArrayOutputStream();
94             LocateLoader.deflt.marshal(this,new DataOutputStream(baos));
95             return ByteBuffer.wrap(baos.toByteArray());
96         }
97         
98         @Override
99         public void reconstitute(ByteBuffer bb) throws IOException {
100             LocateLoader.deflt.unmarshal(this, toDIS(bb));
101         }
102
103         public Data copy() {
104             Data out = new Data();
105             out.name = name;
106             out.hostname = hostname;
107             out.port = port;
108             out.major = major;
109             out.minor = minor;
110             out.patch = patch;
111             out.pkg = pkg;
112             out.latitude = latitude;
113             out.longitude = longitude;
114             out.protocol = protocol;
115             out.subprotocol = new HashSet<>();
116             out.subprotocol.addAll(subprotocol);
117             out.port_key = port_key;
118             return out;
119         }
120     }
121
122     private static class LocateLoader extends Loader<Data> implements Streamer<Data>{
123         public static final int MAGIC=85102934;
124             public static final int VERSION=1;
125             public static final int BUFF_SIZE=48; // Note: 
126     
127             public static final LocateLoader deflt = new LocateLoader(KEYLIMIT);
128             public LocateLoader(int keylimit) {
129             super(keylimit);
130         }
131
132         @Override
133         public Data load(Data data, Row row) {
134                 data.name = row.getString(0);
135                 data.hostname = row.getString(1);
136                 data.port = row.getInt(2);
137                 data.major = row.getInt(3);
138                 data.minor = row.getInt(4);
139                 data.patch = row.getInt(5);
140                 data.pkg = row.getInt(6);
141                 data.latitude = row.getFloat(7);
142                 data.longitude = row.getFloat(8);
143                 data.protocol = row.getString(9);
144                 data.subprotocol = row.getSet(10,String.class);
145                 data.port_key = row.getUUID(11);
146             return data;
147         }
148
149         @Override
150         protected void key(Data data, int idx, Object[] obj) {
151             obj[idx] = data.name;
152             obj[++idx] = data.hostname;
153             obj[++idx] = data.port;
154         }
155
156         @Override
157         protected void body(final Data data, final int _idx, final Object[] obj) {
158                 int idx = _idx;
159             obj[idx] = data.major;
160             obj[++idx] = data.minor;
161             obj[++idx] = data.patch;
162             obj[++idx] = data.pkg;
163             obj[++idx] = data.latitude;
164             obj[++idx] = data.longitude;
165             obj[++idx] = data.protocol;
166             obj[++idx] = data.subprotocol;
167             obj[++idx] = data.port_key;
168         }
169
170         @Override
171         public void marshal(Data data, DataOutputStream os) throws IOException {
172             writeHeader(os,MAGIC,VERSION);
173             writeString(os, data.name);
174             writeString(os, data.hostname);
175             os.writeInt(data.port);
176             os.writeInt(data.major);
177             os.writeInt(data.minor);
178             os.writeInt(data.patch);
179             os.writeInt(data.pkg);
180             os.writeFloat(data.latitude);
181             os.writeFloat(data.longitude);
182             writeString(os, data.protocol);
183             if (data.subprotocol==null) {
184                 os.writeInt(0);
185             } else {
186                 os.writeInt(data.subprotocol.size());
187                 for (String s: data.subprotocol) {
188                     writeString(os,s);
189                 }
190             }
191             
192             writeString(os,data.port_key==null?"":data.port_key.toString());
193         }
194
195         @Override
196         public void unmarshal(Data data, DataInputStream is) throws IOException {
197             /*int version = */readHeader(is,MAGIC,VERSION);
198             // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
199             byte[] buff = new byte[BUFF_SIZE];
200             data.name = readString(is,buff);
201             data.hostname = readString(is,buff);
202             data.port = is.readInt();
203             data.major = is.readInt();
204             data.minor = is.readInt();
205             data.patch = is.readInt();
206             data.pkg = is.readInt();
207             data.latitude = is.readFloat();
208             data.longitude = is.readFloat();
209             data.protocol = readString(is,buff);
210             
211             int size = is.readInt();
212             data.subprotocol = new HashSet<>(size);
213             for (int i=0;i<size;++i) {
214                 data.subprotocol.add(readString(is,buff));
215             }
216             String port_key = readString(is,buff);
217             if (port_key.length()>0) {
218                 data.port_key=UUID.fromString(port_key);
219             } else {
220                 data.port_key = null;
221             }
222         }
223     }
224     
225     public Result<List<LocateDAO.Data>> readByName(AuthzTrans trans, String service) {
226             return psName.read(trans, "Read By Name", new Object[] {service});
227     }
228
229     private void init(AuthzTrans trans) throws APIException, IOException {
230         // Set up sub-DAOs
231         String[] helpers = setCRUD(trans, TABLE, Data.class, LocateLoader.deflt);
232 //        int lastComma = helpers[ASSIGNMENT_COMMAS].lastIndexOf(',');
233 //        replace(CRUD.update,new PSInfo(trans,"UPDATE LOCATE SET " + helpers[ASSIGNMENT_COMMAS].substring(0, lastComma) +
234 //                " WHERE name=? AND hostname=? AND port=?;", new LocateLoader(3),writeConsistency));
235         psName = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
236                 " WHERE name = ?", new LocateLoader(1),readConsistency);
237     }
238     
239     /**
240      * Log Modification statements to History
241      *
242      * @param modified        which CRUD action was done
243      * @param data            entity data that needs a log entry
244      * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
245      */
246     @Override
247     protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
248     }
249 }