Matteo Scandolo | 7781b5b | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 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 Williams | 6360656 | 2016-10-05 16:01:40 -0700 | [diff] [blame] | 17 | from xosresource import XOSResource |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 18 | from core.models import User, ServiceInstanceAttribute, Service, ServiceInstanceLink |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 19 | from services.onos.models import ONOSApp, ONOSService |
| 20 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 21 | class 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 Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 32 | args["owner"] = self.get_xos_object(ONOSService, throw_exception=throw_exception, name=provider_name) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 33 | |
| 34 | return args |
| 35 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 36 | def set_tenant_attr(self, obj, prop_name, value): |
| 37 | value = self.try_intrinsic_function(value) |
| 38 | if value: |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 39 | attrs = ServiceInstanceAttribute.objects.filter(service_instance=obj, name=prop_name) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 40 | 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 Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 48 | ta = ServiceInstanceAttribute(service_instance=obj, name=prop_name, value=value) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 49 | 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 Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 64 | # 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 Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 73 | def can_delete(self, obj): |
| 74 | return super(XOSONOSApp, self).can_delete(obj) |