Zack Williams | 6360656 | 2016-10-05 16:01:40 -0700 | [diff] [blame] | 1 | from xosresource import XOSResource |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 2 | from core.models import User, ServiceInstanceAttribute, Service, ServiceInstanceLink |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 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: |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 16 | args["owner"] = self.get_xos_object(ONOSService, throw_exception=throw_exception, name=provider_name) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 17 | |
| 18 | return args |
| 19 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 20 | def set_tenant_attr(self, obj, prop_name, value): |
| 21 | value = self.try_intrinsic_function(value) |
| 22 | if value: |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 23 | attrs = ServiceInstanceAttribute.objects.filter(service_instance=obj, name=prop_name) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 24 | if attrs: |
| 25 | attr = attrs[0] |
| 26 | if attr.value != value: |
| 27 | self.info("updating attribute %s" % prop_name) |
| 28 | attr.value = value |
| 29 | attr.save() |
| 30 | else: |
| 31 | self.info("adding attribute %s" % prop_name) |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 32 | ta = ServiceInstanceAttribute(service_instance=obj, name=prop_name, value=value) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 33 | ta.save() |
| 34 | |
| 35 | def postprocess(self, obj): |
| 36 | props = self.nodetemplate.get_properties() |
| 37 | for (k,d) in props.items(): |
| 38 | v = d.value |
| 39 | if k.startswith("config_"): |
| 40 | self.set_tenant_attr(obj, k, v) |
| 41 | elif k.startswith("rest_") and (k!="rest_hostname") and (k!="rest_port"): |
| 42 | self.set_tenant_attr(obj, k, v) |
| 43 | elif k.startswith("component_config"): |
| 44 | self.set_tenant_attr(obj, k, v) |
| 45 | elif k == "autogenerate": |
| 46 | self.set_tenant_attr(obj, k, v) |
| 47 | |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 48 | # subscriber_service is optional and can be any service |
| 49 | subscriber_name = self.get_requirement("tosca.relationships.UsedByService", throw_exception=False) |
| 50 | if subscriber_name: |
| 51 | sub_serv = self.get_xos_object(Service, throw_exception=True, name=subscriber_name) |
| 52 | existing_links = ServiceInstanceLink.objects.filter(provider_service_instance_id = obj.id, subscriber_service_id = sub_serv.id) |
| 53 | if not existing_links: |
| 54 | link = ServiceInstanceLink(provider_service_instance = obj, subscriber_service = sub_serv) |
| 55 | link.save() |
| 56 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 57 | def can_delete(self, obj): |
| 58 | return super(XOSONOSApp, self).can_delete(obj) |