blob: ef3d05e8385497ae6e3b277747329d1b93cffec3 [file] [log] [blame]
Andy Bavier03df22b2017-08-30 14:46:02 -07001
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
17from services.volt.models import VOLTDevice, VOLTService, AccessDevice, AccessAgent
18from xosresource import XOSResource
19
20class XOSVOLTDevice(XOSResource):
21 provides = "tosca.nodes.VOLTDevice"
22 xos_model = VOLTDevice
23 copyin_props = ["openflow_id", "driver"]
24
25 def get_xos_args(self, throw_exception=True):
26 args = super(XOSVOLTDevice, 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 agent_name = self.get_requirement("tosca.relationships.UsesAgent", throw_exception=throw_exception)
33 if agent_name:
34 args["access_agent"] = self.get_xos_object(AccessAgent, throw_exception=throw_exception, name=agent_name)
35
36 return args
37
38 def postprocess(self, obj):
39 access_devices_str = self.get_property("access_devices")
40 access_devices = []
41 if access_devices_str:
42 lines = [x.strip() for x in access_devices_str.split(",")]
43 for line in lines:
44 if not (" " in line):
45 raise "Malformed access device `%s`", line
46 (uplink, vlan) = line.split(" ")
47 uplink=int(uplink.strip())
48 vlan=int(vlan.strip())
49 access_devices.append( (uplink, vlan) )
50
51 for ad in list(AccessDevice.objects.filter(volt_device=obj)):
52 if (ad.uplink, ad.vlan) not in access_devices:
53 print "Deleting AccessDevice '%s'" % ad
54 ad.delete()
55
56 for access_device in access_devices:
57 existing_objs = AccessDevice.objects.filter(volt_device=obj, uplink=access_device[0], vlan=access_device[1])
58 if not existing_objs:
59 ad = AccessDevice(volt_device=obj, uplink=access_device[0], vlan=access_device[1])
60 ad.save()
61 print "Created AccessDevice '%s'" % ad