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.cadi.locator;
24 import java.io.IOException;
25 import java.net.InetAddress;
27 import java.net.URISyntaxException;
29 import org.onap.aaf.cadi.Access;
30 import org.onap.aaf.cadi.Locator;
31 import org.onap.aaf.cadi.LocatorException;
32 import org.onap.aaf.cadi.Access.Level;
34 public class DNSLocator implements Locator<URI> {
35 private static enum Status {UNTRIED, OK, INVALID, SLOW};
36 private static final int CHECK_TIME = 3000;
38 private String host, protocol;
39 private Access access;
41 private int startPort, endPort;
42 private String suffix;
44 public DNSLocator(Access access, String protocol, String host, String range) {
46 this.protocol = protocol;
48 int dash = range.indexOf('-');
50 startPort = endPort = Integer.parseInt(range);
52 startPort = Integer.parseInt(range.substring(0,dash));
53 endPort = Integer.parseInt(range.substring(dash + 1));
58 public DNSLocator(Access access, String aaf_locate) throws LocatorException {
60 if(aaf_locate==null) {
61 throw new LocatorException("Null passed into DNSLocator constructor");
64 if(aaf_locate.startsWith("https:")) {
66 start = 9; // https://
68 } else if(aaf_locate.startsWith("http:")) {
73 throw new LocatorException("DNSLocator accepts only https or http protocols. (requested URL " + aaf_locate + ')');
76 int colon = aaf_locate.indexOf(':',start);
80 int left = aaf_locate.indexOf('[',start);
82 int right = aaf_locate.indexOf(']',left+1);
84 int dash = aaf_locate.indexOf('-',left+1);
86 startPort = endPort = Integer.parseInt(aaf_locate.substring(left+1,right));
88 startPort = Integer.parseInt(aaf_locate.substring(left+1,dash));
89 endPort = Integer.parseInt(aaf_locate.substring(dash + 1,right));
94 slash = aaf_locate.indexOf('/',colon+1);
96 startPort = endPort = Integer.parseInt(aaf_locate.substring(start));
98 startPort = endPort = Integer.parseInt(aaf_locate.substring(start,slash));
102 startPort = endPort = port;
107 public URI get(Item item) throws LocatorException {
108 return hosts[((DLItem)item).cnt].uri;
112 public boolean hasItems() {
113 for(Host h : hosts) {
114 if(h.status==Status.OK) {
122 public void invalidate(Item item) {
123 DLItem di = (DLItem)item;
124 hosts[di.cnt].status = Status.INVALID;
128 public Item best() throws LocatorException {
130 for(int i=0;i<hosts.length;++i) {
131 switch(hosts[i].status) {
133 return new DLItem(i);
140 if(hosts[i].ia.isReachable(CHECK_TIME)) {
141 hosts[i].status = Status.OK;
142 return new DLItem(i);
144 } catch (IOException e) {
145 throw new LocatorException(e);
152 throw new LocatorException("No Available URIs for " + host);
156 public Item first() throws LocatorException {
157 return new DLItem(0);
161 public Item next(Item item) throws LocatorException {
162 DLItem di = (DLItem)item;
163 if(++di.cnt<hosts.length) {
171 public boolean refresh() {
173 InetAddress[] ias = InetAddress.getAllByName(host);
174 Host[] temp = new Host[ias.length * (1 + endPort - startPort)];
176 for(int j=startPort; j<=endPort; ++j) {
177 for(int i=0;i<ias.length;++i) {
178 temp[++cnt] = new Host(ias[i], j, suffix);
183 } catch (Exception e) {
184 access.log(Level.ERROR, e);
191 private InetAddress ia;
192 private Status status;
194 public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {
196 uri = new URI(protocol,null,inetAddress.getHostAddress(),port,suffix,null,null);
197 status = Status.UNTRIED;
201 private class DLItem implements Item {
202 public DLItem(int i) {
209 public void destroy() {}