Zack Williams | 6360656 | 2016-10-05 16:01:40 -0700 | [diff] [blame] | 1 | from xosresource import XOSResource |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 2 | from core.models import User, TenantAttribute, Service |
| 3 | from services.onos.models import ONOSApp, ONOSService |
| 4 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 5 | class XOSONOSApp(XOSResource): |
| 6 | provides = ["tosca.nodes.ONOSApp", "tosca.nodes.ONOSvBNGApp", "tosca.nodes.ONOSvOLTApp", "tosca.nodes.ONOSVTNApp", "tosca.nodes.ONOSvRouterApp"] |
| 7 | xos_model = ONOSApp |
| 8 | copyin_props = ["service_specific_id", "dependencies", "install_dependencies"] |
| 9 | |
| 10 | def get_xos_args(self, throw_exception=True): |
| 11 | args = super(XOSONOSApp, self).get_xos_args() |
| 12 | |
| 13 | # provider_service is mandatory and must be the ONOS Service |
| 14 | provider_name = self.get_requirement("tosca.relationships.TenantOfService", throw_exception=throw_exception) |
| 15 | if provider_name: |
| 16 | args["provider_service"] = self.get_xos_object(ONOSService, throw_exception=throw_exception, name=provider_name) |
| 17 | |
| 18 | # subscriber_service is optional and can be any service |
| 19 | subscriber_name = self.get_requirement("tosca.relationships.UsedByService", throw_exception=False) |
| 20 | if subscriber_name: |
| 21 | args["subscriber_service"] = self.get_xos_object(Service, throw_exception=throw_exception, name=subscriber_name) |
| 22 | |
| 23 | return args |
| 24 | |
| 25 | def get_existing_objs(self): |
| 26 | objs = ONOSApp.get_tenant_objects().all() |
| 27 | objs = [x for x in objs if x.name == self.obj_name] |
| 28 | return objs |
| 29 | |
| 30 | def set_tenant_attr(self, obj, prop_name, value): |
| 31 | value = self.try_intrinsic_function(value) |
| 32 | if value: |
| 33 | attrs = TenantAttribute.objects.filter(tenant=obj, name=prop_name) |
| 34 | if attrs: |
| 35 | attr = attrs[0] |
| 36 | if attr.value != value: |
| 37 | self.info("updating attribute %s" % prop_name) |
| 38 | attr.value = value |
| 39 | attr.save() |
| 40 | else: |
| 41 | self.info("adding attribute %s" % prop_name) |
| 42 | ta = TenantAttribute(tenant=obj, name=prop_name, value=value) |
| 43 | ta.save() |
| 44 | |
| 45 | def postprocess(self, obj): |
| 46 | props = self.nodetemplate.get_properties() |
| 47 | for (k,d) in props.items(): |
| 48 | v = d.value |
| 49 | if k.startswith("config_"): |
| 50 | self.set_tenant_attr(obj, k, v) |
| 51 | elif k.startswith("rest_") and (k!="rest_hostname") and (k!="rest_port"): |
| 52 | self.set_tenant_attr(obj, k, v) |
| 53 | elif k.startswith("component_config"): |
| 54 | self.set_tenant_attr(obj, k, v) |
| 55 | elif k == "autogenerate": |
| 56 | self.set_tenant_attr(obj, k, v) |
| 57 | |
| 58 | def can_delete(self, obj): |
| 59 | return super(XOSONOSApp, self).can_delete(obj) |