added a topology which will stop when a certain method is called or a SIGTERM/SIGINT...
[osm/vim-emu.git] / src / emuvim / examples / profiling.py
1 """
2 Copyright (c) 2015 SONATA-NFV
3 ALL RIGHTS RESERVED.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
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 Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
20 permission.
21
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
27 """
28 """
29 A simple topology with a single data center for usage in son-profile.
30
31 """
32
33 import logging
34 from mininet.log import setLogLevel
35 from emuvim.dcemulator.net import DCNetwork
36 from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint
37 from emuvim.api.sonata import SonataDummyGatekeeperEndpoint
38 from mininet.node import RemoteController
39 import argparse
40 import sys
41 import signal
42
43 logging.basicConfig()
44 LOG = logging.getLogger("sonata-profiling")
45 LOG.setLevel(logging.DEBUG)
46 logging.getLogger("werkzeug").setLevel(logging.WARNING)
47
48 """
49 Catches SIGINT and SIGTERM to shut the topology down gracefully.
50 """
51 class GracefulKiller:
52 def __init__(self, to_be_killed):
53 signal.signal(signal.SIGINT, self.exit_gracefully)
54 signal.signal(signal.SIGTERM, self.exit_gracefully)
55 self.to_be_killed = to_be_killed
56
57 def exit_gracefully(self, signum, frame):
58 self.to_be_killed.stop_it()
59
60
61 """
62 A simple topology with only one data center which will stop when another thread tells it to or when a time limit is reached.
63 :args: an argument list which may contain the time limit
64 """
65 class Profiling:
66
67 stop_now = False
68
69 def __init__(self):
70 GracefulKiller(self)
71 # create topology
72 self.net = DCNetwork(controller=RemoteController, monitor=False, enable_learning=False)
73 self.dc = self.net.addDatacenter("dc1")
74
75 # add the command line interface endpoint to each DC (REST API)
76 self.rapi1 = RestApiEndpoint("0.0.0.0", 5001)
77 self.rapi1.connectDCNetwork(self.net)
78 self.rapi1.connectDatacenter(self.dc)
79 # run API endpoint server (in another thread, don't block)
80 self.rapi1.start()
81
82 # add the SONATA dummy gatekeeper to each DC
83 self.sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000, deploy_sap=False)
84 self.sdkg1.connectDatacenter(self.dc)
85 # run the dummy gatekeeper (in another thread, don't block)
86 self.sdkg1.start()
87
88
89 self.net.start()
90 LOG.info("Started topology")
91 while(not self.stop_now):
92 pass
93 self.net.stop()
94 LOG.info("Stopped topology")
95
96 def stop_it(self):
97 self.stop_now = True
98
99
100 def main(args):
101 setLogLevel('info') # set Mininet loglevel
102 p = Profiling()
103
104
105 if __name__ == '__main__':
106 parser = argparse.ArgumentParser(description="Run a simple topology")
107 parser.add_argument('--time', '-t', metavar='seconds', type=float, help='a time limit', default=-1, required=False, dest='time')
108 arg_list = vars(parser.parse_args(sys.argv[1:]))
109 main(arg_list)