blob: d1c587a306a10f118662663a1835dea9309d799c [file] [log] [blame]
Matteo Scandolo7781b5b2017-08-08 13:05:26 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Zack Williams63606562016-10-05 16:01:40 -070017from xosresource import XOSResource
Scott Baker8d8f9922017-07-17 17:30:42 -070018from core.models import User, ServiceInstanceAttribute, Service, ServiceInstanceLink
Scott Baker7a327592016-06-20 17:34:06 -070019from services.onos.models import ONOSApp, ONOSService
20
Scott Baker7a327592016-06-20 17:34:06 -070021class XOSONOSApp(XOSResource):
22 provides = ["tosca.nodes.ONOSApp", "tosca.nodes.ONOSvBNGApp", "tosca.nodes.ONOSvOLTApp", "tosca.nodes.ONOSVTNApp", "tosca.nodes.ONOSvRouterApp"]
23 xos_model = ONOSApp
24 copyin_props = ["service_specific_id", "dependencies", "install_dependencies"]
25
26 def get_xos_args(self, throw_exception=True):
27 args = super(XOSONOSApp, self).get_xos_args()
28
29 # provider_service is mandatory and must be the ONOS Service
30 provider_name = self.get_requirement("tosca.relationships.TenantOfService", throw_exception=throw_exception)
31 if provider_name:
Scott Baker8d8f9922017-07-17 17:30:42 -070032 args["owner"] = self.get_xos_object(ONOSService, throw_exception=throw_exception, name=provider_name)
Scott Baker7a327592016-06-20 17:34:06 -070033
34 return args
35
Scott Baker7a327592016-06-20 17:34:06 -070036 def set_tenant_attr(self, obj, prop_name, value):
37 value = self.try_intrinsic_function(value)
38 if value:
Scott Baker8d8f9922017-07-17 17:30:42 -070039 attrs = ServiceInstanceAttribute.objects.filter(service_instance=obj, name=prop_name)
Scott Baker7a327592016-06-20 17:34:06 -070040 if attrs:
41 attr = attrs[0]
42 if attr.value != value:
43 self.info("updating attribute %s" % prop_name)
44 attr.value = value
45 attr.save()
46 else:
47 self.info("adding attribute %s" % prop_name)
Scott Baker8d8f9922017-07-17 17:30:42 -070048 ta = ServiceInstanceAttribute(service_instance=obj, name=prop_name, value=value)
Scott Baker7a327592016-06-20 17:34:06 -070049 ta.save()
50
51 def postprocess(self, obj):
52 props = self.nodetemplate.get_properties()
53 for (k,d) in props.items():
54 v = d.value
55 if k.startswith("config_"):
56 self.set_tenant_attr(obj, k, v)
57 elif k.startswith("rest_") and (k!="rest_hostname") and (k!="rest_port"):
58 self.set_tenant_attr(obj, k, v)
59 elif k.startswith("component_config"):
60 self.set_tenant_attr(obj, k, v)
61 elif k == "autogenerate":
62 self.set_tenant_attr(obj, k, v)
63
Scott Baker8d8f9922017-07-17 17:30:42 -070064 # subscriber_service is optional and can be any service
65 subscriber_name = self.get_requirement("tosca.relationships.UsedByService", throw_exception=False)
66 if subscriber_name:
67 sub_serv = self.get_xos_object(Service, throw_exception=True, name=subscriber_name)
68 existing_links = ServiceInstanceLink.objects.filter(provider_service_instance_id = obj.id, subscriber_service_id = sub_serv.id)
69 if not existing_links:
70 link = ServiceInstanceLink(provider_service_instance = obj, subscriber_service = sub_serv)
71 link.save()
72
Scott Baker7a327592016-06-20 17:34:06 -070073 def can_delete(self, obj):
74 return super(XOSONOSApp, self).can_delete(obj)