2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.dao.cass;
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;
32 import java.util.UUID;
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;
43 import com.datastax.driver.core.Cluster;
44 import com.datastax.driver.core.Row;
47 * LocateDAO manages credentials.
51 public class LocateDAO extends CassDAOImpl<AuthzTrans,LocateDAO.Data> {
52 public static final String TABLE = "locate";
53 private AbsCassDAO<AuthzTrans, Data>.PSInfo psName;
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));
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));
65 public static final int KEYLIMIT = 3;
66 public static class Data implements Bytification {
69 public String hostname;
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.
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);
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());
99 public void reconstitute(ByteBuffer bb) throws IOException {
100 LocateLoader.deflt.unmarshal(this, toDIS(bb));
104 Data out = new Data();
106 out.hostname = hostname;
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;
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:
127 public static final LocateLoader deflt = new LocateLoader(KEYLIMIT);
128 public LocateLoader(int keylimit) {
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);
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;
157 protected void body(final Data data, final int _idx, final Object[] obj) {
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;
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) {
186 os.writeInt(data.subprotocol.size());
187 for (String s: data.subprotocol) {
192 writeString(os,data.port_key==null?"":data.port_key.toString());
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);
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));
216 String port_key = readString(is,buff);
217 if (port_key.length()>0) {
218 data.port_key=UUID.fromString(port_key);
220 data.port_key = null;
225 public Result<List<LocateDAO.Data>> readByName(AuthzTrans trans, String service) {
226 return psName.read(trans, "Read By Name", new Object[] {service});
229 private void init(AuthzTrans trans) throws APIException, IOException {
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);
240 * Log Modification statements to History
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
247 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {