1 #!/usr/local/bin/python
8 # Configure an affinity link and set its waypoint address.
11 def rest_method(url, verb):
14 print "REST call " + url
15 resp, content = h.request(url, verb)
18 print "return code %d" % (resp.status)
23 print "list active hosts"
24 get_url = 'http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/active'
25 content = rest_method(get_url, "GET")
26 hostCfg = json.loads(content)
27 for host in hostCfg['hostConfig']:
30 print "list inactive hosts"
31 get_url = 'http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/inactive'
32 rest_method(get_url, "GET")
33 content = rest_method(put_url, "GET")
34 hostCfg = json.loads(content)
35 for host in hostCfg['hostConfig']:
38 def get_all_affinity_groups():
39 print "get all affinity groups"
40 get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/affinity-groups'
41 rest_method(get_url, "GET")
44 def get_all_affinity_links():
45 print "get all affinity links"
46 get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/affinity-links'
47 content = rest_method(get_url, "GET")
48 affylinks = json.loads(content)
49 for link in affylinks['affinityLinks']:
50 print "Affinity link: %s" % link
51 get_affinity_link(link['name'])
53 def get_affinity_group(groupname):
54 get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/' + groupname
55 rest_method(get_url, "GET")
57 def get_affinity_link(linkname):
58 get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + linkname
59 content = rest_method(get_url, "GET")
60 affyLinkCfg = json.loads(content)
61 for key in sorted(affyLinkCfg):
62 print "%10s : %s" % (key, affyLinkCfg[key])
64 def client_ws_example():
65 # Create two affinity groups
66 print "create web servers group"
67 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/webservers'
68 rest_method(put_url, "PUT")
70 print "create external addresses"
71 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/clients'
72 rest_method(put_url, "PUT")
74 print "create link inflows"
75 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/link/inflows/from/clients/to/webservers'
76 rest_method(put_url, "PUT")
78 print "add subnet to webservers"
79 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/webservers/addsubnet/ipprefix/10.0.0.1/mask/31'
80 rest_method(put_url, "PUT")
82 print "add ip to external"
83 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/clients/add/ip/10.0.0.3'
84 rest_method(put_url, "PUT")
86 # Only one waypoint supported.
87 def set_waypoint_address(al, wp):
88 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/setwaypoint/' + wp
89 rest_method(put_url, "PUT")
91 def unset_waypoint_address(al):
92 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/unsetwaypoint'
93 rest_method(put_url, "PUT")
95 def set_deny(setflag='deny'):
97 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/' + setflag + '/'
98 rest_method(put_url, "PUT")
100 # Add a tap to ipaddress.
101 def set_tap(al, ipaddr):
102 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/settap/' + ipaddr
103 rest_method(put_url, "PUT")
105 # Add a tap to ipaddress.
106 def unset_tap(al, ipaddr):
107 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/unsettap/' + ipaddr
108 rest_method(put_url, "PUT")
111 def set_path_isolate():
113 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/setisolate/'
114 rest_method(put_url, "PUT")
116 # Re-program the network using OF southbound.
117 def enable_affinity():
118 put_url = 'http://localhost:8080/affinity/nb/v2/flatl2/default/enableaffinity/'
119 rest_method(put_url, "PUT")
121 def disable_affinity():
122 put_url = 'http://localhost:8080/affinity/nb/v2/flatl2/default/disableaffinity/'
123 rest_method(put_url, "PUT")
128 # Create two affinity groups and a link between them.
132 get_affinity_group('webservers')
133 get_affinity_group('clients')
134 get_affinity_link('inflows')
136 print "get_all_affinity_groups..."
137 get_all_affinity_groups()
138 print "get_all_affinity_links..."
139 get_all_affinity_links()
145 # Set affinity attributes and make sure they are associated with the affinity link.
149 get_affinity_link('inflows')
151 # Set waypoint and tap.
153 set_waypoint_address('inflows', '10.0.0.2')
154 set_tap('inflows', '10.0.0.6')
155 set_tap('inflows', '10.0.0.4')
156 get_affinity_link('inflows')
158 # Change a few affinity attributes and get the new link configuration.
159 unset_tap('inflows', '10.0.0.6')
160 print "get_affinity_link..."
161 get_affinity_link('inflows')
163 enable_affinity() # Tap to '10.0.0.4'.
164 unset_tap('inflows', '10.0.0.4')
166 get_affinity_link('inflows')
167 enable_affinity() # No action for isolate. Restore normal forwarding.
169 #if __name__ == "__main__":
172 h = httplib2.Http(".cache")
173 h.add_credentials('admin', 'admin')