Fix: Let Jenkins script call unitttest. Added link to examles.
[osm/vim-emu.git] / src / emuvim / examples / son-monitor_test_topo.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 two PoPs for the y1 demo story board.
30
31 (dc1) <<-->> s1 <<-->> (dc2)
32 """
33
34 import logging
35 from mininet.log import setLogLevel
36 from emuvim.dcemulator.net import DCNetwork
37 from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint
38 from emuvim.api.sonata import SonataDummyGatekeeperEndpoint
39 from mininet.node import RemoteController
40 import signal
41 import sys
42 import time
43
44 logging.basicConfig(level=logging.INFO)
45
46 exit = False
47
48 def create_topology1():
49
50 global exit
51
52 # create topology
53 net = DCNetwork(controller=RemoteController, monitor=True, enable_learning = False)
54 dc1 = net.addDatacenter("dc1")
55 dc2 = net.addDatacenter("dc2")
56 s1 = net.addSwitch("s1")
57 net.addLink(dc1, s1, delay="10ms")
58 net.addLink(dc2, s1, delay="20ms")
59
60 # add the command line interface endpoint to each DC (REST API)
61 rapi1 = RestApiEndpoint("0.0.0.0", 5001)
62 rapi1.connectDatacenter(dc1)
63 rapi1.connectDatacenter(dc2)
64 # connect total network also, needed to do the chaining and monitoring
65 rapi1.connectDCNetwork(net)
66 # run API endpoint server (in another thread, don't block)
67 rapi1.start()
68
69 # add the SONATA dummy gatekeeper to each DC
70 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 5000, deploy_sap=True)
71 sdkg1.connectDatacenter(dc1)
72 sdkg1.connectDatacenter(dc2)
73 # run the dummy gatekeeper (in another thread, don't block)
74 sdkg1.start()
75
76 # start the emulation platform
77 net.start()
78
79 #does not work from docker compose (cannot start container in interactive mode)
80 #cli = net.CLI()
81 # instead wait here:
82 logging.info("waiting for SIGTERM or SIGINT signal")
83 while not exit:
84 time.sleep(1)
85 logging.info("got SIG signal")
86 net.stop()
87
88 def exit_gracefully(signum, frame):
89 """
90 7. At shutdown, we should receive the unix signal here and shutdown gracefully
91 """
92
93 global exit
94
95 logging.info('Signal handler called with signal {0}'.format(signum))
96 exit = True
97
98
99 def main():
100 setLogLevel('info') # set Mininet loglevel
101 # add the SIGTERM handler (eg. received when son-emu docker container stops)
102 signal.signal(signal.SIGTERM, exit_gracefully)
103 # also handle Ctrl-C
104 signal.signal(signal.SIGINT, exit_gracefully)
105 # start the topology
106 create_topology1()
107
108
109 if __name__ == '__main__':
110 main()