blob: a65c717e8511500665e7dec018e725a1f6eccbc1 [file] [log] [blame]
Scott Baker7a327592016-06-20 17:34:06 -07001import os
2import pdb
3import sys
4import tempfile
5sys.path.append("/opt/tosca")
6from translator.toscalib.tosca_template import ToscaTemplate
7import pdb
8
9from core.models import User, TenantAttribute, Service
10from services.onos.models import ONOSApp, ONOSService
11
12from xosresource import XOSResource
13
14class XOSONOSApp(XOSResource):
15 provides = ["tosca.nodes.ONOSApp", "tosca.nodes.ONOSvBNGApp", "tosca.nodes.ONOSvOLTApp", "tosca.nodes.ONOSVTNApp", "tosca.nodes.ONOSvRouterApp"]
16 xos_model = ONOSApp
17 copyin_props = ["service_specific_id", "dependencies", "install_dependencies"]
18
19 def get_xos_args(self, throw_exception=True):
20 args = super(XOSONOSApp, self).get_xos_args()
21
22 # provider_service is mandatory and must be the ONOS Service
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
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
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.obj_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:
46 self.info("updating attribute %s" % prop_name)
47 attr.value = value
48 attr.save()
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)
60 elif k.startswith("rest_") and (k!="rest_hostname") and (k!="rest_port"):
61 self.set_tenant_attr(obj, k, v)
62 elif k.startswith("component_config"):
63 self.set_tenant_attr(obj, k, v)
64 elif k == "autogenerate":
65 self.set_tenant_attr(obj, k, v)
66
67 def can_delete(self, obj):
68 return super(XOSONOSApp, self).can_delete(obj)