Scott Baker | 8e6647a | 2016-06-20 17:16:20 -0700 | [diff] [blame] | 1 | from services.volt.models import VOLTDevice, VOLTService, AccessDevice, AccessAgent |
| 2 | from xosresource import XOSResource |
| 3 | |
| 4 | class XOSVOLTDevice(XOSResource): |
| 5 | provides = "tosca.nodes.VOLTDevice" |
| 6 | xos_model = VOLTDevice |
| 7 | copyin_props = ["openflow_id", "driver"] |
| 8 | |
| 9 | def get_xos_args(self, throw_exception=True): |
| 10 | args = super(XOSVOLTDevice, self).get_xos_args() |
| 11 | |
| 12 | volt_service_name = self.get_requirement("tosca.relationships.MemberOfService", throw_exception=throw_exception) |
| 13 | if volt_service_name: |
| 14 | args["volt_service"] = self.get_xos_object(VOLTService, throw_exception=throw_exception, name=volt_service_name) |
| 15 | |
| 16 | agent_name = self.get_requirement("tosca.relationships.UsesAgent", throw_exception=throw_exception) |
| 17 | if agent_name: |
| 18 | args["access_agent"] = self.get_xos_object(AccessAgent, throw_exception=throw_exception, name=agent_name) |
| 19 | |
| 20 | return args |
| 21 | |
| 22 | def postprocess(self, obj): |
| 23 | access_devices_str = self.get_property("access_devices") |
| 24 | access_devices = [] |
| 25 | if access_devices_str: |
| 26 | lines = [x.strip() for x in access_devices_str.split(",")] |
| 27 | for line in lines: |
| 28 | if not (" " in line): |
| 29 | raise "Malformed access device `%s`", line |
| 30 | (uplink, vlan) = line.split(" ") |
| 31 | uplink=int(uplink.strip()) |
| 32 | vlan=int(vlan.strip()) |
| 33 | access_devices.append( (uplink, vlan) ) |
| 34 | |
| 35 | for ad in list(AccessDevice.objects.filter(volt_device=obj)): |
| 36 | if (ad.uplink, ad.vlan) not in access_devices: |
| 37 | print "Deleting AccessDevice '%s'" % ad |
| 38 | ad.delete() |
| 39 | |
| 40 | for access_device in access_devices: |
| 41 | existing_objs = AccessDevice.objects.filter(volt_device=obj, uplink=access_device[0], vlan=access_device[1]) |
| 42 | if not existing_objs: |
| 43 | ad = AccessDevice(volt_device=obj, uplink=access_device[0], vlan=access_device[1]) |
| 44 | ad.save() |
| 45 | print "Created AccessDevice '%s'" % ad |