Remove datarouter-node critical code smells
[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 com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.io.IOException;
31 import java.util.*;
32 import java.net.*;
33
34 /**
35  * Determine if an IP address is from a machine
36  */
37 public class IsFrom {
38     private long nextcheck;
39     private String[] ips;
40     private String fqdn;
41     private static EELFLogger logger = EELFManager.getInstance().getLogger(IsFrom.class);
42
43     /**
44      * 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.
45      */
46     public static void setDNSCache() {
47         java.security.Security.setProperty("networkaddress.cache.ttl", "10");
48     }
49
50     /**
51      * Create an IsFrom for the specified fully qualified domain name.
52      */
53     public IsFrom(String fqdn) {
54         this.fqdn = fqdn;
55     }
56
57     /**
58      * Check if an IP address matches.  If it has been more than
59      * 10 seconds since DNS was last checked for changes to the
60      * IP address(es) of this FQDN, check again.  Then check
61      * if the specified IP address belongs to the FQDN.
62      */
63     public synchronized boolean isFrom(String ip) {
64         long now = System.currentTimeMillis();
65         if (now > nextcheck) {
66             nextcheck = now + 10000;
67             ArrayList<String> hostAddrArray = new ArrayList<>();
68             try {
69                 InetAddress[] addrs = InetAddress.getAllByName(fqdn);
70                 for (InetAddress addr : addrs) {
71                     hostAddrArray.add(addr.getHostAddress());
72                 }
73             } catch (UnknownHostException e) {
74                 logger.error("IsFrom: UnknownHostEx: " + e.toString(), e);
75             }
76             ips = hostAddrArray.toArray(new String[0]);
77             logger.info("IsFrom: DNS ENTRIES FOR FQDN " + fqdn + " : " + Arrays.toString(ips));
78         }
79         for (String ipAddr : ips) {
80             if (ipAddr.equals(ip)) {
81                 return true;
82             }
83         }
84         return false;
85     }
86
87     synchronized boolean isReachable(String ip) {
88         try {
89             if (InetAddress.getByName(ip).isReachable(1000)) {
90                 return true;
91             }
92         } catch (UnknownHostException e) {
93             logger.error("IsFrom: UnknownHostEx: " + e.toString(), e);
94         } catch (IOException e) {
95             logger.error("IsFrom: Failed to parse IP : " + ip + " : " + e.toString(), e);
96         }
97         return false;
98     }
99
100     /**
101      * Return the fully qualified domain name
102      */
103     public String toString() {
104         return (fqdn);
105     }
106 }