DR AAF CADI integration
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / IsFrom.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24
25 package org.onap.dmaap.datarouter.node;
26
27 import org.apache.log4j.Logger;
28
29 import java.io.IOException;
30 import java.util.*;
31 import java.net.*;
32
33 /**
34  * Determine if an IP address is from a machine
35  */
36 public class IsFrom {
37     private long nextcheck;
38     private String[] ips;
39     private String fqdn;
40     private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.node.IsFrom");
41
42     /**
43      * Configure the JVM DNS cache to have a 10 second TTL.  This needs to be called very very early or it won't have any effect.
44      */
45     public static void setDNSCache() {
46         java.security.Security.setProperty("networkaddress.cache.ttl", "10");
47     }
48
49     /**
50      * Create an IsFrom for the specified fully qualified domain name.
51      */
52     public IsFrom(String fqdn) {
53         this.fqdn = fqdn;
54     }
55
56     /**
57      * Check if an IP address matches.  If it has been more than
58      * 10 seconds since DNS was last checked for changes to the
59      * IP address(es) of this FQDN, check again.  Then check
60      * if the specified IP address belongs to the FQDN.
61      */
62     public synchronized boolean isFrom(String ip) {
63         long now = System.currentTimeMillis();
64         if (now > nextcheck) {
65             nextcheck = now + 10000;
66             ArrayList<String> hostAddrArray = new ArrayList<>();
67             try {
68                 InetAddress[] addrs = InetAddress.getAllByName(fqdn);
69                 for (InetAddress addr : addrs) {
70                     hostAddrArray.add(addr.getHostAddress());
71                 }
72             } catch (UnknownHostException e) {
73                 logger.debug("IsFrom: UnknownHostEx: " + e.toString(), e);
74             }
75             ips = hostAddrArray.toArray(new String[0]);
76             logger.info("IsFrom: DNS ENTRIES FOR FQDN " + fqdn + " : " + Arrays.toString(ips));
77         }
78         for (String ipAddr : ips) {
79             if (ipAddr.equals(ip)) {
80                 return true;
81             }
82         }
83         return false;
84     }
85
86     synchronized boolean isReachable(String ip) {
87         try {
88             if (InetAddress.getByName(ip).isReachable(1000)) {
89                 return true;
90             }
91         } catch (UnknownHostException e) {
92             logger.debug("IsFrom: UnknownHostEx: " + e.toString(), e);
93         } catch (IOException e) {
94             logger.debug("IsFrom: Failed to parse IP : " + ip + " : " + e.toString(), e);
95         }
96         return false;
97     }
98
99     /**
100      * Return the fully qualified domain name
101      */
102     public String toString() {
103         return (fqdn);
104     }
105 }