b5b1662f8368bb39a7b7604c029c21e0d83ca6c3
[affinity.git] / scripts / affinity_control.py
1 #!/usr/local/bin/python
2
3 '''
4 Copyright (c) 2013 Plexxi, Inc.  All rights reserved.
5
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
9 '''
10
11 import httplib2
12 import json
13
14 class AffinityControl:
15
16     def __init__(self):
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/"
21
22     # Add affinity group
23     def add_affinity_group(self, group_name, **kwargs):
24         # Create the group
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
28             return
29         # If a list of IPs is passed, add each one
30         if "ips" in kwargs:
31             ips = kwargs['ips']
32             for ip in ips:
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)
36                     return
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)
44                 return
45             print "AffinityGroup %s added successfully. Subnet is %s/%s" % (group_name, ip, mask)
46
47     # Add affinity link
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)
52             return
53         print "AffinityLink %s added between %s and %s" % (link_name, src_group, dst_group)
54
55     # Add waypoint
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)
60             return
61         print "Waypoint %s successfully set for link %s" % (ip, link_name)
62
63     # Enable waypoint
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
68             return
69         print "Waypoint enabled for link %s" % link_name
70
71     # Disable waypoint
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
76             return
77         print "Waypoint disabled for link %s" % link_name
78
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."
84             return
85         print "Affinity rules enabled"
86
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" 
92             return
93         print "Affinity rules disabled"