def delete_topology(schema, count): hyphen = "-" topoid = schema["TopoId"] for device in schema["Details"]: devid = device["Device_id"] dev_id = topoid + hyphen + devid dev1 = device["Device_type"] dev_type1 = DeviceTypes.getDeviceTypeFromStr(dev1) for conn in device["Connections"]: end = get_dev_id(schema, conn["Connected_to"]) end_id = topoid + hyphen + end if count[devid][end] > 0: if1 = conn["Local_interface_name"] if2 = conn["remoteInterfaceName"] #print("Connname is ", dev_id ,"conn is ", end_id ,"if1 is", if1 ,"if2 is", if2) dev2 = get_device_type_from_name(schema, conn["Connected_to"]) dev_type2 = DeviceTypes.getDeviceTypeFromStr(dev2) #print("device type is ", dev_type1, dev_type2) free_ip_address(schema, devid, if1, dev_type1) free_ip_address(schema, end, if2, dev_type2) ret = sbw.deleteLink(dev_id, if1, dev_type1, end_id, if2, dev_type2) count[end][devid] -= 1 ret = sbw.deleteDevice(dev_id, dev_type1)
def rnd_delete_link(current, dev, end): topoid = current["TopoId"] hyphen = "-" details = current["Details"] count = 0 for i in details: if i["Device_name"] == dev: dev1 = i["Device_id"] dev_id = topoid + hyphen + i["Device_id"] dev_type = i["Device_type"] for j in i["Connections"]: if j["Connected_to"] == end: if1 = j["Local_interface_name"] if2 = j["remoteInterfaceName"] ip1 = j["Local_ip_address"] ip2 = j["remoteIPAddress"] count += 1 elif i["Device_name"] == end: dev2 = i["Device_id"] end_id = topoid + hyphen + i["Device_id"] end_type = i["Device_type"] #print("Removing link for", dev_id, if1, dev_type, end_id, if2, end_type) sb_dev_type = DeviceTypes.getDeviceTypeFromStr(dev_type) sb_end_type = DeviceTypes.getDeviceTypeFromStr(end_type) #return ret = sbw.deleteLink(dev_id, if1, sb_dev_type, end_id, if2, sb_end_type) ret = make_changes_for_var_links(current, ret, dev1, dev_id, dev2, end_id) #print(ret) uj.removeConnection(ret) if count == 1: free_ip_address(current, dev1, if1, sb_dev_type) free_ip_address(current, dev2, if2, sb_end_type)
def rnd_add_link(new, dev, end, count): topoid = new["TopoId"] hyphen = "-" details = new["Details"] for i in details: if i["Device_name"] == dev: dev1 = i["Device_id"] dev_id = topoid + hyphen + i["Device_id"] dev_type = i["Device_type"] elif i["Device_name"] == end: dev2 = i["Device_id"] end_id = topoid + hyphen + i["Device_id"] end_type = i["Device_type"] #print("dev1 is", dev1, "dev2 is", dev2, str(count[dev2][dev1])) if1 = topoid + dev1 + hyphen + dev2 + hyphen + "if" + str( count[dev2][dev1]) if2 = topoid + dev2 + hyphen + dev1 + hyphen + "if" + str( count[dev2][dev1]) if get_interface_name(new, dev1, dev2) != "": if1 = get_interface_name(new, dev1, dev2) if get_interface_name(new, dev2, dev1) != "": if2 = get_interface_name(schema, dev2, dev1) #print("Adding link for", dev_id, if1, dev_type, end_id, if2, end_type) count[dev2][dev1] -= 1 #return sb_dev_type = DeviceTypes.getDeviceTypeFromStr(dev_type) sb_end_type = DeviceTypes.getDeviceTypeFromStr(end_type) ret = sbw.createLink(dev_id, if1, sb_dev_type, end_id, if2, sb_end_type) ret = make_changes_for_var_links(new, ret, dev1, dev_id, dev2, end_id) #print(ret) uj.constructConnection(ret) ##Also assign IP address if count[dev1][dev2] == 1 or (count[dev1][dev2] > 1): subnet = get_subnet() if sb_dev_type != DeviceTypes.TYPE_BRIDGE: ip1 = get_ip(subnet) if get_ip_address_from_if(new, dev1, if1) != "": ip1 = get_ip_address_from_if(schema, dev1, if1) #print("IP1 is", ip1) ret = sbw.assignInterfaceIP(dev_id, if1, ip1) ret = json.loads(ret) ret["deviceName"] = get_device_name_from_id(new, dev1) #print(ret) uj.addDeviceInterfaceIP(json.dumps(ret)) if sb_end_type != DeviceTypes.TYPE_BRIDGE: ip2 = get_ip(subnet) if get_ip_address_from_if(new, dev2, if2) != "": ip2 = get_ip_address_from_if(new, dev2, if2) #print("IP2 is", ip2) ret = sbw.assignInterfaceIP(end_id, if2, ip2) ret = json.loads(ret) ret["deviceName"] = get_device_name_from_id(new, dev2) #print(ret) uj.addDeviceInterfaceIP(json.dumps(ret))
def createContainer(self, containerName, imageType): """ [Checks if a container with the given name 'containerName' exists. If not, creates the container.] Note: [DO NOT CALL THIS METHOD. USE createDevice() INSTEAD.] Arguments: containerName {[str]} -- [Name of the container to be created.] imageType {[DeviceTypes]} -- [Type of docker image to be used to create the container.] Returns: [JSON] -- [JSON containing container ID, name, docker image, SSH IP and error code(if any).] """ errCode=ErrorCodes.NO_ERROR retJSON = {} retCode = self.checkContainerExists(containerName) if retCode == ErrorCodes.INVALID_ARGUMENT: self.logger.error("createContainer - INVALID CONTAINER NAME - " + containerName) errCode=retCode if retCode == ErrorCodes.CONTAINER_ALREADY_EXISTS: self.logger.error("createContainer - CONTAINER ALREADY EXISTS - " + containerName) errCode=retCode if self.topologyName=="" or self.checkManagementBridgeExists(self.topologyName+"br") == ErrorCodes.BRIDGE_DOESNOT_EXIST: errCode=ErrorCodes.DOCKER_BRIDGE_MISSING self.logger.error("createContainer - DOCKER BRIDGE MISSING - " + self.topologyName) else: imageName = DeviceTypes.getImageName(imageType) bridgeName = self.topologyName+"br" SouthBoundActual.createContainer(self.logger, containerName, imageName, bridgeName) # Verify if container was created if self.checkContainerExists(containerName) == ErrorCodes.CONTAINER_ALREADY_EXISTS: retJSON["dockerImage"]=imageName retJSON["dockerID"]=SouthBoundActual.getContainerID(self.logger,containerName) if imageType == DeviceTypes.TYPE_HOST: retJSON["deviceType"]="Host" if imageType == DeviceTypes.TYPE_ROUTER: retJSON["deviceType"]="Router" retJSON["deviceID"]=containerName retJSON["sshIPAddress"]=SouthBoundActual.getContainerSSHIP(self.logger, containerName, bridgeName) self.logger.info("createContainer - CREATED CONTAINER. DETAILS:") self.logger.info(json.dumps(retJSON)) else: errCode = ErrorCodes.DEPLOYMENT_ERROR retJSON["error"] = ErrorCodes.getErrorDesc(errCode) return json.dumps(retJSON)
def update_delete_device(current, dev): topoid = current["TopoId"] hyphen = "-" details = current["Details"] for i in details: if i["Device_name"] == dev: dev_id = i["Device_id"] dev_type = i["Device_type"] break device = topoid + hyphen + dev_id #print("Deleting device", device, dev_type) sb_dev_type = DeviceTypes.getDeviceTypeFromStr(dev_type) #return ret = sbw.deleteDevice(device, sb_dev_type) ret = json.loads(ret) ret["deviceName"] = get_device_name_from_id(current, dev_id) uj.removeDevice(json.dumps(ret))
def create_devices(schema, adjlist, topoid): hyphen = "-" for i in adjlist: ConName = topoid + hyphen + i #print("CREATING CONTAINER " + ConName) for j in adjlist[i]: if dev_type(j) == 1: dev = j dev_ty = DeviceTypes.getDeviceTypeFromStr(dev) ret = sbw.createDevice(ConName, dev_ty) ret = make_changes_for_var_devices(schema, ret, i) #print("Creating device with ", ConName, dev_ty, ret) uj.constructDevice(ret)
def update_add_device(new, dev): dev_con = [] topoid = new["TopoId"] hyphen = "-" details = new["Details"] for i in details: if i["Device_name"] == dev: dev_id = i["Device_id"] dev_type = i["Device_type"] device = topoid + hyphen + dev_id #print("Adding device", device, dev_type) sb_dev_type = DeviceTypes.getDeviceTypeFromStr(dev_type) #return ret = sbw.createDevice(device, sb_dev_type) ret = make_changes_for_var_devices(new, ret, dev_id) uj.constructDevice(ret) con = {} cons = [] for j in i["Connections"]: cons.append(j["Connected_to"]) con[dev] = cons break dev_con.append(con)
def create_links(schema, count, topoid): ret = {} hyphen = "-" for i in count: ConName = topoid + hyphen + i #print(count[i]["Router"]) #print("conn name is ", ConName) for j in count[i]: if dev_type(j) == 1: continue cnt = count[i][j] while cnt > 0: end = topoid + hyphen + j if1 = topoid + i + hyphen + j + hyphen + "if" + str(cnt) if2 = topoid + j + hyphen + i + hyphen + "if" + str(cnt) if get_interface_name(schema, i, j) != "": if1 = get_interface_name(schema, i, j) if get_interface_name(schema, j, i) != "": if2 = get_interface_name(schema, j, i) #print("Connname is ", ConName ,"conn is ", end ,"if1 is", if1 ,"if2 is", if2) dev1 = get_device_type(count, i) dev_type1 = DeviceTypes.getDeviceTypeFromStr(dev1) dev2 = get_device_type(count, j) dev_type2 = DeviceTypes.getDeviceTypeFromStr(dev2) #print("device type is ", dev_type1, dev_type2) ret = sbw.createLink(ConName, if1, dev_type1, end, if2, dev_type2) ret = make_changes_for_var_links(schema, ret, i, ConName, j, end) #print(ret) uj.constructConnection(ret) ## Give IP-Address to links ## if count[i][j] == 1 or (count[i][j] > 1 and cnt == count[i][j]): subnet = get_subnet() #print("Subnet ", int(subnet), "for router", i[0]) if dev_type1 != DeviceTypes.TYPE_BRIDGE: ip1 = get_ip(subnet) if get_ip_address_from_if(schema, i, if1) != "": ip1 = get_ip_address_from_if(schema, i, if1) #print(ip1) ret = sbw.assignInterfaceIP(ConName, if1, ip1) ret = json.loads(ret) ret["deviceName"] = get_device_name_from_id(schema, i) #print(ret) uj.addDeviceInterfaceIP(json.dumps(ret)) if dev_type2 != DeviceTypes.TYPE_BRIDGE: ip2 = get_ip(subnet) if get_ip_address_from_if(schema, j, if2) != "": ip2 = get_ip_address_from_if(schema, j, if2) #print(ip2) ret = sbw.assignInterfaceIP(end, if2, ip2) ret = json.loads(ret) #print(ret) ret["deviceName"] = get_device_name_from_id(schema, j) uj.addDeviceInterfaceIP(json.dumps(ret)) #print("ip1 is ", ip1, "ip2 is", ip2) cnt -= 1 count[j][i] -= 1