Fix get_all_affinity_links example in script.
[affinity.git] / scripts / affinity.py
1 #!/usr/local/bin/python
2
3 import httplib2
4 import json
5 import sys
6
7 #
8 # Configure an affinity link and set its waypoint address. 
9 #
10
11 def rest_method(url, verb): 
12     global h
13
14     print "REST call " + url
15     resp, content = h.request(url, verb)
16
17 #    print content
18     print "return code %d" % (resp.status)
19     return content
20
21
22 def list_all_hosts(): 
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']:
28         print host
29
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']:
36         print host
37
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")
42
43 # Tbd
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'])
52
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")
56
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])
63
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")
69
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")
73
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")
77
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")
81
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")
85
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")
90
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")
94
95 def set_deny(setflag='deny'):
96     al = 'inflows'
97     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/' + setflag + '/'
98     rest_method(put_url, "PUT")
99
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")
104
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")
109
110 # Set path isolate. 
111 def set_path_isolate():
112     al = 'inflows'
113     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/setisolate/'
114     rest_method(put_url, "PUT")
115
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")
120
121 def disable_affinity():
122     put_url = 'http://localhost:8080/affinity/nb/v2/flatl2/default/disableaffinity/'
123     rest_method(put_url, "PUT")
124
125 def main():
126     global h
127
128     # Create two affinity groups and a link between them. 
129     # Assign attributes. 
130     client_ws_example()
131
132     get_affinity_group('webservers')
133     get_affinity_group('clients')
134     get_affinity_link('inflows')
135
136     print "get_all_affinity_groups..."
137     get_all_affinity_groups()
138     print "get_all_affinity_links..."
139     get_all_affinity_links()
140
141     set_attr()
142     list_all_hosts()
143     return
144
145 # Set affinity attributes and make sure they are associated with the affinity link. 
146 def set_attr(): 
147     # Set deny. 
148     set_deny('deny')
149     get_affinity_link('inflows')
150
151     # Set waypoint and tap. 
152     set_deny('permit')
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')
157     
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')
162
163     enable_affinity() # Tap to '10.0.0.4'.
164     unset_tap('inflows', '10.0.0.4')
165     set_path_isolate()    
166     get_affinity_link('inflows')
167     enable_affinity() # No action for isolate. Restore normal forwarding. 
168     
169 #if __name__ == "__main__":
170 #    main()
171
172 h = httplib2.Http(".cache")
173 h.add_credentials('admin', 'admin')
174
175
176     
177