Added example topology for daemonized execution.
[osm/vim-emu.git] / src / emuvim / examples / profiling.py
1 """
2 Copyright (c) 2017 SONATA-NFV and Paderborn University
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, Paderborn University
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 from time import sleep
40 import argparse
41 import sys
42 import signal
43
44 logging.basicConfig()
45 LOG = logging.getLogger("sonata-profiling")
46 LOG.setLevel(logging.DEBUG)
47 logging.getLogger("werkzeug").setLevel(logging.WARNING)
48
49 """
50 Catches SIGINT and SIGTERM to shut the topology down gracefully.
51 """
52 class GracefulKiller:
53 def __init__(self, to_be_killed):
54 signal.signal(signal.SIGINT, self.exit_gracefully)
55 signal.signal(signal.SIGTERM, self.exit_gracefully)
56 self.to_be_killed = to_be_killed
57
58 def exit_gracefully(self, signum, frame):
59 self.to_be_killed.stop_it()
60
61
62 """
63 A simple topology with only one data center which will stop when another thread tells it to or when a time limit is reached.
64 """
65 class Profiling:
66
67 stop_now = False
68
69 """
70 Set up a simple topology and start it
71 :port: the port the REST interface will be using, port+1 will be in use as well
72 """
73 def __init__(self, port=5000):
74 GracefulKiller(self)
75 # create topology
76 self.net = DCNetwork(controller=RemoteController, monitor=False, enable_learning=False)
77 self.dc = self.net.addDatacenter("dc1")
78
79 # add the command line interface endpoint to each DC (REST API)
80 self.rapi1 = RestApiEndpoint("0.0.0.0", port+1)
81 self.rapi1.connectDCNetwork(self.net)
82 self.rapi1.connectDatacenter(self.dc)
83 # run API endpoint server (in another thread, don't block)
84 self.rapi1.start()
85
86 # add the SONATA dummy gatekeeper to each DC
87 self.sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", port, deploy_sap=False)
88 self.sdkg1.connectDatacenter(self.dc)
89 # run the dummy gatekeeper (in another thread, don't block)
90 self.sdkg1.start()
91
92
93 self.net.start()
94 LOG.info("Started topology")
95 while(not self.stop_now):
96 sleep(1)
97 self.net.stop()
98 LOG.info("Stopped topology")
99
100 """
101 Set stop value to stop the topology
102 """
103 def stop_it(self):
104 self.stop_now = True
105
106
107 def main(args):
108 setLogLevel('info') # set Mininet loglevel
109 p = Profiling(args.get('port'))
110
111
112 if __name__ == '__main__':
113 parser = argparse.ArgumentParser(description="Run a simple topology")
114 parser.add_argument('--port', '-p', type=int, help='the port for the REST interface', default=5000, required=False, dest='port')
115 arg_list = vars(parser.parse_args(sys.argv[1:]))
116 main(arg_list)