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)
24 print "list active hosts"
25 put_url = 'http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/active'
26 content = rest_method(put_url, "GET")
27 hostCfg = json.loads(content)
28 for host in hostCfg['hostConfig']:
31 print "list inactive hosts"
32 put_url = 'http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/inactive'
33 rest_method(put_url, "GET")
34 content = rest_method(put_url, "GET")
35 hostCfg = json.loads(content)
36 for host in hostCfg['hostConfig']:
39 def get_all_affinity_groups():
40 print "get all affinity groups"
41 get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/affinity-groups'
42 rest_method(get_url, "GET")
45 def get_all_affinity_links():
46 print "get all affinity links"
47 get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/affinity-links'
48 rest_method(get_url, "GET")
50 def get_affinity_group(groupname):
51 get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/' + groupname
52 rest_method(get_url, "GET")
54 def get_affinity_link(linkname):
55 get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + linkname
56 content = rest_method(get_url, "GET")
57 affyLinkCfg = json.loads(content)
58 for key in affyLinkCfg:
59 print "%10s : %s" % (key, affyLinkCfg[key])
61 def client_ws_example():
62 # Create two affinity groups
63 print "create web servers group"
64 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/webservers'
65 rest_method(put_url, "PUT")
67 print "create external addresses"
68 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/clients'
69 rest_method(put_url, "PUT")
71 print "create link inflows"
72 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/link/inflows/from/clients/to/webservers'
73 rest_method(put_url, "PUT")
75 print "add subnet to webservers"
76 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/webservers/addsubnet/ipprefix/10.0.0.1/mask/31'
77 rest_method(put_url, "PUT")
79 print "add ip to external"
80 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/clients/add/ip/10.0.0.3'
81 rest_method(put_url, "PUT")
83 # Only one waypoint supported.
84 def set_waypoint_address(al, wp):
85 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/setwaypoint/' + wp
86 rest_method(put_url, "PUT")
88 def unset_waypoint_address(al):
89 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/unsetwaypoint'
90 rest_method(put_url, "PUT")
92 def set_deny(setflag='deny'):
94 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/' + setflag + '/'
95 rest_method(put_url, "PUT")
97 # Add a tap to ipaddress.
98 def set_tap(al, ipaddr):
99 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/settap/' + ipaddr
100 rest_method(put_url, "PUT")
102 # Add a tap to ipaddress.
103 def unset_tap(al, ipaddr):
104 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/unsettap/' + ipaddr
105 rest_method(put_url, "PUT")
108 def set_path_isolate():
110 put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/setisolate/'
111 rest_method(put_url, "PUT")
113 # Re-program the network using OF southbound.
114 def enable_affinity():
115 put_url = 'http://localhost:8080/affinity/nb/v2/flatl2/default/enableaffinity/'
116 rest_method(put_url, "PUT")
118 def disable_affinity():
119 put_url = 'http://localhost:8080/affinity/nb/v2/flatl2/default/disableaffinity/'
120 rest_method(put_url, "PUT")
125 # Create two affinity groups and a link between them.
129 get_affinity_group('webservers')
130 get_affinity_group('clients')
131 get_affinity_link('inflows')
133 print "get_all_affinity_groups..."
134 get_all_affinity_groups()
135 print "get_all_affinity_links..."
136 get_all_affinity_links()
142 # Set affinity attributes and make sure they are associated with the affinity link.
146 get_affinity_link('inflows')
148 # Set waypoint and tap.
150 set_waypoint_address('inflows', '10.0.0.2')
151 set_tap('inflows', '10.0.0.6')
152 set_tap('inflows', '10.0.0.4')
153 get_affinity_link('inflows')
155 # Change a few affinity attributes and get the new link configuration.
156 unset_tap('inflows', '10.0.0.6')
157 print "get_affinity_link..."
158 get_affinity_link('inflows')
160 enable_affinity() # Tap to '10.0.0.4'.
161 unset_tap('inflows', '10.0.0.4')
163 get_affinity_link('inflows')
164 enable_affinity() # No action for isolate. Restore normal forwarding.
166 #if __name__ == "__main__":
169 h = httplib2.Http(".cache")
170 h.add_credentials('admin', 'admin')