757efa5fc636b879dcc33dd6b349ece41f570ccf
[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
104     private static class LocateLoader extends Loader<Data> implements Streamer<Data>{
105         public static final int MAGIC=85102934;
106             public static final int VERSION=1;
107             public static final int BUFF_SIZE=48; // Note: 
108     
109             public static final LocateLoader deflt = new LocateLoader(KEYLIMIT);
110             public LocateLoader(int keylimit) {
111             super(keylimit);
112         }
113
114         @Override
115         public Data load(Data data, Row row) {
116                 data.name = row.getString(0);
117                 data.hostname = row.getString(1);
118                 data.port = row.getInt(2);
119                 data.major = row.getInt(3);
120                 data.minor = row.getInt(4);
121                 data.patch = row.getInt(5);
122                 data.pkg = row.getInt(6);
123                 data.latitude = row.getFloat(7);
124                 data.longitude = row.getFloat(8);
125                 data.protocol = row.getString(9);
126                 data.subprotocol = row.getSet(10,String.class);
127                 data.port_key = row.getUUID(11);
128             return data;
129         }
130
131         @Override
132         protected void key(Data data, int idx, Object[] obj) {
133             obj[idx] = data.name;
134             obj[++idx] = data.hostname;
135             obj[++idx] = data.port;
136         }
137
138         @Override
139         protected void body(final Data data, final int _idx, final Object[] obj) {
140                 int idx = _idx;
141             obj[idx] = data.major;
142             obj[++idx] = data.minor;
143             obj[++idx] = data.patch;
144             obj[++idx] = data.pkg;
145             obj[++idx] = data.latitude;
146             obj[++idx] = data.longitude;
147             obj[++idx] = data.protocol;
148             obj[++idx] = data.subprotocol;
149             obj[++idx] = data.port_key;
150         }
151
152         @Override
153         public void marshal(Data data, DataOutputStream os) throws IOException {
154             writeHeader(os,MAGIC,VERSION);
155             writeString(os, data.name);
156             writeString(os, data.hostname);
157             os.writeInt(data.port);
158             os.writeInt(data.major);
159             os.writeInt(data.minor);
160             os.writeInt(data.patch);
161             os.writeInt(data.pkg);
162             os.writeFloat(data.latitude);
163             os.writeFloat(data.longitude);
164             writeString(os, data.protocol);
165             if (data.subprotocol==null) {
166                 os.writeInt(0);
167             } else {
168                 os.writeInt(data.subprotocol.size());
169                 for (String s: data.subprotocol) {
170                     writeString(os,s);
171                 }
172             }
173             
174             writeString(os,data.port_key==null?"":data.port_key.toString());
175         }
176
177         @Override
178         public void unmarshal(Data data, DataInputStream is) throws IOException {
179             /*int version = */readHeader(is,MAGIC,VERSION);
180             // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
181             byte[] buff = new byte[BUFF_SIZE];
182             data.name = readString(is,buff);
183             data.hostname = readString(is,buff);
184             data.port = is.readInt();
185             data.major = is.readInt();
186             data.minor = is.readInt();
187             data.patch = is.readInt();
188             data.pkg = is.readInt();
189             data.latitude = is.readFloat();
190             data.longitude = is.readFloat();
191             data.protocol = readString(is,buff);
192             
193             int size = is.readInt();
194             data.subprotocol = new HashSet<>(size);
195             for (int i=0;i<size;++i) {
196                 data.subprotocol.add(readString(is,buff));
197             }
198             String port_key = readString(is,buff);
199             if (port_key.length()>0) {
200                 data.port_key=UUID.fromString(port_key);
201             } else {
202                 data.port_key = null;
203             }
204         }
205     }
206     
207     public Result<List<LocateDAO.Data>> readByName(AuthzTrans trans, String service) {
208             return psName.read(trans, "Read By Name", new Object[] {service});
209     }
210
211     private void init(AuthzTrans trans) throws APIException, IOException {
212         // Set up sub-DAOs
213         String[] helpers = setCRUD(trans, TABLE, Data.class, LocateLoader.deflt);
214 //        int lastComma = helpers[ASSIGNMENT_COMMAS].lastIndexOf(',');
215 //        replace(CRUD.update,new PSInfo(trans,"UPDATE LOCATE SET " + helpers[ASSIGNMENT_COMMAS].substring(0, lastComma) +
216 //                " WHERE name=? AND hostname=? AND port=?;", new LocateLoader(3),writeConsistency));
217         psName = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
218                 " WHERE name = ?", new LocateLoader(1),readConsistency);
219     }
220     
221     /**
222      * Log Modification statements to History
223      *
224      * @param modified        which CRUD action was done
225      * @param data            entity data that needs a log entry
226      * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
227      */
228     @Override
229     protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
230     }
231 }