blob: 42cb0bf6918a897238dbc52b5dbc6259bff98d99 [file] [log] [blame]
Scott Baker526e2742015-10-16 17:47:52 -07001import os
2import pdb
3import sys
4import tempfile
5sys.path.append("/opt/tosca")
6from translator.toscalib.tosca_template import ToscaTemplate
7import pdb
8
Scott Baker2cd7c482015-10-20 21:18:45 -07009from core.models import User, TenantAttribute, Service
Scott Baker526e2742015-10-16 17:47:52 -070010from services.onos.models import ONOSApp, ONOSService
11
12from xosresource import XOSResource
13
14class XOSONOSApp(XOSResource):
Scott Bakerf6c8b462015-12-15 17:59:26 -080015 provides = ["tosca.nodes.ONOSApp", "tosca.nodes.ONOSvBNGApp", "tosca.nodes.ONOSvOLTApp", "tosca.nodes.ONOSVTNApp"]
Scott Baker526e2742015-10-16 17:47:52 -070016 xos_model = ONOSApp
Scott Baker17fecaa2015-10-19 16:24:02 -070017 copyin_props = ["service_specific_id", "dependencies"]
Scott Baker526e2742015-10-16 17:47:52 -070018
19 def get_xos_args(self, throw_exception=True):
20 args = super(XOSONOSApp, self).get_xos_args()
21
Scott Baker2cd7c482015-10-20 21:18:45 -070022 # provider_service is mandatory and must be the ONOS Service
Scott Baker526e2742015-10-16 17:47:52 -070023 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 Baker2cd7c482015-10-20 21:18:45 -070027 # 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 Baker526e2742015-10-16 17:47:52 -070032 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 Bakerc7272962015-12-01 16:03:04 -080046 self.info("updating attribute %s" % prop_name)
47 attr.value = value
48 attr.save()
Scott Baker526e2742015-10-16 17:47:52 -070049 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 Bakerc6628bb2015-12-03 21:14:39 -080060 elif k.startswith("rest_"):
61 self.set_tenant_attr(obj, k, v)
Scott Baker526e2742015-10-16 17:47:52 -070062
63 def can_delete(self, obj):
64 return super(XOSONOSApp, self).can_delete(obj)
65