Add l2agent as the provider of connectivity/forwarding service for test/debug bundle...
[affinity.git] / scripts / subnet.py
1 #!/usr/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 import sys
14
15 '''
16 Class for controlling subnets.  Right now, just adds subnets and
17 checks whether they exist, because that's all we need.
18 '''
19 class SubnetControl:
20
21     def __init__(self):
22         self.http = httplib2.Http(".cache")
23         self.http.add_credentials("admin", "admin")
24         self.url_prefix = "http://localhost:8080/controller/nb/v2/subnetservice/default/"
25
26     # Checks whether subnet exists.  Checks against the actual subnet
27     # string (e.g., "10.0.0.255/1"), not the subnet name.  Will not
28     # catch things like overlapping subnets.
29     def exists(self, subnet):
30         resp, content = self.http.request(self.url_prefix + "subnets", "GET")
31         if (resp.status != 200):
32             print "Fatal error - can't check for subnet existence"
33             sys.exit(-1)
34
35         data = json.loads(content)
36         for key in data["subnetConfig"]:
37             if (key["subnet"] == subnet):
38                 return True
39         return False
40
41     # Add a subnet if it doesn't already exist.
42     def add_subnet(self, subnet_name, subnet):
43         if (self.exists(subnet)):
44             print "Subnet", subnet, "already exists"
45             return
46         subnet_config = dict(name=subnet_name, subnet=subnet)
47         json_data = json.dumps(subnet_config)
48         resp, content = self.http.request(self.url_prefix + "subnet/" + subnet_name, "POST", json_data, {'Content-Type': 'application/json'})
49         if (resp.status == 201):
50             print "Subnet", subnet, "added"
51         else:
52             print "Subnet", subnet, "could not be added"