1 #!/usr/local/bin/python
4 Copyright (c) 2013 Plexxi, Inc. All rights reserved.
6 This program and the accompanying materials are made available under the
7 terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 and is available at http://www.eclipse.org/legal/epl-v10.html
14 class AffinityControl:
17 self.http = httplib2.Http(".cache")
18 self.http.add_credentials("admin", "admin")
19 self.url_prefix = "http://localhost:8080/affinity/nb/v2/affinity/default/"
20 self.flatl2url_prefix = "http://localhost:8080/affinity/nb/v2/flatl2/default/"
23 def add_affinity_group(self, group_name, **kwargs):
25 resp, content = self.http.request(self.url_prefix + "create/group/%s" % group_name, "PUT")
26 if (resp.status != 201):
27 print "AffinityGroup %s could not be created" % group_name
29 # If a list of IPs is passed, add each one
33 resp, content = self.http.request(self.url_prefix + "group/%s/add/ip/%s" % (group_name, ip), "PUT")
34 if (resp.status != 201):
35 print "IP %s could not be added to AffinityGroup %s" % (ip, group_name)
37 print "AffinityGroup %s added successfully. IPs are %s" % (group_name, ips)
38 # If a subnet is passed, add that
39 elif "subnet" in kwargs:
40 ip, mask = kwargs['subnet'].split("/")
41 resp, content = self.http.request(self.url_prefix + "group/%s/addsubnet/ipprefix/%s/mask/%s" % (group_name, ip, mask), "PUT")
42 if (resp.status != 201):
43 print "AffinityGroup could not be created for subnet %s/%s" % (ip, mask)
45 print "AffinityGroup %s added successfully. Subnet is %s/%s" % (group_name, ip, mask)
48 def add_affinity_link(self, link_name, src_group, dst_group):
49 resp, content = self.http.request(self.url_prefix + "create/link/%s/from/%s/to/%s" % (link_name, src_group, dst_group), "PUT")
50 if (resp.status != 201):
51 print "AffinityLink %s could not be added between %s and %s" % (link_name, src_group, dst_group)
53 print "AffinityLink %s added between %s and %s" % (link_name, src_group, dst_group)
56 def add_waypoint(self, link_name, ip):
57 resp, content = self.http.request(self.url_prefix + "link/%s/setwaypoint/%s" % (link_name, ip), "PUT")
58 if (resp.status != 201):
59 print "Waypoint %s could not be set for link %s" % (ip, link_name)
61 print "Waypoint %s successfully set for link %s" % (ip, link_name)
64 def enable_waypoint(self, link_name):
65 resp, content = self.http.request(self.url_prefix + "link/%s/enable" % link_name, "PUT")
66 if (resp.status != 201):
67 print "Waypoint could not be enabled for link %s" % link_name
69 print "Waypoint enabled for link %s" % link_name
72 def disable_waypoint(self, link_name):
73 resp, content = self.http.request(self.url_prefix + "link/%s/disable" % link_name, "PUT")
74 if (resp.status != 201):
75 print "Waypoint could not be disabled for link %s" % link_name
77 print "Waypoint disabled for link %s" % link_name
79 # Enable all affinity rules
80 def enable_affinity(self):
81 resp, content = self.http.request(self.flatl2url_prefix + "enableaffinity", "PUT")
82 if (resp.status != 201):
83 print "Affinity rules could not be enabled."
85 print "Affinity rules enabled"
87 # Disable all affinity rules
88 def disable_affinity(self):
89 resp, content = self.http.request(self.flatl2url_prefix + "disableaffinity", "PUT")
90 if (resp.status != 201):
91 print "Affinity rules could not be disabled"
93 print "Affinity rules disabled"