Fix Northbound API annotations.
[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     print "done"
20     return content
21
22
23 def list_all_hosts(): 
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']:
29         print host
30
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']:
37         print host
38
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")
43
44 # Tbd
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")
49
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")
53
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])
60
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")
66
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")
70
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")
74
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")
78
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")
82
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")
87
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")
91
92 def set_deny(setflag='deny'):
93     al = 'inflows'
94     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/' + setflag + '/'
95     rest_method(put_url, "PUT")
96
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")
101
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")
106
107 # Set path isolate. 
108 def set_path_isolate():
109     al = 'inflows'
110     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/setisolate/'
111     rest_method(put_url, "PUT")
112
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")
117
118 def disable_affinity():
119     put_url = 'http://localhost:8080/affinity/nb/v2/flatl2/default/disableaffinity/'
120     rest_method(put_url, "PUT")
121
122 def main():
123     global h
124
125     # Create two affinity groups and a link between them. 
126     # Assign attributes. 
127     client_ws_example()
128
129     get_affinity_group('webservers')
130     get_affinity_group('clients')
131     get_affinity_link('inflows')
132
133     print "get_all_affinity_groups..."
134     get_all_affinity_groups()
135     print "get_all_affinity_links..."
136     get_all_affinity_links()
137
138     set_attr()
139     list_all_hosts()
140     return
141
142 # Set affinity attributes and make sure they are associated with the affinity link. 
143 def set_attr(): 
144     # Set deny. 
145     set_deny('deny')
146     get_affinity_link('inflows')
147
148     # Set waypoint and tap. 
149     set_deny('permit')
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')
154     
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')
159
160     enable_affinity() # Tap to '10.0.0.4'.
161     unset_tap('inflows', '10.0.0.4')
162     set_path_isolate()    
163     get_affinity_link('inflows')
164     enable_affinity() # No action for isolate. Restore normal forwarding. 
165     
166 #if __name__ == "__main__":
167 #    main()
168
169 h = httplib2.Http(".cache")
170 h.add_credentials('admin', 'admin')
171
172
173     
174