blob: 3462960a61ca853488d6ccefc47832d7c9c0baaf [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
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 Bakerffac7182017-07-27 15:21:30 -070079 objs = ExampleServiceInstance.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
Scott Bakerffac7182017-07-27 15:21:30 -070090 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 +000091
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 Bakerffac7182017-07-27 15:21:30 -070098 super(SyncExampleServiceInstance, self).run_playbook(obj, fields, template_name)
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000099 pass