Mass-pnf-simulator publishing ports
[integration.git] / test / mocks / mass-pnf-sim / mass-pnf-sim.py
1 #!/usr/bin/env python3
2 import argparse
3 import sys
4 from subprocess import *
5 from subprocess import STDOUT
6 import subprocess
7 import ipaddress
8 import time
9
10 parser = argparse.ArgumentParser()
11 parser.add_argument(
12     '--bootstrap',
13     help='Bootstrapping the system',
14 )
15
16 parser.add_argument(
17     '--trigger',
18     help='Trigger one single VES event from each simulator',
19 )
20
21 parser.add_argument(
22     '--ipves',
23     help='IP of the VES collector',
24 )
25
26 parser.add_argument(
27     '--ipfileserver',
28     help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
29 )
30
31 parser.add_argument(
32     '--ipstart',
33     help='IP address range beginning',
34 )
35
36 parser.add_argument(
37     '--clean',
38     action='store_true',
39     help='Cleaning work-dirs',
40 )
41
42 parser.add_argument(
43     '--start',
44     help='Starting instances',
45 )
46
47 parser.add_argument(
48     '--status',
49     help='Status',
50 )
51
52 parser.add_argument(
53     '--stop',
54     help='Stopping instances',
55 )
56
57 args = parser.parse_args()
58
59 if args.bootstrap and args.ipstart and args.ipves:
60     print("Bootstrap:")
61
62     start_port=2000
63
64     for i in range(int(args.bootstrap)):
65         print("PNF simulator instance: " + str(i) + ".")
66
67         ip_subnet = ipaddress.ip_address(args.ipstart) + int(0 + (i * 16))
68         print("\tIp Subnet:" + str(ip_subnet))
69         # The IP ranges are in distance of 16 compared to each other.
70         # This is matching the /28 subnet mask used in the dockerfile inside.
71
72         ip_gw = ipaddress.ip_address(args.ipstart) + int(1 + (i * 16))
73         print("\tIP Gateway:" + str(ip_gw))
74
75         IpPnfSim = ipaddress.ip_address(args.ipstart) + int(2 + (i * 16))
76         print("\tIp Pnf SIM:" + str(IpPnfSim))
77
78         IpFileServer = args.ipfileserver
79
80         
81         PortSftp=start_port +1
82         PortFtps=start_port +2 
83         start_port +=2
84         IpFtps = ipaddress.ip_address(args.ipstart) + int(3 + (i * 16))
85         print("\tIp Ftps: " + str(IpFtps))
86  
87         IpSftp = ipaddress.ip_address(args.ipstart) + int(4 + (i * 16))
88         print("\tIp Sftp:" + str(IpSftp))
89
90         foldername = "pnf-sim-lw-" + str(i)
91         completed = subprocess.run('mkdir ' + foldername, shell=True)
92         print('\tCreating folder:', completed.stdout)
93         completed = subprocess.run(
94             'cp -r pnf-sim-lightweight/* ' +
95             foldername,
96             shell=True)
97         print('\tCloning folder:', completed.stdout)
98
99         composercmd = "./simulator.sh compose " +\
100             str(ip_gw) + " " +\
101             str(ip_subnet) + " " +\
102             str(i) + " " +\
103             str(args.ipves) + " " +\
104             str(IpPnfSim) + " " +\
105             str(IpFileServer) + " " +\
106             str(PortSftp) + " " +\
107             str(PortFtps) + " " +\
108             str(IpFtps) + " " +\
109             str(IpSftp)
110
111         completed = subprocess.run(
112             'set -x; cd ' +
113             foldername +
114             '; ' +
115             composercmd,
116             shell=True)
117         print('Cloning:', completed.stdout)
118
119     sys.exit()
120
121 if args.clean:
122     completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
123     print('Deleting:', completed.stdout)
124     sys.exit()
125
126 if args.start:
127
128     for i in range(int(args.start)):
129         foldername = "pnf-sim-lw-" + str(i)
130
131         completed = subprocess.run(
132             'set -x ; cd ' +
133             foldername +
134             "; bash -x ./simulator.sh start",
135             shell=True)
136         print('Starting:', completed.stdout)
137
138         time.sleep(5)
139
140 if args.status:
141
142     for i in range(int(args.status)):
143         foldername = "pnf-sim-lw-" + str(i)
144
145         completed = subprocess.run(
146             'cd ' +
147             foldername +
148             "; ./simulator.sh status",
149             shell=True)
150         print('Status:', completed.stdout)
151
152 if args.stop:
153     for i in range(int(args.stop)):
154         foldername = "pnf-sim-lw-" + str(i)
155
156         completed = subprocess.run(
157             'cd ' +
158             foldername +
159             "; ./simulator.sh stop " + str(i),
160             shell=True)
161         print('Stopping:', completed.stdout)
162
163
164 if args.trigger:
165     print("Triggering VES sending:")
166
167     for i in range(int(args.trigger)):
168         foldername = "pnf-sim-lw-" + str(i)
169
170         completed = subprocess.run(
171             'cd ' +
172             foldername +
173             "; ./simulator.sh trigger-simulator",
174             shell=True)
175         print('Status:', completed.stdout)
176
177 else:
178     print("No instruction was defined")
179     sys.exit()