blob: a65c717e8511500665e7dec018e725a1f6eccbc1 [file] [log] [blame]
Scott Baker0e5be0a2015-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 Baker5e5c0e32015-10-20 21:18:45 -07009from core.models import User, TenantAttribute, Service
Scott Baker0e5be0a2015-10-16 17:47:52 -070010from services.onos.models import ONOSApp, ONOSService
11
12from xosresource import XOSResource
13
14class XOSONOSApp(XOSResource):
Andy Baviercb09e2e2016-05-16 11:12:01 -040015 provides = ["tosca.nodes.ONOSApp", "tosca.nodes.ONOSvBNGApp", "tosca.nodes.ONOSvOLTApp", "tosca.nodes.ONOSVTNApp", "tosca.nodes.ONOSvRouterApp"]
Scott Baker0e5be0a2015-10-16 17:47:52 -070016 xos_model = ONOSApp
Srikanth Vavilapalli6ff55982016-01-22 14:35:50 -050017 copyin_props = ["service_specific_id", "dependencies", "install_dependencies"]
Scott Baker0e5be0a2015-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 Baker5e5c0e32015-10-20 21:18:45 -070022 # provider_service is mandatory and must be the ONOS Service
Scott Baker0e5be0a2015-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 Baker5e5c0e32015-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 Baker0e5be0a2015-10-16 17:47:52 -070032 return args
33
34 def get_existing_objs(self):
35 objs = ONOSApp.get_tenant_objects().all()
Scott Baker32596412016-04-25 14:55:22 -070036 objs = [x for x in objs if x.name == self.obj_name]
Scott Baker0e5be0a2015-10-16 17:47:52 -070037 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 Baker9e956282015-12-01 16:03:04 -080046 self.info("updating attribute %s" % prop_name)
47 attr.value = value
48 attr.save()
Scott Baker0e5be0a2015-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 Bakerf510bf82016-02-10 14:05:01 -080060 elif k.startswith("rest_") and (k!="rest_hostname") and (k!="rest_port"):
Scott Bakera758d822015-12-03 21:14:39 -080061 self.set_tenant_attr(obj, k, v)
Srikanth Vavilapalli6ff55982016-01-22 14:35:50 -050062 elif k.startswith("component_config"):
63 self.set_tenant_attr(obj, k, v)
Andy Bavier7532cef2016-04-14 15:13:12 -040064 elif k == "autogenerate":
65 self.set_tenant_attr(obj, k, v)
Scott Baker0e5be0a2015-10-16 17:47:52 -070066
67 def can_delete(self, obj):
68 return super(XOSONOSApp, self).can_delete(obj)