[AAF-21] Initial code import
[aaf/cadi.git] / client / src / main / java / com / att / cadi / locator / DNSLocator.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.locator;\r
25 \r
26 import java.io.IOException;\r
27 import java.net.InetAddress;\r
28 import java.net.URI;\r
29 import java.net.URISyntaxException;\r
30 \r
31 import com.att.cadi.Access;\r
32 import com.att.cadi.Locator;\r
33 import com.att.cadi.LocatorException;\r
34 import com.att.cadi.Access.Level;\r
35 \r
36 public class DNSLocator implements Locator<URI> {\r
37         private static enum Status {UNTRIED, OK, INVALID, SLOW};\r
38         private static final int CHECK_TIME = 3000;\r
39         \r
40         private String host, protocol;\r
41         private Access access;\r
42         private Host[] hosts;\r
43         private int startPort, endPort;\r
44         private String suffix;\r
45         \r
46         public DNSLocator(Access access, String protocol, String host, String range) {\r
47                 this.host = host;\r
48                 this.protocol = protocol;\r
49                 this.access = access;\r
50                 int dash = range.indexOf('-');\r
51                 if(dash<0) {\r
52                         startPort = endPort = Integer.parseInt(range);\r
53                 } else {\r
54                         startPort = Integer.parseInt(range.substring(0,dash));\r
55                         endPort = Integer.parseInt(range.substring(dash + 1));\r
56                 }\r
57                 refresh();\r
58         }\r
59 \r
60         @Override\r
61         public URI get(Item item) throws LocatorException {\r
62                 return hosts[((DLItem)item).cnt].uri;\r
63         }\r
64 \r
65         @Override\r
66         public boolean hasItems() {\r
67                 for(Host h : hosts) {\r
68                         if(h.status==Status.OK) {\r
69                                 return true;\r
70                         }\r
71                 }\r
72                 return false;\r
73         }\r
74 \r
75         @Override\r
76         public void invalidate(Item item) {\r
77                 DLItem di = (DLItem)item;\r
78                 hosts[di.cnt].status = Status.INVALID;\r
79         }\r
80 \r
81         @Override\r
82         public Item best() throws LocatorException {\r
83                 // not a good "best"\r
84                 for(int i=0;i<hosts.length;++i) {\r
85                         switch(hosts[i].status) {\r
86                                 case OK:\r
87                                         return new DLItem(i);\r
88                                 case INVALID:\r
89                                         break;\r
90                                 case SLOW:\r
91                                         break;\r
92                                 case UNTRIED:\r
93                                         try {\r
94                                                 if(hosts[i].ia.isReachable(CHECK_TIME)) {\r
95                                                         hosts[i].status = Status.OK;\r
96                                                         return new DLItem(i);\r
97                                                 }\r
98                                         } catch (IOException e) {\r
99                                                 throw new LocatorException(e);\r
100                                         }\r
101                                         break;\r
102                                 default:\r
103                                         break;\r
104                         }\r
105                 }\r
106                 throw new LocatorException("No Available URIs for " + host);\r
107         }\r
108 \r
109         @Override\r
110         public Item first() throws LocatorException {\r
111                 return new DLItem(0);\r
112         }\r
113 \r
114         @Override\r
115         public Item next(Item item) throws LocatorException {\r
116                 DLItem di = (DLItem)item;\r
117                 if(++di.cnt<hosts.length) {\r
118                         return di;\r
119                 } else {\r
120                         return null;\r
121                 }\r
122         }\r
123 \r
124         @Override\r
125         public boolean refresh() {\r
126                 try {\r
127                         InetAddress[] ias = InetAddress.getAllByName(host);\r
128                         Host[] temp = new Host[ias.length * (1 + endPort - startPort)];\r
129                         int cnt = -1;\r
130                         for(int j=startPort; j<=endPort; ++j) {\r
131                                 for(int i=0;i<ias.length;++i) {\r
132                                         temp[++cnt] = new Host(ias[i], j, suffix);\r
133                                 }\r
134                         }\r
135                         hosts = temp;\r
136                         return true;\r
137                 } catch (Exception e) {\r
138                         access.log(Level.ERROR, e);\r
139                 }\r
140                 return false;\r
141         }\r
142 \r
143         private class Host {\r
144                 private URI uri;\r
145                 private InetAddress ia;\r
146                 private Status status;\r
147                 \r
148                 public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {\r
149                         ia = inetAddress;\r
150                         uri = new URI(protocol,null,inetAddress.getHostAddress(),port,suffix,null,null);\r
151                         status = Status.UNTRIED;\r
152                 }\r
153         }\r
154         \r
155         private class DLItem implements Item {\r
156                 public DLItem(int i) {\r
157                         cnt = i;\r
158                 }\r
159 \r
160                 private int cnt;\r
161         }\r
162         \r
163         public void destroy() {}\r
164 }\r