Matteo Scandolo | 35113f7 | 2017-08-08 13:05:25 -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 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 17 | import os |
| 18 | import sys |
Scott Baker | e95023b | 2017-02-23 09:50:04 -0800 | [diff] [blame] | 19 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 20 | from synchronizers.new_base.modelaccessor import * |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 21 | from xos.logger import Logger, logging |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 22 | |
| 23 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 24 | sys.path.insert(0, parentdir) |
| 25 | |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 26 | logger = Logger(level=logging.INFO) |
| 27 | |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 28 | class SyncExampleServiceInstance(SyncInstanceUsingAnsible): |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 29 | |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 30 | provides = [ExampleServiceInstance] |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 31 | |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 32 | observes = ExampleServiceInstance |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 33 | |
| 34 | requested_interval = 0 |
| 35 | |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 36 | template_name = "exampleserviceinstance_playbook.yaml" |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 37 | |
| 38 | service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key" |
| 39 | |
Scott Baker | 1bfb91b | 2017-03-20 11:00:23 -0700 | [diff] [blame] | 40 | watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')] |
Srikanth Vavilapalli | 2c41ab3 | 2016-11-17 03:14:30 +0000 | [diff] [blame] | 41 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 42 | def __init__(self, *args, **kwargs): |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 43 | super(SyncExampleServiceInstance, self).__init__(*args, **kwargs) |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 44 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 45 | def get_exampleservice(self, o): |
Scott Baker | 0982002 | 2017-07-18 11:29:59 -0700 | [diff] [blame] | 46 | if not o.owner: |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 47 | return None |
| 48 | |
Scott Baker | 0982002 | 2017-07-18 11:29:59 -0700 | [diff] [blame] | 49 | exampleservice = ExampleService.objects.filter(id=o.owner.id) |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 50 | |
| 51 | if not exampleservice: |
| 52 | return None |
| 53 | |
| 54 | return exampleservice[0] |
| 55 | |
| 56 | # Gets the attributes that are used by the Ansible template but are not |
| 57 | # part of the set of default attributes. |
| 58 | def get_extra_attributes(self, o): |
| 59 | fields = {} |
| 60 | fields['tenant_message'] = o.tenant_message |
| 61 | exampleservice = self.get_exampleservice(o) |
| 62 | fields['service_message'] = exampleservice.service_message |
Scott Baker | 7a916f7 | 2017-10-25 16:52:52 -0700 | [diff] [blame] | 63 | |
| 64 | if o.foreground_color: |
| 65 | fields["foreground_color"] = o.foreground_color.html_code |
| 66 | |
| 67 | if o.background_color: |
| 68 | fields["background_color"] = o.background_color.html_code |
| 69 | |
| 70 | images=[] |
| 71 | for image in o.embedded_images.all(): |
| 72 | images.append({"name": image.name, |
| 73 | "url": image.url}) |
| 74 | fields["images"] = images |
| 75 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 76 | return fields |
| 77 | |
Scott Baker | 9b1a3eb | 2017-01-23 14:46:27 -0800 | [diff] [blame] | 78 | def delete_record(self, port): |
| 79 | # Nothing needs to be done to delete an exampleservice; it goes away |
| 80 | # when the instance holding the exampleservice is deleted. |
| 81 | pass |
| 82 | |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 83 | def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info): |
| 84 | if not monitoring_agent_info.service: |
| 85 | logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info)) |
| 86 | return |
| 87 | |
| 88 | if not monitoring_agent_info.target_uri: |
| 89 | logger.info("handle watch notifications for service monitoring agent info...ignoring because target_uri attribute in monitoring agent info:%s is null" % (monitoring_agent_info)) |
| 90 | return |
| 91 | |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 92 | objs = ExampleServiceInstance.objects.all() |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 93 | for obj in objs: |
Scott Baker | 0982002 | 2017-07-18 11:29:59 -0700 | [diff] [blame] | 94 | if obj.owner.id != monitoring_agent_info.service.id: |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 95 | logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is not matching" % (monitoring_agent_info)) |
| 96 | return |
| 97 | |
| 98 | instance = self.get_instance(obj) |
| 99 | if not instance: |
| 100 | logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj))) |
| 101 | return |
| 102 | |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 103 | logger.info("handling watch notification for monitoring agent info:%s for ExampleServiceInstance object:%s" % (monitoring_agent_info, obj)) |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 104 | |
| 105 | #Run ansible playbook to update the routing table entries in the instance |
| 106 | fields = self.get_ansible_fields(instance) |
| 107 | fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_monitoring" |
| 108 | fields["target_uri"] = monitoring_agent_info.target_uri |
| 109 | |
| 110 | template_name = "monitoring_agent.yaml" |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame] | 111 | super(SyncExampleServiceInstance, self).run_playbook(obj, fields, template_name) |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 112 | pass |