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 |
| 63 | return fields |
| 64 | |
Scott Baker | 9b1a3eb | 2017-01-23 14:46:27 -0800 | [diff] [blame] | 65 | def delete_record(self, port): |
| 66 | # Nothing needs to be done to delete an exampleservice; it goes away |
| 67 | # when the instance holding the exampleservice is deleted. |
| 68 | pass |
| 69 | |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 70 | def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info): |
| 71 | if not monitoring_agent_info.service: |
| 72 | logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info)) |
| 73 | return |
| 74 | |
| 75 | if not monitoring_agent_info.target_uri: |
| 76 | 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)) |
| 77 | return |
| 78 | |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame^] | 79 | objs = ExampleServiceInstance.objects.all() |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 80 | for obj in objs: |
Scott Baker | 0982002 | 2017-07-18 11:29:59 -0700 | [diff] [blame] | 81 | if obj.owner.id != monitoring_agent_info.service.id: |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 82 | 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)) |
| 83 | return |
| 84 | |
| 85 | instance = self.get_instance(obj) |
| 86 | if not instance: |
| 87 | logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj))) |
| 88 | return |
| 89 | |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame^] | 90 | 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] | 91 | |
| 92 | #Run ansible playbook to update the routing table entries in the instance |
| 93 | fields = self.get_ansible_fields(instance) |
| 94 | fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_monitoring" |
| 95 | fields["target_uri"] = monitoring_agent_info.target_uri |
| 96 | |
| 97 | template_name = "monitoring_agent.yaml" |
Scott Baker | ffac718 | 2017-07-27 15:21:30 -0700 | [diff] [blame^] | 98 | super(SyncExampleServiceInstance, self).run_playbook(obj, fields, template_name) |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 99 | pass |