Merge "Unit test instability fix"
[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
22 parser.add_argument(
23     '--triggerstart',
24     help='Trigger only a subset of the simulators (note --triggerend)',
25 )
26
27 parser.add_argument(
28     '--triggerend',
29     help='Last instance to trigger',
30 )
31
32 parser.add_argument(
33     '--urlves',
34     help='URL of the VES collector',
35 )
36
37 parser.add_argument(
38     '--ipfileserver',
39     help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
40 )
41
42 parser.add_argument(
43     '--ipstart',
44     help='IP address range beginning',
45 )
46
47 parser.add_argument(
48     '--clean',
49     action='store_true',
50     help='Cleaning work-dirs',
51 )
52
53 parser.add_argument(
54     '--start',
55     help='Starting instances',
56 )
57
58 parser.add_argument(
59     '--status',
60     help='Status',
61 )
62
63 parser.add_argument(
64     '--stop',
65     help='Stopping instances',
66 )
67
68 args = parser.parse_args()
69
70 if args.bootstrap and args.ipstart and args.urlves:
71     print("Bootstrap:")
72
73     start_port=2000
74
75     for i in range(int(args.bootstrap)):
76         print("PNF simulator instance: " + str(i) + ".")
77
78         ip_subnet = ipaddress.ip_address(args.ipstart) + int(0 + (i * 16))
79         print("\tIp Subnet:" + str(ip_subnet))
80         # The IP ranges are in distance of 16 compared to each other.
81         # This is matching the /28 subnet mask used in the dockerfile inside.
82
83         ip_gw = ipaddress.ip_address(args.ipstart) + int(1 + (i * 16))
84         print("\tIP Gateway:" + str(ip_gw))
85
86         IpPnfSim = ipaddress.ip_address(args.ipstart) + int(2 + (i * 16))
87         print("\tIp Pnf SIM:" + str(IpPnfSim))
88
89         IpFileServer = args.ipfileserver
90
91         
92         PortSftp=start_port +1
93         PortFtps=start_port +2 
94         start_port +=2
95         UrlFtps = str(ipaddress.ip_address(args.ipstart) + int(3 + (i * 16)))
96         print("\tUrl Ftps: " + str(UrlFtps))
97  
98         UrlSftp = str(ipaddress.ip_address(args.ipstart) + int(4 + (i * 16)))
99         print("\tUrl Sftp: " + str(UrlSftp))
100
101         foldername = "pnf-sim-lw-" + str(i)
102         completed = subprocess.run('mkdir ' + foldername, shell=True)
103         print('\tCreating folder:', completed.stdout)
104         completed = subprocess.run(
105             'cp -r pnf-sim-lightweight/* ' +
106             foldername,
107             shell=True)
108         print('\tCloning folder:', completed.stdout)
109
110         composercmd = "./simulator.sh compose " + \
111             str(ip_gw) + " " + \
112             str(ip_subnet) + " " + \
113             str(i) + " " + \
114             str(args.urlves) + " " + \
115             str(IpPnfSim) + " " + \
116             str(IpFileServer) + " " + \
117             str(PortSftp) + " " + \
118             str(PortFtps) + " " + \
119             str(UrlFtps) + " " + \
120             str(UrlSftp)
121
122         completed = subprocess.run(
123             'set -x; cd ' +
124             foldername +
125             '; ' +
126             composercmd,
127             shell=True)
128         print('Cloning:', completed.stdout)
129
130     completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
131     print("Build docker image: ", completed.stdout)
132
133     sys.exit()
134
135 if args.clean:
136     completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
137     print('Deleting:', completed.stdout)
138     sys.exit()
139
140 if args.start:
141
142     for i in range(int(args.start)):
143         foldername = "pnf-sim-lw-" + str(i)
144
145         completed = subprocess.run(
146             'set -x ; cd ' +
147             foldername +
148             "; bash -x ./simulator.sh start",
149             shell=True)
150         print('Starting:', completed.stdout)
151
152         time.sleep(5)
153
154 if args.status:
155
156     for i in range(int(args.status)):
157         foldername = "pnf-sim-lw-" + str(i)
158
159         completed = subprocess.run(
160             'cd ' +
161             foldername +
162             "; ./simulator.sh status",
163             shell=True)
164         print('Status:', completed.stdout)
165
166 if args.stop:
167     for i in range(int(args.stop)):
168         foldername = "pnf-sim-lw-" + str(i)
169
170         completed = subprocess.run(
171             'cd ' +
172             foldername +
173             "; ./simulator.sh stop " + str(i),
174             shell=True)
175         print('Stopping:', completed.stdout)
176
177
178 if args.trigger:
179     print("Triggering VES sending:")
180
181     for i in range(int(args.trigger)):
182         foldername = "pnf-sim-lw-" + str(i)
183
184         completed = subprocess.run(
185             'cd ' +
186             foldername +
187             "; ./simulator.sh trigger-simulator",
188             shell=True)
189         print('Status:', completed.stdout)
190         
191 if args.triggerstart and args.triggerend:
192     print("Triggering VES sending by a range of simulators:")
193     
194     for i in range(int(args.triggerstart), int(args.triggerend)+1):
195         foldername = "pnf-sim-lw-" + str(i)
196         print("Instance being processed:" + str(i))
197          
198         completed = subprocess.run(
199             'cd ' +
200             foldername +
201             "; ./simulator.sh trigger-simulator",
202             shell=True)
203         print('Status:', completed.stdout)
204          
205         
206
207 else:
208     print("No instruction was defined")
209     sys.exit()