blob: d37e4c347a4e33759567b00fea02df9d7480c5ea [file] [log] [blame]
Matteo Scandolo35113f72017-08-08 13:05:25 -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
Scott Baker619de672016-06-20 12:49:38 -070017import os
18import sys
Scott Bakere95023b2017-02-23 09:50:04 -080019from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
20from synchronizers.new_base.modelaccessor import *
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000021from xos.logger import Logger, logging
Scott Baker619de672016-06-20 12:49:38 -070022
23parentdir = os.path.join(os.path.dirname(__file__), "..")
24sys.path.insert(0, parentdir)
25
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000026logger = Logger(level=logging.INFO)
27
Scott Baker619de672016-06-20 12:49:38 -070028class SyncExampleTenant(SyncInstanceUsingAnsible):
29
30 provides = [ExampleTenant]
31
32 observes = ExampleTenant
33
34 requested_interval = 0
35
36 template_name = "exampletenant_playbook.yaml"
37
38 service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key"
39
Scott Baker1bfb91b2017-03-20 11:00:23 -070040 watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
Srikanth Vavilapalli2c41ab32016-11-17 03:14:30 +000041
Scott Baker619de672016-06-20 12:49:38 -070042 def __init__(self, *args, **kwargs):
43 super(SyncExampleTenant, self).__init__(*args, **kwargs)
44
Scott Baker619de672016-06-20 12:49:38 -070045 def get_exampleservice(self, o):
Scott Baker09820022017-07-18 11:29:59 -070046 if not o.owner:
Scott Baker619de672016-06-20 12:49:38 -070047 return None
48
Scott Baker09820022017-07-18 11:29:59 -070049 exampleservice = ExampleService.objects.filter(id=o.owner.id)
Scott Baker619de672016-06-20 12:49:38 -070050
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 Baker9b1a3eb2017-01-23 14:46:27 -080065 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 Vavilapalli974c9ce2017-01-25 01:50:27 +000070 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 Bakere95023b2017-02-23 09:50:04 -080079 objs = ExampleTenant.objects.all()
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000080 for obj in objs:
Scott Baker09820022017-07-18 11:29:59 -070081 if obj.owner.id != monitoring_agent_info.service.id:
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000082 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
90 logger.info("handling watch notification for monitoring agent info:%s for ExampleTenant object:%s" % (monitoring_agent_info, obj))
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"
98 super(SyncExampleTenant, self).run_playbook(obj, fields, template_name)
99 pass