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