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 private int size = 1; // initial, until refreshed.
46 public DNSLocator(Access access, String protocol, String host, String range) {
48 this.protocol = protocol;
50 int dash = range.indexOf('-');
52 startPort = endPort = Integer.parseInt(range);
54 startPort = Integer.parseInt(range.substring(0,dash));
55 endPort = Integer.parseInt(range.substring(dash + 1));
60 public DNSLocator(Access access, String aaf_locate) throws LocatorException {
62 if (aaf_locate==null) {
63 throw new LocatorException("Null passed into DNSLocator constructor");
66 if (aaf_locate.startsWith("https:")) {
68 start = 8; // https://
70 } else if (aaf_locate.startsWith("http:")) {
75 throw new LocatorException("DNSLocator accepts only https or http protocols. (requested URL " + aaf_locate + ')');
77 host = parseHostAndPorts(aaf_locate, start, defPort);
81 public static DNSLocator create(Access access, String url) throws LocatorException {
82 return new DNSLocator(access, url);
86 public URI get(Item item) throws LocatorException {
87 return hosts[((DLItem)item).cnt].uri;
91 public boolean hasItems() {
92 for (Host h : hosts) {
93 if (h.status==Status.OK) {
101 public void invalidate(Item item) {
102 DLItem di = (DLItem)item;
103 hosts[di.cnt].status = Status.INVALID;
107 public Item best() throws LocatorException {
109 for (int i=0;i<hosts.length;++i) {
110 switch(hosts[i].status) {
112 return new DLItem(i);
119 if (hosts[i].ia.isReachable(CHECK_TIME)) {
120 hosts[i].status = Status.OK;
121 return new DLItem(i);
123 } catch (IOException e) {
124 throw new LocatorException(e);
131 throw new LocatorException("No Available URIs for " + host);
135 public Item first() throws LocatorException {
136 return new DLItem(0);
140 public Item next(Item item) throws LocatorException {
141 DLItem di = (DLItem)item;
142 if (++di.cnt<hosts.length) {
150 public boolean refresh() {
152 InetAddress[] ias = InetAddress.getAllByName(host);
153 Host[] temp = new Host[ias.length * (1 + endPort - startPort)];
155 for (int j=startPort; j<=endPort; ++j) {
156 for (int i=0;i<ias.length;++i) {
157 temp[++cnt] = new Host(ias[i], j, suffix);
161 size = temp.length * (endPort-startPort+1);
163 } catch (Exception e) {
164 access.log(Level.ERROR, e);
169 private String parseHostAndPorts(String aaf_locate, int _start, int defaultPort) throws LocatorException {
171 int colon = aaf_locate.indexOf(':',_start);
173 host = aaf_locate.substring(_start,colon);
175 int left = aaf_locate.indexOf('[', start);
177 int right = aaf_locate.indexOf(']', left + 1);
179 throw new LocatorException("Missing closing bracket in DNSLocator constructor. (requested URL " + aaf_locate + ')');
180 } else if (right == (left + 1)) {
181 throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')');
183 int dash = aaf_locate.indexOf('-', left + 1);
184 if (dash == (right - 1) || dash == (left + 1)) {
185 throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')');
188 startPort = endPort = Integer.parseInt(aaf_locate.substring(left + 1, right));
190 startPort = Integer.parseInt(aaf_locate.substring(left + 1, dash));
191 endPort = Integer.parseInt(aaf_locate.substring(dash + 1, right));
193 slash = aaf_locate.indexOf('/', start);
195 suffix = aaf_locate.substring(slash);
199 slash = aaf_locate.indexOf('/', start);
200 if (slash == start) {
201 throw new LocatorException("Missing port before '/' in DNSLocator constructor. (requested URL " + aaf_locate + ')');
204 startPort = endPort = Integer.parseInt(aaf_locate.substring(start));
206 startPort = endPort = Integer.parseInt(aaf_locate.substring(start, slash));
207 suffix = aaf_locate.substring(slash);
211 slash = aaf_locate.indexOf('/', _start);
212 host = slash<_start?aaf_locate.substring(_start):aaf_locate.substring(_start,slash);
213 startPort = endPort = defaultPort;
221 private InetAddress ia;
222 private Status status;
224 public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {
226 uri = new URI(protocol,null,inetAddress.getCanonicalHostName(),port,suffix,null,null);
227 status = Status.UNTRIED;
230 public String toString() {
231 return uri.toString() + " - " + status.name();
235 private class DLItem implements Item {
236 public DLItem(int i) {
243 public void destroy() {}