aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/mass-pnf-sim/mass-pnf-sim.py
blob: db567ede0fa4b7d87de4bab8fadda8c0ef0fe045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
import argparse
import sys
import subprocess
import ipaddress
import time
import logging
from requests import get
from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout

def validate_url(url):
    '''Helper function to perform --urlves input param validation'''
    logger = logging.getLogger("urllib3")
    logger.setLevel(logging.WARNING)
    try:
        get(url, timeout=0.001)
    except (MissingSchema, InvalidSchema, InvalidURL):
        raise argparse.ArgumentTypeError(f'{url} is not a valid URL')
    except (ConnectionError, ConnectTimeout):
        pass
    return url

def validate_ip(ip):
    '''Helper function to validate input param is a vaild IP address'''
    try:
        ip_valid = ipaddress.ip_address(ip)
    except ValueError:
        raise argparse.ArgumentTypeError(f'{ip} is not a valid IP address')
    else:
        return ip_valid

if sys.stdout.isatty():
    logging.basicConfig(level=logging.INFO, format='\033[92m[%(levelname)s]\033[0m %(message)s')
else:
    logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')

parser = argparse.ArgumentParser()
parser.add_argument('--bootstrap', help='Bootstrap the system', type=int, metavar='COUNT')
parser.add_argument('--trigger', help='Trigger one single VES event from each simulator', type=int,
                    metavar='COUNT')
parser.add_argument('--triggerstart', help='Trigger only a subset of the simulators (note --triggerend)', type=int,
                    metavar='COUNT_START')
parser.add_argument('--triggerend', help='Last instance to trigger', type=int, metavar='COUNT_END')
parser.add_argument('--urlves', help='URL of the VES collector', type=validate_url, metavar='URL')
parser.add_argument('--ipfileserver', help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
                    type=validate_ip, metavar='IP')
parser.add_argument('--typefileserver', help='Type of the file server (SFTP/FTPS) to be included in the VES event',
                    type=str, choices=['sftp', 'ftps'])
parser.add_argument('--ipstart', help='IP address range beginning', type=validate_ip, metavar='IP')
parser.add_argument('--clean', action='store_true', help='Clean work-dirs')
parser.add_argument('--start', help='Start instances', type=int, metavar='COUNT')
parser.add_argument('--status', help='Status', type=int, metavar='COUNT')
parser.add_argument('--stop', help='Stop instances', type=int, metavar='COUNT')
parser.add_argument('--verbose', help='Verbosity level', choices=['info', 'debug'],
                    type=str, default='debug')

args = parser.parse_args()

logger = logging.getLogger(__name__)
logger.setLevel(getattr(logging, args.verbose.upper()))

if args.bootstrap and args.ipstart and args.urlves:
    logger.info("Bootstrap:")

    start_port=2000
    ftps_pasv_port_start=8000
    ftps_pasv_port_num_of_ports=10

    ftps_pasv_port_end=ftps_pasv_port_start + ftps_pasv_port_num_of_ports

    for i in range(args.bootstrap):
        logger.info("PNF simulator instance: " + str(i) + ".")

        ip_subnet = args.ipstart + int(0 + (i * 16))
        logger.debug("\tIp Subnet:" + str(ip_subnet))
        # The IP ranges are in distance of 16 compared to each other.
        # This is matching the /28 subnet mask used in the dockerfile inside.

        ip_gw = args.ipstart + int(1 + (i * 16))
        logger.debug("\tIP Gateway:" + str(ip_gw))

        IpPnfSim = args.ipstart + int(2 + (i * 16))
        logger.debug("\tIp Pnf SIM:" + str(IpPnfSim))

        IpFileServer = str(args.ipfileserver)
        TypeFileServer = args.typefileserver

        PortSftp=start_port +1
        PortFtps=start_port +2
        start_port +=2
        UrlFtps = str(args.ipstart + int(3 + (i * 16)))
        logger.debug("\tUrl Ftps: " + str(UrlFtps))

        UrlSftp = str(args.ipstart + int(4 + (i * 16)))
        logger.debug("\tUrl Sftp: " + str(UrlSftp))

        foldername = "pnf-sim-lw-" + str(i)
        completed = subprocess.run('mkdir ' + foldername, shell=True)
        logger.info(f'\tCreating folder: {completed.stdout}')
        completed = subprocess.run(
            'cp -r pnf-sim-lightweight/* ' +
            foldername,
            shell=True)
        logger.info(f'\tCloning folder: {completed.stdout}')

        composercmd = "./simulator.sh compose " + \
            str(ip_gw) + " " + \
            str(ip_subnet) + " " + \
            str(i) + " " + \
            args.urlves + " " + \
            str(IpPnfSim) + " " + \
            IpFileServer + " " + \
            TypeFileServer + " " + \
            str(PortSftp) + " " + \
            str(PortFtps) + " " + \
            str(UrlFtps) + " " + \
            str(UrlSftp) + " " + \
            str(ftps_pasv_port_start) + " " + \
            str(ftps_pasv_port_end)

        completed = subprocess.run(
            'set -x; cd ' +
            foldername +
            '; ' +
            composercmd,
            shell=True)
        logger.info(f'Cloning: {completed.stdout}')

        ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1
        ftps_pasv_port_end += ftps_pasv_port_num_of_ports +1

    completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
    logger.info(f"Build docker image: {completed.stdout}")

    sys.exit()

if args.clean:
    completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
    logger.info(f'Deleting: {completed.stdout}')
    sys.exit()

if args.start:

    for i in range(args.start):
        foldername = "pnf-sim-lw-" + str(i)

        completed = subprocess.run(
            'set -x ; cd ' +
            foldername +
            "; bash -x ./simulator.sh start",
            shell=True)
        logger.info(f'Starting: {completed.stdout}')

        time.sleep(5)

if args.status:

    for i in range(args.status):
        foldername = "pnf-sim-lw-" + str(i)

        completed = subprocess.run(
            'cd ' +
            foldername +
            "; ./simulator.sh status",
            shell=True)
        logger.info(f'Status: {completed.stdout}')

if args.stop:
    for i in range(args.stop):
        foldername = "pnf-sim-lw-" + str(i)

        completed = subprocess.run(
            'cd ' +
            foldername +
            "; ./simulator.sh stop " + str(i),
            shell=True)
        logger.info(f'Stopping: {completed.stdout}')


if args.trigger:
    logger.info("Triggering VES sending:")

    for i in range(args.trigger):
        foldername = "pnf-sim-lw-" + str(i)

        completed = subprocess.run(
            'cd ' +
            foldername +
            "; ./simulator.sh trigger-simulator",
            shell=True)
        logger.info(f'Status: {completed.stdout}')

if args.triggerstart and args.triggerend:
    logger.info("Triggering VES sending by a range of simulators:")

    for i in range(args.triggerstart, args.triggerend+1):
        foldername = "pnf-sim-lw-" + str(i)
        logger.info("Instance being processed:" + str(i))

        completed = subprocess.run(
            'cd ' +
            foldername +
            "; ./simulator.sh trigger-simulator",
            shell=True)
        logger.info(f'Status: {completed.stdout}')
else:
    logger.warning("No instruction was defined")
    parser.print_usage()
    sys.exit()