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;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.onap.aaf.auth.env.AuthzEnv;
29 import org.onap.aaf.cadi.config.Config;
30 import org.onap.aaf.cadi.routing.GreatCircle;
31 import org.onap.aaf.misc.env.APIException;
32 import org.onap.aaf.misc.env.Env;
33 import org.onap.aaf.misc.env.util.Split;
35 import com.datastax.driver.core.Cluster;
36 import com.datastax.driver.core.Cluster.Builder;
37 import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
38 import com.datastax.driver.core.policies.TokenAwarePolicy;
40 public class CassAccess {
41 public static final String KEYSPACE = "authz";
42 public static final String CASSANDRA_CLUSTERS = "cassandra.clusters";
43 public static final String CASSANDRA_CLUSTERS_PORT = "cassandra.clusters.port";
44 public static final String CASSANDRA_CLUSTERS_USER_NAME = "cassandra.clusters.user";
45 public static final String CASSANDRA_CLUSTERS_PASSWORD = "cassandra.clusters.password";
46 public static final String CASSANDRA_RESET_EXCEPTIONS = "cassandra.reset.exceptions";
47 private static final List<Resettable> resetExceptions = new ArrayList<>();
48 public static final String ERR_ACCESS_MSG = "Accessing Backend";
49 private static Builder cb = null;
52 * To create DCAwareRoundRobing Policy:
54 * LATITUDE (or AFT_LATITUDE)
55 * LONGITUDE (or AFT_LONGITUDE)
56 * CASSANDRA CLUSTERS with additional information:
57 * machine:DC:lat:long,machine:DC:lat:long
61 * @throws APIException
65 // @SuppressWarnings("deprecation")
66 public static synchronized Cluster cluster(Env env, String prefix) throws APIException, IOException {
72 env.info().log("Cassandra Connection for ",prefix);
75 cb = Cluster.builder();
76 String str = env.getProperty(pre+CASSANDRA_CLUSTERS_PORT,env.getProperty(CASSANDRA_CLUSTERS_PORT,"9042"));
78 env.init().log("Cass Port = ",str );
79 cb.withPort(Integer.parseInt(str));
81 str = env.getProperty(pre+CASSANDRA_CLUSTERS_USER_NAME,env.getProperty(CASSANDRA_CLUSTERS_USER_NAME,null));
83 env.init().log("Cass User = ",str );
84 String epass = env.getProperty(pre + CASSANDRA_CLUSTERS_PASSWORD,env.getProperty(CASSANDRA_CLUSTERS_PASSWORD,null));
86 throw new APIException("No Password configured for " + str);
88 //TODO Figure out way to ensure Decryptor setting in AuthzEnv
89 if (env instanceof AuthzEnv) {
90 cb.withCredentials(str,((AuthzEnv)env).decrypt(epass,true));
92 cb.withCredentials(str, env.decryptor().decrypt(epass));
96 str = env.getProperty(pre+CASSANDRA_RESET_EXCEPTIONS,env.getProperty(CASSANDRA_RESET_EXCEPTIONS,null));
98 env.init().log("Cass ResetExceptions = ",str );
99 for (String ex : Split.split(',', str)) {
100 resetExceptions.add(new Resettable(env,ex));
104 str = env.getProperty(Config.CADI_LATITUDE);
105 Double lat = str!=null && !str.isEmpty()?Double.parseDouble(str):null;
106 str = env.getProperty(Config.CADI_LONGITUDE);
107 Double lon = str!=null && !str.isEmpty()?Double.parseDouble(str):null;
108 if (lat == null || lon == null) {
109 throw new APIException(Config.CADI_LATITUDE + " and/or " + Config.CADI_LONGITUDE + " are not set");
112 env.init().printf("Service Latitude,Longitude = %f,%f",lat,lon);
114 str = env.getProperty(pre+CASSANDRA_CLUSTERS,env.getProperty(CASSANDRA_CLUSTERS,"localhost"));
115 env.init().printf("Cass Clusters = '%s'\n",str );
116 String[] machs = Split.split(',', str);
117 String[] cpoints = new String[machs.length];
118 String bestDC = null;
120 double mlat, mlon,temp,distance = Double.MAX_VALUE;
121 for (int i=0;i<machs.length;++i) {
122 String[] minfo = Split.split(':',machs[i]);
123 if (minfo.length>0) {
127 if (minfo.length>3) {
128 if (minfo[1].equals(bestDC)) {
131 // Calc closest DC with Great Circle
132 mlat = Double.parseDouble(minfo[2]);
133 mlon = Double.parseDouble(minfo[3]);
134 // Note: GreatCircle Distance is always >= 0.0 (not negative)
135 if ((temp=GreatCircle.calc(lat, lon, mlat, mlon)) < distance) {
137 if (bestDC==null || !bestDC.equals(minfo[1])) {
146 cb.addContactPoints(cpoints);
149 // 8/26/2016 Management has determined that Accuracy is preferred over speed in bad situations
150 // Local DC Aware Load Balancing appears to have the highest normal performance, with the best
152 DCAwareRoundRobinPolicy dcrrPolicy = DCAwareRoundRobinPolicy.builder()
154 .withUsedHostsPerRemoteDc(numInBestDC)
156 // cb.withLoadBalancingPolicy(new DCAwareRoundRobinPolicy(
157 // bestDC, numInBestDC, true /*allow LocalDC to look at other DCs for LOCAL_QUORUM */));
158 cb.withLoadBalancingPolicy(new TokenAwarePolicy(dcrrPolicy));
159 env.init().printf("Cassandra configured for DCAwareRoundRobinPolicy with best DC at %s with emergency remote of up to %d node(s)"
160 ,bestDC, numInBestDC);
162 env.init().printf("Cassandra is using Default Policy, which is not DC aware");
168 private static class Resettable {
169 private Class<? extends Exception> cls;
170 private List<String> messages;
172 @SuppressWarnings("unchecked")
173 public Resettable(Env env, String propData) throws APIException {
174 if (propData!=null && propData.length()>1) {
175 String[] split = Split.split(':', propData);
176 if (split.length>0) {
178 cls = (Class<? extends Exception>)Class.forName(split[0]);
179 } catch (ClassNotFoundException e) {
180 throw new APIException("Declared Cassandra Reset Exception, " + propData + ", cannot be ClassLoaded");
183 if (split.length>1) {
184 messages=new ArrayList<>();
185 for (int i=1;i<split.length;++i) {
186 String str = split[i];
187 int start = str.startsWith("\"")?1:0;
188 int end = str.length()-(str.endsWith("\"")?1:0);
189 messages.add(split[i].substring(start, end));
197 public boolean matches(Exception ex) {
198 if (ex.getClass().equals(cls)) {
199 if (messages!=null) {
200 String msg = ex.getMessage();
201 for (String m : messages) {
202 if (msg.contains(m)) {
212 public static final boolean isResetException(Exception e) {
216 for (Resettable re : resetExceptions) {