Initial OpenECOMP Demo commit
[demo.git] / vnfs / vLB / DNSClient / src / main / java / DNSServer.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.net.DatagramPacket;
23 import java.net.DatagramSocket;
24
25 /*
26  * Client-side Failure Detector (FD) implementation.
27  * @param server_addr: IP address of the remote server
28  * @param port: the port that the FD service listens for incoming UDP packets
29  * @param timeout: how often the FD checks the status of client processes
30  * @param debug: debug mode on/off
31  */
32
33 public class DNSServer {
34
35         public static void main(String[] args) throws IOException {
36                 if(args.length != 5) {
37                         System.out.println("Missing input parameters");
38                         System.out.println("Usage:");
39                         System.out.print("\t- java FDClient [process ID] [server IP address] [server port] [timeout (sec)] [debug]\n");
40                         System.exit(0);
41                 }
42                 // IP address and port of the remote server
43                 String PID = args[0];
44                 String SERVER_ADDR = args[1];
45                 int PORT = Integer.parseInt(args[2]);
46                 long TIMEOUT = Long.parseLong(args[3]) * 1000; // convert the FD timeout to milliseconds
47                 int debug = Integer.parseInt(args[4]);
48                 boolean DEBUG;
49                 if(debug <= 0)
50                         DEBUG = false;
51                 else
52                         DEBUG = true;
53                 
54                 // Run FD client
55                 new FDClient(PID, SERVER_ADDR, PORT, TIMEOUT, DEBUG);
56                 
57                 // Listen for reply messages
58                 @SuppressWarnings("resource")
59                 DatagramSocket sock = new DatagramSocket(PORT);
60                 byte[] buffer = new byte[256];
61                 boolean gre_tunnel_enabled = false;
62                 
63                 while(true) {
64                         DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
65             sock.receive(packet);
66             String[] content = new String(packet.getData()).trim().split(":"); // Remove leading and trailing spaces
67             String msg_type = content[0];
68             String ip = content[1];
69             // Process only PING UDP packets
70             if(msg_type.equals("PONG")) {
71                                 if(DEBUG) System.out.println("Reply message received from process " + ip);
72                                 if(!gre_tunnel_enabled) {
73                                         // Set GRE tunnel towards load balancer
74                                         String script = new String("bash /opt/FDclient/set_gre_tunnel.sh " + ip);
75                             Runtime.getRuntime().exec(script);
76                                         if(DEBUG) System.out.println("GRE tunnel towards load balancer " + ip + " done");
77                                         gre_tunnel_enabled = true;
78                                 }
79             }
80             else {
81                 if(DEBUG) System.out.println("The received message is not a PONG. Received content: " + content);
82             }
83                 }
84         }
85 }