From 4a51a8f96715ffb2a42189b93b9fa91b453b8530 Mon Sep 17 00:00:00 2001 From: sg481n Date: Thu, 3 Aug 2017 17:39:12 -0400 Subject:  [AAF-21] Initial code import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ia1dd196befd061f6ba0c2be6bf4456a30ea50f97 Signed-off-by: sg481n --- .../java/com/att/cadi/locator/DME2Locator.java | 346 +++++++++++++++++++++ 1 file changed, 346 insertions(+) create mode 100644 client/src/main/java/com/att/cadi/locator/DME2Locator.java (limited to 'client/src/main/java/com/att/cadi/locator/DME2Locator.java') diff --git a/client/src/main/java/com/att/cadi/locator/DME2Locator.java b/client/src/main/java/com/att/cadi/locator/DME2Locator.java new file mode 100644 index 0000000..72a5f73 --- /dev/null +++ b/client/src/main/java/com/att/cadi/locator/DME2Locator.java @@ -0,0 +1,346 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aai + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * Copyright © 2017 Amdocs + * * =========================================================================== + * * 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 com.att.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 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; +import com.att.cadi.Access; +import com.att.cadi.Access.Level; +import com.att.cadi.Locator; +import com.att.cadi.LocatorException; +import com.att.cadi.PropAccess; + +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