From 62c4eb45e157d502463d797c1353802ca8e1e307 Mon Sep 17 00:00:00 2001 From: sg481n Date: Fri, 25 Aug 2017 01:57:24 -0400 Subject: Update project structure for aaf/cadi Update project structure from com.att to org.onap and add distribution management and staging plugin. Issue-id: AAF-22 Change-Id: Idf2b591139e38921ad28782a51486714a05dee92 Signed-off-by: sg481n --- .../org/onap/aaf/cadi/locator/DME2Locator.java | 347 +++++++++++++++++++++ .../java/org/onap/aaf/cadi/locator/DNSLocator.java | 163 ++++++++++ .../aaf/cadi/locator/HClientHotPeerLocator.java | 61 ++++ .../org/onap/aaf/cadi/locator/HotPeerLocator.java | 304 ++++++++++++++++++ .../org/onap/aaf/cadi/locator/PropertyLocator.java | 282 +++++++++++++++++ 5 files changed, 1157 insertions(+) create mode 100644 client/src/main/java/org/onap/aaf/cadi/locator/DME2Locator.java create mode 100644 client/src/main/java/org/onap/aaf/cadi/locator/DNSLocator.java create mode 100644 client/src/main/java/org/onap/aaf/cadi/locator/HClientHotPeerLocator.java create mode 100644 client/src/main/java/org/onap/aaf/cadi/locator/HotPeerLocator.java create mode 100644 client/src/main/java/org/onap/aaf/cadi/locator/PropertyLocator.java (limited to 'client/src/main/java/org/onap/aaf/cadi/locator') diff --git a/client/src/main/java/org/onap/aaf/cadi/locator/DME2Locator.java b/client/src/main/java/org/onap/aaf/cadi/locator/DME2Locator.java new file mode 100644 index 0000000..656fd19 --- /dev/null +++ b/client/src/main/java/org/onap/aaf/cadi/locator/DME2Locator.java @@ -0,0 +1,347 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package org.onap.aaf.cadi.locator; + + +import java.net.InetAddress; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Properties; +import java.util.Random; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.Locator; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.Access.Level; + +import java.security.SecureRandom; + +//import com.att.aft.dme2.api.DME2Endpoint; +import com.att.aft.dme2.api.DME2Exception; +import com.att.aft.dme2.api.DME2Manager; +import com.att.aft.dme2.api.DME2Server; +import com.att.aft.dme2.manager.registry.DME2Endpoint; + +public class DME2Locator implements Locator { + private DME2Manager dm; + private DME2Endpoint[] endpoints; + private Access access; + private String service; + private String version; + private String routeOffer; + private String envContext; + private String thisMachine; + private String pathInfo; + private int thisPort; + private boolean removeSelf; + private final static SecureRandom random = new SecureRandom(); + + // Default is to not bother trying to remove self + public DME2Locator(Access access, DME2Manager dm, String service, String version, String envContext, String routeOffer) throws DME2Exception, UnknownHostException, LocatorException { + this(access,dm,service,version,envContext,routeOffer,false); + } + + public DME2Locator(Access access, DME2Manager dm, String service, String version, String envContext, String routeOffer, boolean removeSelf) throws DME2Exception, UnknownHostException, LocatorException { + this.access = access; + if(dm==null) { + this.dm = new DME2Manager("DME2Locator created DME2Manager",System.getProperties()); + } else { + this.dm = dm; + } + this.service = service; + this.version = version; + this.envContext = envContext; + this.routeOffer = routeOffer; + refresh(); + if(thisMachine==null) { + // Can't get from dm... + thisMachine = InetAddress.getLocalHost().getHostName(); + thisPort = 0; + } else { + thisPort = dm.getPort(); + } + + this.removeSelf = removeSelf; + } + + // Default is to not bother trying to remove self + public DME2Locator(Access access, DME2Manager dm, String aafurl) throws DME2Exception, UnknownHostException, LocatorException { + this(access,dm,aafurl,false); + } + + public DME2Locator(Access access, DME2Manager dm, String aafurl, boolean removeSelf) throws DME2Exception, UnknownHostException, LocatorException { + if(aafurl==null) { + throw new LocatorException("URL is null"); + } + this.access = access; + if(dm==null) { + Properties dprops; + if(access instanceof PropAccess) { + dprops = ((PropAccess)access).getDME2Properties(); + } else { + dprops = System.getProperties(); + } + dm = this.dm = new DME2Manager("DME2Locator created DME2Manager",dprops); + } else { + this.dm = dm; + } + String[] split = aafurl.split("/"); + StringBuilder sb = new StringBuilder(); + boolean dme2Entered = false; + for(String s : split) { + if(s.startsWith("service=")) { + this.service = s.substring(8); + } else if(s.startsWith("version=")) { + this.version = s.substring(8); + } else if(s.startsWith("envContext=")) { + this.envContext = s.substring(11); + } else if(s.startsWith("routeOffer=")) { + this.routeOffer = s.substring(11); + dme2Entered = true; + } else if(dme2Entered) { + sb.append('/'); + sb.append(s); + } + } + pathInfo = sb.toString(); + thisMachine = dm.getHostname(); + if(thisMachine==null) { + // Can't get from dm... + thisMachine = InetAddress.getLocalHost().getHostName(); + thisPort = 0; + } else { + thisPort = dm.getPort(); + } + this.removeSelf=removeSelf; + refresh(); + } + + @Override + public boolean refresh() { + try { + dm.refresh(); + //endpoints = dm.findEndpoints(service, version, envContext, routeOffer, true); + if(removeSelf) { +// for(int i=0;i0; + return true; + } + + @Override + public void invalidate(Locator.Item item) throws LocatorException { + if(item instanceof DME2Item) { + int idx = ((DME2Item)item).idx; +// if(idx () { + @Override + public int compare(DoubIndex a, DoubIndex b) { + if(a.db.d) return 1; + return (random.nextInt()%1)==0?1:0;// randomize if the same + } + + }); + return new DME2Item(remote[0].idx); + } + } + } + + private static class DoubIndex { + public final double d; + public final int idx; + + public DoubIndex(double doub, int i) { + d = doub; + idx = i; + } + } + @Override + public DME2Item first() { +// if(endpoints==null)return null; +// for(int i=0;i { + private static enum Status {UNTRIED, OK, INVALID, SLOW}; + private static final int CHECK_TIME = 3000; + + private String host, protocol; + private Access access; + private Host[] hosts; + private int startPort, endPort; + private String suffix; + + public DNSLocator(Access access, String protocol, String host, String range) { + this.host = host; + this.protocol = protocol; + this.access = access; + int dash = range.indexOf('-'); + if(dash<0) { + startPort = endPort = Integer.parseInt(range); + } else { + startPort = Integer.parseInt(range.substring(0,dash)); + endPort = Integer.parseInt(range.substring(dash + 1)); + } + refresh(); + } + + @Override + public URI get(Item item) throws LocatorException { + return hosts[((DLItem)item).cnt].uri; + } + + @Override + public boolean hasItems() { + for(Host h : hosts) { + if(h.status==Status.OK) { + return true; + } + } + return false; + } + + @Override + public void invalidate(Item item) { + DLItem di = (DLItem)item; + hosts[di.cnt].status = Status.INVALID; + } + + @Override + public Item best() throws LocatorException { + // not a good "best" + for(int i=0;i { + private final HX509SS ss; + + public HClientHotPeerLocator(Access access, String urlstr, long invalidateTime, String localLatitude, + String localLongitude, HX509SS ss) throws LocatorException { + super(access, urlstr, invalidateTime, localLatitude, localLongitude); + + this.ss = ss; + } + + @Override + protected HClient _newClient(String clientInfo) throws LocatorException { + try { + int idx = clientInfo.indexOf('/'); + return new HClient(ss,new URI("https://"+(idx<0?clientInfo:clientInfo.substring(0, idx))),3000); + } catch (URISyntaxException e) { + throw new LocatorException(e); + } + } + + @Override + protected HClient _invalidate(HClient client) { + return null; + } + + @Override + protected void _destroy(HClient client) { + } +} diff --git a/client/src/main/java/org/onap/aaf/cadi/locator/HotPeerLocator.java b/client/src/main/java/org/onap/aaf/cadi/locator/HotPeerLocator.java new file mode 100644 index 0000000..04d2a28 --- /dev/null +++ b/client/src/main/java/org/onap/aaf/cadi/locator/HotPeerLocator.java @@ -0,0 +1,304 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package org.onap.aaf.cadi.locator; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.Locator; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.Access.Level; +import org.onap.aaf.cadi.routing.GreatCircle; + +import org.onap.aaf.inno.env.util.Split; + +/** + * This Locator is to handle Hot Peer load protection, when the Servers are + * 1) Static + * 2) Well known client URL + * + * The intention is to change traffic over to the Hot Peer, if a server goes down, and reinstate + * when it is back up. + * + * Example of this kind of Service is a MS Certificate Server + * + * + * + * @param + */ +public abstract class HotPeerLocator implements Locator { + private final String[] urlstrs; + private final CLIENT[] clients; + private final long[] failures; + private final double[] distances; + private int preferred; + private long invalidateTime; + private Thread refreshThread; + protected Access access; + + /** + * Construct: Expect one or more Strings in the form: + * 192.555.112.223:39/38.88087/-77.30122 + * separated by commas + * + * @param trans + * @param urlstr + * @param invalidateTime + * @param localLatitude + * @param localLongitude + * @throws LocatorException + */ + @SuppressWarnings("unchecked") + protected HotPeerLocator(Access access, final String urlstr, final long invalidateTime, final String localLatitude, final String localLongitude) throws LocatorException { + this.access = access; + urlstrs = Split.split(',', urlstr); + clients = (CLIENT[])new Object[urlstrs.length]; + failures = new long[urlstrs.length]; + distances= new double[urlstrs.length]; + this.invalidateTime = invalidateTime; + + double distance = Double.MAX_VALUE; + for(int i=0;iSystem.currentTimeMillis()) { + throw new LocatorException("Client requested is invalid"); + } else { + synchronized(clients) { + c = _newClient(urlstrs[hpi.idx]); + failures[hpi.idx]=0L; + } + } + } else if(failures[hpi.idx]>0){ + throw new LocatorException("Client requested is invalid"); + } + return c; + } + + public String info(Item item) { + HPItem hpi = (HPItem)item; + if(hpi!=null && hpi.idx=clients.length) { + return null; + } + return hpi; + } + + @Override + public boolean refresh() { + boolean force = !hasItems(); // If no Items at all, reset + boolean rv = true; + long now = System.currentTimeMillis(); + for(int i=0;i0L && (failures[i] { + private final URI [] orig; + private PLItem[] current; + private int end; + private final SecureRandom random; + private URI[] resolved; + private long lastRefreshed=0L; + private long minRefresh; + private long backgroundRefresh; + + public PropertyLocator(String locList) throws LocatorException { + this(locList,10000L, 1000*60*20); // defaults, do not refresh more than once in 10 seconds, Refresh Locator every 20 mins. + } + /** + * comma delimited root url list + * + * @param locList + * @throws LocatorException + */ + public PropertyLocator(String locList, long minRefreshMillis, long backgroundRefreshMillis) throws LocatorException { + minRefresh = minRefreshMillis; + backgroundRefresh = backgroundRefreshMillis; + if(locList==null) { + throw new LocatorException("No Location List given for PropertyLocator"); + } + String[] locarray = Split.split(',',locList); + List uriList = new ArrayList(); + + random = new SecureRandom(); + + for(int i=0;i0?current[0]:null; + } + + @Override + public boolean hasItems() { + return end>0; + } + + @Override + public Item next(Item item) throws LocatorException { + if(item==null) { + return null; + } else { + int spot; + if((spot=(((PLItem)item).order+1))>=end)return null; + return current[spot]; + } + } + + @Override + public synchronized void invalidate(Item item) throws LocatorException { + if(--end<=0) { + refresh(); + return; + } + PLItem pli = (PLItem)item; + int i,order; + for(i=0;ilastRefreshed) { + // Build up list + List resolve = new ArrayList(); + String realname; + for(int i = 0; i < orig.length ; ++i) { + try { + InetAddress ia[] = InetAddress.getAllByName(orig[i].getHost()); + + URI o,n; + for(int j=0;j