blob: 005e3459468804f433966b0748c45324600480da [file] [log] [blame]
Zack Williams63606562016-10-05 16:01:40 -07001from xosresource import XOSResource
Scott Baker8d8f9922017-07-17 17:30:42 -07002from core.models import User, ServiceInstanceAttribute, Service, ServiceInstanceLink
Scott Baker7a327592016-06-20 17:34:06 -07003from services.onos.models import ONOSApp, ONOSService
4
Scott Baker7a327592016-06-20 17:34:06 -07005class 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 Baker8d8f9922017-07-17 17:30:42 -070016 args["owner"] = self.get_xos_object(ONOSService, throw_exception=throw_exception, name=provider_name)
Scott Baker7a327592016-06-20 17:34:06 -070017
18 return args
19
Scott Baker7a327592016-06-20 17:34:06 -070020 def set_tenant_attr(self, obj, prop_name, value):
21 value = self.try_intrinsic_function(value)
22 if value:
Scott Baker8d8f9922017-07-17 17:30:42 -070023 attrs = ServiceInstanceAttribute.objects.filter(service_instance=obj, name=prop_name)
Scott Baker7a327592016-06-20 17:34:06 -070024 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 Baker8d8f9922017-07-17 17:30:42 -070032 ta = ServiceInstanceAttribute(service_instance=obj, name=prop_name, value=value)
Scott Baker7a327592016-06-20 17:34:06 -070033 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 Baker8d8f9922017-07-17 17:30:42 -070048 # 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 Baker7a327592016-06-20 17:34:06 -070057 def can_delete(self, obj):
58 return super(XOSONOSApp, self).can_delete(obj)