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 parsePorts(aaf_locate.substring(start), port);
80 public URI get(Item item) throws LocatorException {
81 return hosts[((DLItem)item).cnt].uri;
85 public boolean hasItems() {
86 for (Host h : hosts) {
87 if (h.status==Status.OK) {
95 public void invalidate(Item item) {
96 DLItem di = (DLItem)item;
97 hosts[di.cnt].status = Status.INVALID;
101 public Item best() throws LocatorException {
103 for (int i=0;i<hosts.length;++i) {
104 switch(hosts[i].status) {
106 return new DLItem(i);
113 if (hosts[i].ia.isReachable(CHECK_TIME)) {
114 hosts[i].status = Status.OK;
115 return new DLItem(i);
117 } catch (IOException e) {
118 throw new LocatorException(e);
125 throw new LocatorException("No Available URIs for " + host);
129 public Item first() throws LocatorException {
130 return new DLItem(0);
134 public Item next(Item item) throws LocatorException {
135 DLItem di = (DLItem)item;
136 if (++di.cnt<hosts.length) {
144 public boolean refresh() {
146 InetAddress[] ias = InetAddress.getAllByName(host);
147 Host[] temp = new Host[ias.length * (1 + endPort - startPort)];
149 for (int j=startPort; j<=endPort; ++j) {
150 for (int i=0;i<ias.length;++i) {
151 temp[++cnt] = new Host(ias[i], j, suffix);
156 } catch (Exception e) {
157 access.log(Level.ERROR, e);
162 private void parsePorts(String aaf_locate, int defaultPort) throws LocatorException {
164 int colon = aaf_locate.indexOf(':');
167 int left = aaf_locate.indexOf('[', start);
169 int right = aaf_locate.indexOf(']', left + 1);
171 throw new LocatorException("Missing closing bracket in DNSLocator constructor. (requested URL " + aaf_locate + ')');
172 } else if (right == (left + 1)) {
173 throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')');
175 int dash = aaf_locate.indexOf('-', left + 1);
176 if (dash == (right - 1) || dash == (left + 1)) {
177 throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')');
180 startPort = endPort = Integer.parseInt(aaf_locate.substring(left + 1, right));
182 startPort = Integer.parseInt(aaf_locate.substring(left + 1, dash));
183 endPort = Integer.parseInt(aaf_locate.substring(dash + 1, right));
187 slash = aaf_locate.indexOf('/', start);
188 if (slash == start) {
189 throw new LocatorException("Missing port before '/' in DNSLocator constructor. (requested URL " + aaf_locate + ')');
192 startPort = endPort = Integer.parseInt(aaf_locate.substring(start));
194 startPort = endPort = Integer.parseInt(aaf_locate.substring(start, slash));
198 startPort = endPort = defaultPort;
204 private InetAddress ia;
205 private Status status;
207 public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {
209 uri = new URI(protocol,null,inetAddress.getHostAddress(),port,suffix,null,null);
210 status = Status.UNTRIED;
214 private class DLItem implements Item {
215 public DLItem(int i) {
222 public void destroy() {}