Merge remote-tracking branch 'upstream/master'
[osm/vim-emu.git] / src / emuvim / examples / resource_model_demo_topology.py
1 """
2 A simple topology to test resource model support.
3 """
4
5 import logging
6 import time
7 import os
8 from mininet.log import setLogLevel
9 from mininet.node import Controller
10 from emuvim.dcemulator.net import DCNetwork
11 from emuvim.api.zerorpc.compute import ZeroRpcApiEndpoint
12 from emuvim.api.sonata import SonataDummyGatekeeperEndpoint
13 from emuvim.dcemulator.resourcemodel.upb.simple import UpbSimpleCloudDcRM
14
15 logging.basicConfig(level=logging.INFO)
16
17
18 RESOURCE_LOG_PATH = "resource.log"
19
20
21 def create_topology1():
22 cleanup()
23 # create topology
24 # use a maximum of 50% cpu time for containers added to data centers
25 net = DCNetwork(dc_emulation_max_cpu=0.5, controller=Controller)
26 # add some data centers and create a topology
27 dc1 = net.addDatacenter("dc1", resource_log_path=RESOURCE_LOG_PATH)
28 dc2 = net.addDatacenter("dc2", resource_log_path=RESOURCE_LOG_PATH)
29 s1 = net.addSwitch("s1")
30 net.addLink(dc1, s1, delay="10ms")
31 net.addLink(dc2, s1, delay="20ms")
32
33 # create and assign resource models for each DC
34 rm1 = UpbSimpleCloudDcRM(max_cu=4, max_mu=1024)
35 rm2 = UpbSimpleCloudDcRM(max_cu=6)
36 dc1.assignResourceModel(rm1)
37 dc2.assignResourceModel(rm2)
38
39 # add the command line interface endpoint to each DC
40 zapi1 = ZeroRpcApiEndpoint("0.0.0.0", 4242)
41 zapi1.connectDatacenter(dc1)
42 zapi1.connectDatacenter(dc2)
43 # run API endpoint server (in another thread, don't block)
44 zapi1.start()
45
46 # add the SONATA dummy gatekeeper to each DC
47 sdkg1 = SonataDummyGatekeeperEndpoint("0.0.0.0", 8000)
48 sdkg1.connectDatacenter(dc1)
49 sdkg1.connectDatacenter(dc2)
50 # run the dummy gatekeeper (in another thread, don't block)
51 sdkg1.start()
52
53 # start the emulation platform
54 net.start()
55 print "Wait a moment and allocate some compute start some compute resources..."
56 time.sleep(2)
57 dc1.startCompute("vnf1")
58 dc1.startCompute("vnf2", flavor_name="tiny")
59 dc1.startCompute("vnf3", flavor_name="small")
60 dc2.startCompute("vnf4", flavor_name="medium")
61 print "... done."
62 time.sleep(5)
63 print "Removing instances ..."
64 dc1.stopCompute("vnf1")
65 dc2.stopCompute("vnf4")
66 print "... done"
67 net.CLI()
68 net.stop()
69
70
71 def cleanup():
72 try:
73 os.remove(RESOURCE_LOG_PATH)
74 except OSError:
75 pass
76
77
78 def main():
79 setLogLevel('info') # set Mininet loglevel
80 create_topology1()
81
82
83 if __name__ == '__main__':
84 main()