Scott Baker | 8e6647a | 2016-06-20 17:16:20 -0700 | [diff] [blame] | 1 | from services.volt.models import AccessAgent, VOLTDevice, VOLTService, AgentPortMapping |
| 2 | from xosresource import XOSResource |
| 3 | |
| 4 | class XOSAccessAgent(XOSResource): |
| 5 | provides = "tosca.nodes.AccessAgent" |
| 6 | xos_model = AccessAgent |
| 7 | copyin_props = ["mac"] |
| 8 | |
| 9 | def get_xos_args(self, throw_exception=True): |
| 10 | args = super(XOSAccessAgent, 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 | return args |
| 17 | |
| 18 | def postprocess(self, obj): |
| 19 | # For convenient, allow the port mappings to be specified by a Tosca |
| 20 | # string with commas between lines. |
| 21 | # <port> <mac>, |
| 22 | # <port> <mac>, |
| 23 | # ... |
| 24 | # <port> <mac> |
| 25 | |
| 26 | port_mappings_str = self.get_property("port_mappings") |
| 27 | port_mappings = [] |
| 28 | if port_mappings_str: |
| 29 | lines = [x.strip() for x in port_mappings_str.split(",")] |
| 30 | for line in lines: |
| 31 | if not (" " in line): |
| 32 | raise "Malformed port mapping `%s`", line |
| 33 | (port, mac) = line.split(" ") |
| 34 | port=port.strip() |
| 35 | mac=mac.strip() |
| 36 | port_mappings.append( (port, mac) ) |
| 37 | |
| 38 | for apm in list(AgentPortMapping.objects.filter(access_agent=obj)): |
| 39 | if (apm.port, apm.mac) not in port_mappings: |
| 40 | print "Deleting AgentPortMapping '%s'" % apm |
| 41 | apm.delete() |
| 42 | |
| 43 | for port_mapping in port_mappings: |
| 44 | existing_objs = AgentPortMapping.objects.filter(access_agent=obj, port=port_mapping[0], mac=port_mapping[1]) |
| 45 | if not existing_objs: |
| 46 | apm = AgentPortMapping(access_agent=obj, port=port_mapping[0], mac=port_mapping[1]) |
| 47 | apm.save() |
| 48 | print "Created AgentPortMapping '%s'" % apm |
| 49 | |