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