blob: 9665b8597dbb05fd1c984ed754ec5b4a446e9694 [file] [log] [blame]
Scott Baker8e6647a2016-06-20 17:16:20 -07001import os
2import pdb
3import sys
4import tempfile
5sys.path.append("/opt/tosca")
6from translator.toscalib.tosca_template import ToscaTemplate
7
8from services.volt.models import VOLTDevice, VOLTService, AccessDevice, AccessAgent
9from xosresource import XOSResource
10
11class 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