Merge "Fixed Sonar issue"
[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.util.*;
30 import java.net.*;
31
32 /**
33  * Determine if an IP address is from a machine
34  */
35 public class IsFrom {
36     private long nextcheck;
37     private String[] ips;
38     private String fqdn;
39     private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.node.IsFrom");
40
41     /**
42      * 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.
43      */
44     public static void setDNSCache() {
45         java.security.Security.setProperty("networkaddress.cache.ttl", "10");
46     }
47
48     /**
49      * Create an IsFrom for the specified fully qualified domain name.
50      */
51     public IsFrom(String fqdn) {
52         this.fqdn = fqdn;
53     }
54
55     /**
56      * Check if an IP address matches.  If it has been more than
57      * 10 seconds since DNS was last checked for changes to the
58      * IP address(es) of this FQDN, check again.  Then check
59      * if the specified IP address belongs to the FQDN.
60      */
61     public synchronized boolean isFrom(String ip) {
62         long now = System.currentTimeMillis();
63         if (now > nextcheck) {
64             nextcheck = now + 10000;
65             Vector<String> v = new Vector<>();
66             try {
67                 InetAddress[] addrs = InetAddress.getAllByName(fqdn);
68                 for (InetAddress a : addrs) {
69                     v.add(a.getHostAddress());
70                 }
71             } catch (UnknownHostException e) {
72                 logger.debug("IsFrom: UnknownHostEx: " + e.toString(), e);
73             }
74             ips = v.toArray(new String[v.size()]);
75             logger.info("IsFrom: DNS ENTRIES FOR FQDN " + fqdn + " : " + Arrays.toString(ips));
76         }
77         for (String s : ips) {
78             if (s.equals(ip) || s.equals(System.getenv("DMAAP_DR_PROV_SERVICE_HOST"))) {
79                 return (true);
80             }
81         }
82         return (false);
83     }
84
85     /**
86      * Return the fully qualified domain name
87      */
88     public String toString() {
89         return (fqdn);
90     }
91 }