Removed outdated zerorpc API.
[osm/vim-emu.git] / src / emuvim / examples / resource_model_demo_topology.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 to test resource model support.
30 """
31
32 import logging
33 import time
34 import os
35 from mininet.log import setLogLevel
36 from mininet.node import Controller
37 from emuvim.dcemulator.net import DCNetwork
38 from emuvim.api.zerorpc.compute import ZeroRpcApiEndpoint
39 from emuvim.api.sonata import SonataDummyGatekeeperEndpoint
40 from emuvim.dcemulator.resourcemodel.upb.simple import UpbSimpleCloudDcRM, UpbOverprovisioningCloudDcRM
41
42 logging.basicConfig(level=logging.INFO)
43
44
45 RESOURCE_LOG_PATH = "resource.log"
46
47
48 def create_topology1():
49 cleanup()
50 # create topology
51 # use a maximum of 50% cpu time for containers added to data centers
52 net = DCNetwork(dc_emulation_max_cpu=0.5, controller=Controller)
53 # add some data centers and create a topology
54 dc1 = net.addDatacenter("dc1", resource_log_path=RESOURCE_LOG_PATH)
55 dc2 = net.addDatacenter("dc2", resource_log_path=RESOURCE_LOG_PATH)
56 s1 = net.addSwitch("s1")
57 net.addLink(dc1, s1, delay="10ms")
58 net.addLink(dc2, s1, delay="20ms")
59
60 # create and assign resource models for each DC
61 rm1 = UpbSimpleCloudDcRM(max_cu=4, max_mu=1024)
62 rm2 = UpbOverprovisioningCloudDcRM(max_cu=4)
63 dc1.assignResourceModel(rm1)
64 dc2.assignResourceModel(rm2)
65
66 # add the command line interface endpoint to each DC
67 zapi1 = ZeroRpcApiEndpoint("0.0.0.0", 4242)
68 zapi1.connectDatacenter(dc1)
69 zapi1.connectDatacenter(dc2)
70 # run API endpoint server (in another thread, don't block)
71 zapi1.start()
72
73 # start the emulation platform
74 net.start()
75 print "Wait a moment and allocate some compute start some compute resources..."
76 time.sleep(2)
77 dc1.startCompute("vnf1")
78 dc1.startCompute("vnf2", flavor_name="tiny")
79 dc1.startCompute("vnf3", flavor_name="small")
80 dc2.startCompute("vnf4", flavor_name="medium")
81 dc2.startCompute("vnf5", flavor_name="medium")
82 dc2.startCompute("vnf6", flavor_name="medium")
83 print "... done."
84 time.sleep(5)
85 print "Removing instances ..."
86 dc1.stopCompute("vnf1")
87 dc2.stopCompute("vnf4")
88 print "... done"
89 net.CLI()
90 net.stop()
91
92
93 def cleanup():
94 try:
95 os.remove(RESOURCE_LOG_PATH)
96 except OSError:
97 pass
98
99
100 def main():
101 setLogLevel('info') # set Mininet loglevel
102 create_topology1()
103
104
105 if __name__ == '__main__':
106 main()