Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | from services.volt.models import AccessAgent, VOLTDevice, VOLTService, AgentPortMapping |
| 18 | from xosresource import XOSResource |
| 19 | |
| 20 | class XOSAccessAgent(XOSResource): |
| 21 | provides = "tosca.nodes.AccessAgent" |
| 22 | xos_model = AccessAgent |
| 23 | copyin_props = ["mac"] |
| 24 | |
| 25 | def get_xos_args(self, throw_exception=True): |
| 26 | args = super(XOSAccessAgent, self).get_xos_args() |
| 27 | |
| 28 | volt_service_name = self.get_requirement("tosca.relationships.MemberOfService", throw_exception=throw_exception) |
| 29 | if volt_service_name: |
| 30 | args["volt_service"] = self.get_xos_object(VOLTService, throw_exception=throw_exception, name=volt_service_name) |
| 31 | |
| 32 | return args |
| 33 | |
| 34 | def postprocess(self, obj): |
| 35 | # For convenient, allow the port mappings to be specified by a Tosca |
| 36 | # string with commas between lines. |
| 37 | # <port> <mac>, |
| 38 | # <port> <mac>, |
| 39 | # ... |
| 40 | # <port> <mac> |
| 41 | |
| 42 | port_mappings_str = self.get_property("port_mappings") |
| 43 | port_mappings = [] |
| 44 | if port_mappings_str: |
| 45 | lines = [x.strip() for x in port_mappings_str.split(",")] |
| 46 | for line in lines: |
| 47 | if not (" " in line): |
| 48 | raise "Malformed port mapping `%s`", line |
| 49 | (port, mac) = line.split(" ") |
| 50 | port=port.strip() |
| 51 | mac=mac.strip() |
| 52 | port_mappings.append( (port, mac) ) |
| 53 | |
| 54 | for apm in list(AgentPortMapping.objects.filter(access_agent=obj)): |
| 55 | if (apm.port, apm.mac) not in port_mappings: |
| 56 | print "Deleting AgentPortMapping '%s'" % apm |
| 57 | apm.delete() |
| 58 | |
| 59 | for port_mapping in port_mappings: |
| 60 | existing_objs = AgentPortMapping.objects.filter(access_agent=obj, port=port_mapping[0], mac=port_mapping[1]) |
| 61 | if not existing_objs: |
| 62 | apm = AgentPortMapping(access_agent=obj, port=port_mapping[0], mac=port_mapping[1]) |
| 63 | apm.save() |
| 64 | print "Created AgentPortMapping '%s'" % apm |
| 65 | |