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 = 8; // https://
68 } else if (aaf_locate.startsWith("http:")) {
73 throw new LocatorException("DNSLocator accepts only https or http protocols. (requested URL " + aaf_locate + ')');
75 host = parseHostAndPorts(aaf_locate, start, defPort);
79 public static DNSLocator create(Access access, String url) throws LocatorException {
80 return new DNSLocator(access, url);
84 public URI get(Item item) throws LocatorException {
85 return hosts[((DLItem)item).cnt].uri;
89 public boolean hasItems() {
90 for (Host h : hosts) {
91 if (h.status==Status.OK) {
99 public void invalidate(Item item) {
100 DLItem di = (DLItem)item;
101 hosts[di.cnt].status = Status.INVALID;
105 public Item best() throws LocatorException {
107 for (int i=0;i<hosts.length;++i) {
108 switch(hosts[i].status) {
110 return new DLItem(i);
117 if (hosts[i].ia.isReachable(CHECK_TIME)) {
118 hosts[i].status = Status.OK;
119 return new DLItem(i);
121 } catch (IOException e) {
122 throw new LocatorException(e);
129 throw new LocatorException("No Available URIs for " + host);
133 public Item first() throws LocatorException {
134 return new DLItem(0);
138 public Item next(Item item) throws LocatorException {
139 DLItem di = (DLItem)item;
140 if (++di.cnt<hosts.length) {
148 public boolean refresh() {
150 InetAddress[] ias = InetAddress.getAllByName(host);
151 Host[] temp = new Host[ias.length * (1 + endPort - startPort)];
153 for (int j=startPort; j<=endPort; ++j) {
154 for (int i=0;i<ias.length;++i) {
155 temp[++cnt] = new Host(ias[i], j, suffix);
160 } catch (Exception e) {
161 access.log(Level.ERROR, e);
166 private String parseHostAndPorts(String aaf_locate, int _start, int defaultPort) throws LocatorException {
168 int colon = aaf_locate.indexOf(':',_start);
170 host = aaf_locate.substring(_start,colon);
172 int left = aaf_locate.indexOf('[', start);
174 int right = aaf_locate.indexOf(']', left + 1);
176 throw new LocatorException("Missing closing bracket in DNSLocator constructor. (requested URL " + aaf_locate + ')');
177 } else if (right == (left + 1)) {
178 throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')');
180 int dash = aaf_locate.indexOf('-', left + 1);
181 if (dash == (right - 1) || dash == (left + 1)) {
182 throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')');
185 startPort = endPort = Integer.parseInt(aaf_locate.substring(left + 1, right));
187 startPort = Integer.parseInt(aaf_locate.substring(left + 1, dash));
188 endPort = Integer.parseInt(aaf_locate.substring(dash + 1, right));
190 slash = aaf_locate.indexOf('/', start);
192 suffix = aaf_locate.substring(slash);
196 slash = aaf_locate.indexOf('/', start);
197 if (slash == start) {
198 throw new LocatorException("Missing port before '/' in DNSLocator constructor. (requested URL " + aaf_locate + ')');
201 startPort = endPort = Integer.parseInt(aaf_locate.substring(start));
203 startPort = endPort = Integer.parseInt(aaf_locate.substring(start, slash));
204 suffix = aaf_locate.substring(slash);
208 slash = aaf_locate.indexOf('/', _start);
209 host = slash<_start?aaf_locate.substring(_start):aaf_locate.substring(_start,slash);
210 startPort = endPort = defaultPort;
218 private InetAddress ia;
219 private Status status;
221 public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {
223 uri = new URI(protocol,null,inetAddress.getCanonicalHostName(),port,suffix,null,null);
224 status = Status.UNTRIED;
227 public String toString() {
228 return uri.toString() + " - " + status.name();
232 private class DLItem implements Item {
233 public DLItem(int i) {
240 public void destroy() {}