+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.dnsloc;\r
-\r
-import java.io.IOException;\r
-import java.net.InetAddress;\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-\r
-import org.onap.aaf.cadi.Access;\r
-import org.onap.aaf.cadi.Locator;\r
-import org.onap.aaf.cadi.LocatorException;\r
-import org.onap.aaf.cadi.Access.Level;\r
-\r
-public class DNSLocator implements Locator {\r
- private static enum Status {UNTRIED, OK, INVALID, SLOW};\r
- private static final int CHECK_TIME = 3000;\r
- \r
- private String host, protocol;\r
- private Access access;\r
- private Host[] hosts;\r
- private int startPort, endPort;\r
- private String suffix;\r
- \r
- public DNSLocator(Access access, String protocol, String host, String range) {\r
- this.host = host;\r
- this.protocol = protocol;\r
- this.access = access;\r
- int dash = range.indexOf('-');\r
- if(dash<0) {\r
- startPort = endPort = Integer.parseInt(range);\r
- } else {\r
- startPort = Integer.parseInt(range.substring(0,dash));\r
- endPort = Integer.parseInt(range.substring(dash + 1));\r
- }\r
- refresh();\r
- }\r
-\r
- @Override\r
- public URI get(Item item) throws LocatorException {\r
- return hosts[((DLItem)item).cnt].uri;\r
- }\r
-\r
- @Override\r
- public boolean hasItems() {\r
- for(Host h : hosts) {\r
- if(h.status==Status.OK) {\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
-\r
- @Override\r
- public void invalidate(Item item) {\r
- DLItem di = (DLItem)item;\r
- hosts[di.cnt].status = Status.INVALID;\r
- }\r
-\r
- @Override\r
- public Item best() throws LocatorException {\r
- // not a good "best"\r
- for(int i=0;i<hosts.length;++i) {\r
- switch(hosts[i].status) {\r
- case OK:\r
- return new DLItem(i);\r
- case INVALID:\r
- break;\r
- case SLOW:\r
- break;\r
- case UNTRIED:\r
- try {\r
- if(hosts[i].ia.isReachable(CHECK_TIME)) {\r
- hosts[i].status = Status.OK;\r
- return new DLItem(i);\r
- }\r
- } catch (IOException e) {\r
- throw new LocatorException(e);\r
- }\r
- break;\r
- default:\r
- break;\r
- }\r
- }\r
- throw new LocatorException("No Available URIs for " + host);\r
- }\r
-\r
- @Override\r
- public Item first() throws LocatorException {\r
- return new DLItem(0);\r
- }\r
-\r
- @Override\r
- public Item next(Item item) throws LocatorException {\r
- DLItem di = (DLItem)item;\r
- if(++di.cnt<hosts.length) {\r
- return di;\r
- } else {\r
- return null;\r
- }\r
- }\r
-\r
- @Override\r
- public boolean refresh() {\r
- try {\r
- InetAddress[] ias = InetAddress.getAllByName(host);\r
- Host[] temp = new Host[ias.length * (1 + endPort - startPort)];\r
- int cnt = -1;\r
- for(int j=startPort; j<=endPort; ++j) {\r
- for(int i=0;i<ias.length;++i) {\r
- temp[++cnt] = new Host(ias[i], j, suffix);\r
- }\r
- }\r
- hosts = temp;\r
- return true;\r
- } catch (Exception e) {\r
- access.log(Level.ERROR, e);\r
- }\r
- return false;\r
- }\r
-\r
- private class Host {\r
- private URI uri;\r
- private InetAddress ia;\r
- private Status status;\r
- \r
- public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {\r
- ia = inetAddress;\r
- uri = new URI(protocol,null,inetAddress.getHostAddress(),port,suffix,null,null);\r
- status = Status.UNTRIED;\r
- }\r
- }\r
- \r
- private class DLItem implements Item {\r
- public DLItem(int i) {\r
- cnt = i;\r
- }\r
-\r
- private int cnt;\r
- }\r
-\r
- @Override\r
- public void destroy() {\r
- // TODO Auto-generated method stub\r
- \r
- }\r
-}\r