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