| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 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 |
| edmaas | 0500890 | 2017-03-04 17:16:33 +0100 | [diff] [blame] | 39 | from time import sleep |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 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. |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 64 | """ |
| 65 | class Profiling: |
| 66 | |
| 67 | stop_now = False |
| 68 | |
| edmaas | e83dcab | 2017-03-04 17:25:14 +0100 | [diff] [blame] | 69 | """ |
| 70 | Set up a simple topology and start it |
| edmaas | c19168f | 2017-03-11 16:32:05 +0100 | [diff] [blame] | 71 | :port: the port the REST interface will be using, port+1 will be in use as well |
| edmaas | e83dcab | 2017-03-04 17:25:14 +0100 | [diff] [blame] | 72 | """ |
| edmaas | c19168f | 2017-03-11 16:32:05 +0100 | [diff] [blame] | 73 | def __init__(self, port=5000): |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 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) |
| edmaas | c19168f | 2017-03-11 16:32:05 +0100 | [diff] [blame] | 80 | self.rapi1 = RestApiEndpoint("0.0.0.0", port+1) |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 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 |
| edmaas | c19168f | 2017-03-11 16:32:05 +0100 | [diff] [blame] | 87 | self.sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", port, deploy_sap=False) |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 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): |
| edmaas | 0500890 | 2017-03-04 17:16:33 +0100 | [diff] [blame] | 96 | sleep(1) |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 97 | self.net.stop() |
| 98 | LOG.info("Stopped topology") |
| 99 | |
| edmaas | e83dcab | 2017-03-04 17:25:14 +0100 | [diff] [blame] | 100 | """ |
| 101 | Set stop value to stop the topology |
| 102 | """ |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 103 | def stop_it(self): |
| 104 | self.stop_now = True |
| 105 | |
| 106 | |
| 107 | def main(args): |
| 108 | setLogLevel('info') # set Mininet loglevel |
| edmaas | c19168f | 2017-03-11 16:32:05 +0100 | [diff] [blame] | 109 | p = Profiling(args.get('port')) |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 110 | |
| 111 | |
| 112 | if __name__ == '__main__': |
| 113 | parser = argparse.ArgumentParser(description="Run a simple topology") |
| edmaas | c19168f | 2017-03-11 16:32:05 +0100 | [diff] [blame] | 114 | parser.add_argument('--port', '-p', type=int, help='the port for the REST interface', default=5000, required=False, dest='port') |
| edmaas | bea3a10 | 2017-02-26 15:42:44 +0100 | [diff] [blame] | 115 | arg_list = vars(parser.parse_args(sys.argv[1:])) |
| 116 | main(arg_list) |