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.Access.Level;
31 import org.onap.aaf.cadi.LocatorException;
33 public class DNSLocator implements SizedLocator<URI> {
34 private static enum Status {UNTRIED, OK, INVALID, SLOW};
35 private static final int CHECK_TIME = 3000;
37 private String host, protocol;
38 private Access access;
40 private int startPort, endPort;
41 private String suffix;
43 private int size = 1; // initial, until refreshed.
45 public DNSLocator(Access access, String protocol, String host, String range) {
47 this.protocol = protocol;
49 int dash = range.indexOf('-');
51 startPort = endPort = Integer.parseInt(range);
53 startPort = Integer.parseInt(range.substring(0,dash));
54 endPort = Integer.parseInt(range.substring(dash + 1));
59 public DNSLocator(Access access, String aaf_locate) throws LocatorException {
61 if (aaf_locate==null) {
62 throw new LocatorException("Null passed into DNSLocator constructor");
65 if (aaf_locate.startsWith("https://")) {
67 start = 8; // https://
69 } else if (aaf_locate.startsWith("http://")) {
74 throw new LocatorException("DNSLocator accepts only https or http protocols. (requested URL " + aaf_locate + ')');
76 host = parseHostAndPorts(aaf_locate, start, defPort);
80 public static DNSLocator create(Access access, String url) throws LocatorException {
81 return new DNSLocator(access, url);
85 public URI get(Item item) throws LocatorException {
86 return hosts[((DLItem)item).cnt].uri;
90 public boolean hasItems() {
91 for (Host h : hosts) {
92 if (h.status==Status.OK) {
100 public void invalidate(Item item) {
101 DLItem di = (DLItem)item;
102 hosts[di.cnt].status = Status.INVALID;
106 public Item best() throws LocatorException {
108 for (int i=0;i<hosts.length;++i) {
109 switch(hosts[i].status) {
111 return new DLItem(i);
118 if (hosts[i].ia.isReachable(CHECK_TIME)) {
119 hosts[i].status = Status.OK;
120 return new DLItem(i);
122 } catch (IOException e) {
123 throw new LocatorException(e);
130 throw new LocatorException("No Available URIs for " + host);
134 public Item first() throws LocatorException {
135 return new DLItem(0);
139 public Item next(Item item) throws LocatorException {
140 DLItem di = (DLItem)item;
141 if (++di.cnt<hosts.length) {
149 public boolean refresh() {
151 InetAddress[] ias = InetAddress.getAllByName(host);
152 Host[] temp = new Host[ias.length * (1 + endPort - startPort)];
154 for (int j=startPort; j<=endPort; ++j) {
155 for (int i=0;i<ias.length;++i) {
156 temp[++cnt] = new Host(ias[i], j, suffix);
160 size = temp.length * (endPort-startPort+1);
162 } catch (Exception e) {
163 access.log(Level.ERROR, e);
168 private String parseHostAndPorts(String aaf_locate, int _start, int defaultPort) throws LocatorException {
170 int colon = aaf_locate.indexOf(':',_start);
172 host = aaf_locate.substring(_start,colon);
174 int left = aaf_locate.indexOf('[', start);
176 int right = aaf_locate.indexOf(']', left + 1);
178 throw new LocatorException("Missing closing bracket in DNSLocator constructor. (requested URL " + aaf_locate + ')');
179 } else if (right == (left + 1)) {
180 throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')');
182 int dash = aaf_locate.indexOf('-', left + 1);
183 if (dash == (right - 1) || dash == (left + 1)) {
184 throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')');
187 startPort = endPort = Integer.parseInt(aaf_locate.substring(left + 1, right));
189 startPort = Integer.parseInt(aaf_locate.substring(left + 1, dash));
190 endPort = Integer.parseInt(aaf_locate.substring(dash + 1, right));
192 slash = aaf_locate.indexOf('/', start);
194 suffix = aaf_locate.substring(slash);
198 slash = aaf_locate.indexOf('/', start);
199 if (slash == start) {
200 throw new LocatorException("Missing port before '/' in DNSLocator constructor. (requested URL " + aaf_locate + ')');
203 startPort = endPort = Integer.parseInt(aaf_locate.substring(start));
205 startPort = endPort = Integer.parseInt(aaf_locate.substring(start, slash));
206 suffix = aaf_locate.substring(slash);
210 slash = aaf_locate.indexOf('/', _start);
211 host = slash<_start?aaf_locate.substring(_start):aaf_locate.substring(_start,slash);
212 startPort = endPort = defaultPort;
220 private InetAddress ia;
221 private Status status;
223 public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {
225 uri = new URI(protocol,null,inetAddress.getCanonicalHostName(),port,suffix,null,null);
226 status = Status.UNTRIED;
229 public String toString() {
230 return uri.toString() + " - " + status.name();
234 private class DLItem implements Item {
235 public DLItem(int i) {
242 public void destroy() {}