Scott Baker | 526e274 | 2015-10-16 17:47:52 -0700 | [diff] [blame] | 1 | import os |
| 2 | import pdb |
| 3 | import sys |
| 4 | import tempfile |
| 5 | sys.path.append("/opt/tosca") |
| 6 | from translator.toscalib.tosca_template import ToscaTemplate |
| 7 | import pdb |
| 8 | |
Scott Baker | 2cd7c48 | 2015-10-20 21:18:45 -0700 | [diff] [blame] | 9 | from core.models import User, TenantAttribute, Service |
Scott Baker | 526e274 | 2015-10-16 17:47:52 -0700 | [diff] [blame] | 10 | from services.onos.models import ONOSApp, ONOSService |
| 11 | |
| 12 | from xosresource import XOSResource |
| 13 | |
| 14 | class XOSONOSApp(XOSResource): |
Scott Baker | f6c8b46 | 2015-12-15 17:59:26 -0800 | [diff] [blame] | 15 | provides = ["tosca.nodes.ONOSApp", "tosca.nodes.ONOSvBNGApp", "tosca.nodes.ONOSvOLTApp", "tosca.nodes.ONOSVTNApp"] |
Scott Baker | 526e274 | 2015-10-16 17:47:52 -0700 | [diff] [blame] | 16 | xos_model = ONOSApp |
Scott Baker | 17fecaa | 2015-10-19 16:24:02 -0700 | [diff] [blame] | 17 | copyin_props = ["service_specific_id", "dependencies"] |
Scott Baker | 526e274 | 2015-10-16 17:47:52 -0700 | [diff] [blame] | 18 | |
| 19 | def get_xos_args(self, throw_exception=True): |
| 20 | args = super(XOSONOSApp, self).get_xos_args() |
| 21 | |
Scott Baker | 2cd7c48 | 2015-10-20 21:18:45 -0700 | [diff] [blame] | 22 | # provider_service is mandatory and must be the ONOS Service |
Scott Baker | 526e274 | 2015-10-16 17:47:52 -0700 | [diff] [blame] | 23 | provider_name = self.get_requirement("tosca.relationships.TenantOfService", throw_exception=throw_exception) |
| 24 | if provider_name: |
| 25 | args["provider_service"] = self.get_xos_object(ONOSService, throw_exception=throw_exception, name=provider_name) |
| 26 | |
Scott Baker | 2cd7c48 | 2015-10-20 21:18:45 -0700 | [diff] [blame] | 27 | # subscriber_service is optional and can be any service |
| 28 | subscriber_name = self.get_requirement("tosca.relationships.UsedByService", throw_exception=False) |
| 29 | if subscriber_name: |
| 30 | args["subscriber_service"] = self.get_xos_object(Service, throw_exception=throw_exception, name=subscriber_name) |
| 31 | |
Scott Baker | 526e274 | 2015-10-16 17:47:52 -0700 | [diff] [blame] | 32 | return args |
| 33 | |
| 34 | def get_existing_objs(self): |
| 35 | objs = ONOSApp.get_tenant_objects().all() |
| 36 | objs = [x for x in objs if x.name == self.nodetemplate.name] |
| 37 | return objs |
| 38 | |
| 39 | def set_tenant_attr(self, obj, prop_name, value): |
| 40 | value = self.try_intrinsic_function(value) |
| 41 | if value: |
| 42 | attrs = TenantAttribute.objects.filter(tenant=obj, name=prop_name) |
| 43 | if attrs: |
| 44 | attr = attrs[0] |
| 45 | if attr.value != value: |
Scott Baker | c727296 | 2015-12-01 16:03:04 -0800 | [diff] [blame] | 46 | self.info("updating attribute %s" % prop_name) |
| 47 | attr.value = value |
| 48 | attr.save() |
Scott Baker | 526e274 | 2015-10-16 17:47:52 -0700 | [diff] [blame] | 49 | else: |
| 50 | self.info("adding attribute %s" % prop_name) |
| 51 | ta = TenantAttribute(tenant=obj, name=prop_name, value=value) |
| 52 | ta.save() |
| 53 | |
| 54 | def postprocess(self, obj): |
| 55 | props = self.nodetemplate.get_properties() |
| 56 | for (k,d) in props.items(): |
| 57 | v = d.value |
| 58 | if k.startswith("config_"): |
| 59 | self.set_tenant_attr(obj, k, v) |
Scott Baker | c6628bb | 2015-12-03 21:14:39 -0800 | [diff] [blame] | 60 | elif k.startswith("rest_"): |
| 61 | self.set_tenant_attr(obj, k, v) |
Scott Baker | 526e274 | 2015-10-16 17:47:52 -0700 | [diff] [blame] | 62 | |
| 63 | def can_delete(self, obj): |
| 64 | return super(XOSONOSApp, self).can_delete(obj) |
| 65 | |