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 AccessAgent, VOLTDevice, VOLTService, AgentPortMapping |
| 9 | from xosresource import XOSResource |
| 10 | |
| 11 | class XOSAccessAgent(XOSResource): |
| 12 | provides = "tosca.nodes.AccessAgent" |
| 13 | xos_model = AccessAgent |
| 14 | copyin_props = ["mac"] |
| 15 | |
| 16 | def get_xos_args(self, throw_exception=True): |
| 17 | args = super(XOSAccessAgent, 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 | return args |
| 24 | |
| 25 | def postprocess(self, obj): |
| 26 | # For convenient, allow the port mappings to be specified by a Tosca |
| 27 | # string with commas between lines. |
| 28 | # <port> <mac>, |
| 29 | # <port> <mac>, |
| 30 | # ... |
| 31 | # <port> <mac> |
| 32 | |
| 33 | port_mappings_str = self.get_property("port_mappings") |
| 34 | port_mappings = [] |
| 35 | if port_mappings_str: |
| 36 | lines = [x.strip() for x in port_mappings_str.split(",")] |
| 37 | for line in lines: |
| 38 | if not (" " in line): |
| 39 | raise "Malformed port mapping `%s`", line |
| 40 | (port, mac) = line.split(" ") |
| 41 | port=port.strip() |
| 42 | mac=mac.strip() |
| 43 | port_mappings.append( (port, mac) ) |
| 44 | |
| 45 | for apm in list(AgentPortMapping.objects.filter(access_agent=obj)): |
| 46 | if (apm.port, apm.mac) not in port_mappings: |
| 47 | print "Deleting AgentPortMapping '%s'" % apm |
| 48 | apm.delete() |
| 49 | |
| 50 | for port_mapping in port_mappings: |
| 51 | existing_objs = AgentPortMapping.objects.filter(access_agent=obj, port=port_mapping[0], mac=port_mapping[1]) |
| 52 | if not existing_objs: |
| 53 | apm = AgentPortMapping(access_agent=obj, port=port_mapping[0], mac=port_mapping[1]) |
| 54 | apm.save() |
| 55 | print "Created AgentPortMapping '%s'" % apm |
| 56 | |