Initial OpenECOMP Demo commit
[demo.git] / vnfs / vLB / DNSManager / src / main / java / DNSMembershipManager.java
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ****************************************************************************/
18
19 package main.java;
20
21 import java.io.IOException;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.Set;
25
26 public class DNSMembershipManager {
27         /*
28          * Uses Failure Detector (FD) to keep track of the DNS servers currently active.
29          * @param port: the port that the FD service listens for incoming UDP packets
30          * @param timeout: how often the FD checks the status of client processes
31          * @param threshold: number of missing ping messages before declaring a client dead
32          * @param debug: debug mode on/off
33          */
34         
35         static Set<String> active = new HashSet<String>();
36         
37         @SuppressWarnings("static-access")
38         public static void main(String[] args) throws IOException, InterruptedException {
39                 if(args.length != 5) {
40                         System.out.println("Missing input parameters");
41                         System.out.println("Usage:");
42                         System.out.print("\t- java FDServer [public IP address] [port] [timeout (sec)] [threshold] [debug]\n");
43                         System.exit(0);
44                 }
45                 
46                 // Input parameters: PORT, TIMEOUT, THRESHOLD
47                 String IPADDR = args[0];
48                 int PORT = Integer.parseInt(args[1]);
49                 long TIMEOUT = Long.parseLong(args[2]) * 1000; // convert the FD timeout to milliseconds
50                 int THRESHOLD = Integer.parseInt(args[3]);
51                 int debug = Integer.parseInt(args[4]);
52                 boolean DEBUG;
53                 if(debug <= 0)
54                         DEBUG = false;
55                 else
56                         DEBUG = true;
57                 
58                 // Start Failure Detector
59                 FDServer fd = new FDServer(IPADDR, PORT, TIMEOUT, THRESHOLD, DEBUG);
60                 
61                 // Check the status of client processes periodically. We use the same timeout value as FD
62                 Set<String> active = new HashSet<String>();
63                 while(true) {
64                         Set<String> alive_this_round = fd.getAliveProcesses();
65                         Iterator<String> iter = alive_this_round.iterator();
66                         String pid;
67                         
68                         // Check if there is some new DNS active
69                         while(iter.hasNext()) {
70                                 pid = iter.next();
71                                 if(!active.contains(pid)) {
72                                         active.add(pid);
73                                         // Add the new vDNS to the set of vDNS servers
74                                         String script = new String("bash /opt/FDserver/add_dns.sh " + pid);
75                             Runtime.getRuntime().exec(script);
76                                         if(DEBUG) System.out.println("Adding process " + pid + " to the list of DNSs");
77                                 }
78                         }
79                         
80                         // Remove possible dead DNSs
81                         iter = active.iterator();
82                         while(iter.hasNext()) {
83                                 pid = iter.next();
84                                 if(!alive_this_round.contains(pid)) {
85                                         iter.remove(); // remove element from the iterator to avoid ConcurrentModificationException
86                                         // Remove the new vDNS from the set of vDNS servers
87                                         String script = new String("bash /opt/FDserver/remove_dns.sh " + pid);
88                             Runtime.getRuntime().exec(script);
89                                         if(DEBUG) System.out.println("Removing process " + pid + " from the list of DNSs");
90                                 }
91                         }
92                         Thread.currentThread().sleep(TIMEOUT);
93                 }
94         }
95 }