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
16 Class for controlling subnets. Right now, just adds subnets and
17 checks whether they exist, because that's all we need.
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/"
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"
35 data = json.loads(content)
36 for key in data["subnetConfig"]:
37 if (key["subnet"] == subnet):
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"
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"
52 print "Subnet", subnet, "could not be added"