blob: c4a756133d7e97d714359d675c2a9d6274f4de4a [file] [log] [blame]
edmaasbea3a102017-02-26 15:42:44 +01001"""
2Copyright (c) 2015 SONATA-NFV
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
27"""
28"""
29A simple topology with a single data center for usage in son-profile.
30
31"""
32
33import logging
34from mininet.log import setLogLevel
35from emuvim.dcemulator.net import DCNetwork
36from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint
37from emuvim.api.sonata import SonataDummyGatekeeperEndpoint
38from mininet.node import RemoteController
edmaas05008902017-03-04 17:16:33 +010039from time import sleep
edmaasbea3a102017-02-26 15:42:44 +010040import argparse
41import sys
42import signal
43
44logging.basicConfig()
45LOG = logging.getLogger("sonata-profiling")
46LOG.setLevel(logging.DEBUG)
47logging.getLogger("werkzeug").setLevel(logging.WARNING)
48
49"""
50 Catches SIGINT and SIGTERM to shut the topology down gracefully.
51"""
52class 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.
edmaasbea3a102017-02-26 15:42:44 +010064"""
65class Profiling:
66
67 stop_now = False
68
edmaase83dcab2017-03-04 17:25:14 +010069 """
70 Set up a simple topology and start it
edmaasc19168f2017-03-11 16:32:05 +010071 :port: the port the REST interface will be using, port+1 will be in use as well
edmaase83dcab2017-03-04 17:25:14 +010072 """
edmaasc19168f2017-03-11 16:32:05 +010073 def __init__(self, port=5000):
edmaasbea3a102017-02-26 15:42:44 +010074 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)
edmaasc19168f2017-03-11 16:32:05 +010080 self.rapi1 = RestApiEndpoint("0.0.0.0", port+1)
edmaasbea3a102017-02-26 15:42:44 +010081 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
edmaasc19168f2017-03-11 16:32:05 +010087 self.sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", port, deploy_sap=False)
edmaasbea3a102017-02-26 15:42:44 +010088 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):
edmaas05008902017-03-04 17:16:33 +010096 sleep(1)
edmaasbea3a102017-02-26 15:42:44 +010097 self.net.stop()
98 LOG.info("Stopped topology")
99
edmaase83dcab2017-03-04 17:25:14 +0100100 """
101 Set stop value to stop the topology
102 """
edmaasbea3a102017-02-26 15:42:44 +0100103 def stop_it(self):
104 self.stop_now = True
105
106
107def main(args):
108 setLogLevel('info') # set Mininet loglevel
edmaasc19168f2017-03-11 16:32:05 +0100109 p = Profiling(args.get('port'))
edmaasbea3a102017-02-26 15:42:44 +0100110
111
112if __name__ == '__main__':
113 parser = argparse.ArgumentParser(description="Run a simple topology")
edmaasc19168f2017-03-11 16:32:05 +0100114 parser.add_argument('--port', '-p', type=int, help='the port for the REST interface', default=5000, required=False, dest='port')
edmaasbea3a102017-02-26 15:42:44 +0100115 arg_list = vars(parser.parse_args(sys.argv[1:]))
116 main(arg_list)