blob: 373fb5ebcefd9fc1323114afb970f529ae36c63c [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 Bakerffac7182017-07-27 15:21:30 -070028class SyncExampleServiceInstance(SyncInstanceUsingAnsible):
Scott Baker619de672016-06-20 12:49:38 -070029
Scott Bakerffac7182017-07-27 15:21:30 -070030 provides = [ExampleServiceInstance]
Scott Baker619de672016-06-20 12:49:38 -070031
Scott Bakerffac7182017-07-27 15:21:30 -070032 observes = ExampleServiceInstance
Scott Baker619de672016-06-20 12:49:38 -070033
34 requested_interval = 0
35
Scott Bakerffac7182017-07-27 15:21:30 -070036 template_name = "exampleserviceinstance_playbook.yaml"
Scott Baker619de672016-06-20 12:49:38 -070037
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):
Scott Bakerffac7182017-07-27 15:21:30 -070043 super(SyncExampleServiceInstance, self).__init__(*args, **kwargs)
Scott Baker619de672016-06-20 12:49:38 -070044
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
Scott Baker7a916f72017-10-25 16:52:52 -070063
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 Baker619de672016-06-20 12:49:38 -070076 return fields
77
Scott Baker9b1a3eb2017-01-23 14:46:27 -080078 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 Vavilapalli974c9ce2017-01-25 01:50:27 +000083 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 Bakerffac7182017-07-27 15:21:30 -070092 objs = ExampleServiceInstance.objects.all()
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000093 for obj in objs:
Scott Baker09820022017-07-18 11:29:59 -070094 if obj.owner.id != monitoring_agent_info.service.id:
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000095 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 Bakerffac7182017-07-27 15:21:30 -0700103 logger.info("handling watch notification for monitoring agent info:%s for ExampleServiceInstance object:%s" % (monitoring_agent_info, obj))
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +0000104
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 Bakerffac7182017-07-27 15:21:30 -0700111 super(SyncExampleServiceInstance, self).run_playbook(obj, fields, template_name)
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +0000112 pass