Fixed bugs while getting demo.py to work.
[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
25     print "list active hosts"
26     put_url = 'http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/active'
27     content = rest_method(put_url, "GET")
28     hostCfg = json.loads(content)
29     for host in hostCfg['hostConfig']:
30         print host
31
32     print "list inactive hosts"
33     put_url = 'http://localhost:8080/controller/nb/v2/hosttracker/default/hosts/inactive'
34     rest_method(put_url, "GET")
35     content = rest_method(put_url, "GET")
36     hostCfg = json.loads(content)
37     for host in hostCfg['hostConfig']:
38         print host
39     
40
41 def waypoint_init():
42     # Create two affinity groups
43
44     print "create web servers group"
45     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/webservers'
46     rest_method(put_url, "PUT")
47
48     print "create external addresses"
49     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/clients'
50     rest_method(put_url, "PUT")
51
52     print "create link inflows"
53     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/link/inflows/from/clients/to/webservers'
54     rest_method(put_url, "PUT")
55
56 #    print "add ip to webservers"
57 #    put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/webservers/add/ip/10.0.0.1'
58 #    rest_method(put_url, "PUT")
59
60     print "add subnet to webservers"
61     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/webservers/addsubnet/ipprefix/10.0.0.1/mask/31'
62     rest_method(put_url, "PUT")
63
64 #    print "add ip to webservers"
65 #    put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/webservers/add/ip/10.0.0.2'
66 #    rest_method(put_url, "PUT")
67
68     print "add ip to external"    
69     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/clients/add/ip/10.0.0.3'
70     rest_method(put_url, "PUT")
71
72
73 def get_all_affinity_groups(): 
74     print "get all affinity groups"
75     get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/affinity-groups'
76     rest_method(get_url, "GET")
77
78 # Tbd
79 def get_all_affinity_links(): 
80     print "get all affinity groups"
81     get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/affinity-links'
82     rest_method(get_url, "GET")
83
84 def get_affinity_group(groupname): 
85     get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/' + groupname
86     rest_method(get_url, "GET")
87
88 def get_affinity_link(linkname):
89     get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + linkname
90     rest_method(get_url, "GET")
91
92 def set_waypoint_address():
93     wp = "10.0.0.2"
94     al = 'inflows'
95     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/setwaypoint/' + wp
96     rest_method(put_url, "PUT")
97
98 def unset_waypoint_address():
99     al = 'inflows'
100     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/unsetwaypoint'
101     rest_method(put_url, "PUT")
102
103 def set_deny(setflag='deny'):
104     al = 'inflows'
105     put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/' + al + '/' + setflag + '/'
106     rest_method(put_url, "PUT")
107
108 #def enable_waypoint():
109 #    put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/inflows/enable/' 
110 #    rest_method(put_url, "PUT")
111
112 #def disable_waypoint():
113 #    put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/inflows/disable/'
114 #    rest_method(put_url, "PUT")
115
116 def enable_affinity():
117     put_url = 'http://localhost:8080/affinity/nb/v2/flatl2/default/enableaffinity/'
118     rest_method(put_url, "PUT")
119
120 def disable_affinity():
121     put_url = 'http://localhost:8080/affinity/nb/v2/flatl2/default/disableaffinity/'
122     rest_method(put_url, "PUT")
123
124 # Add waypoint IP to an affinity link.
125 def main():
126     global h
127
128     waypoint_init()
129
130     get_affinity_group('webservers')
131     get_affinity_group('clients')
132     get_affinity_link('inflows')
133
134     get_all_affinity_groups()
135     list_all_hosts()
136
137     # Set affinity attributes and make sure they are associated with the affinity link. 
138     set_waypoint_address()
139     set_deny('deny')
140     set_deny('permit')
141     get_affinity_link('inflows')
142
143     enable_affinity()
144     disable_affinity()
145
146 #    enable_waypoint()
147 #    disable_waypoint()
148
149 #if __name__ == "__main__":
150 #    main()
151
152 h = httplib2.Http(".cache")
153 h.add_credentials('admin', 'admin')
154
155
156     
157