blob: 853e783f01c754dcd362b045a5579733873d9d86 [file] [log] [blame]
Woojoong Kime30f6062017-10-05 19:38:56 -05001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import sys
17from django.db.models import Q, F
Woojoong Kime30f6062017-10-05 19:38:56 -050018from synchronizers.new_base.modelaccessor import *
Woojoong Kim033b2ce2017-10-10 10:09:06 -070019from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
Woojoong Kime30f6062017-10-05 19:38:56 -050020
21parentdir = os.path.join(os.path.dirname(__file__), "..")
22sys.path.insert(0, parentdir)
23
24class SyncVHSSTenant(SyncInstanceUsingAnsible):
Woojoong Kim033b2ce2017-10-10 10:09:06 -070025
Woojoong Kime30f6062017-10-05 19:38:56 -050026 provides = [VHSSTenant]
27
28 observes = VHSSTenant
29
30 requested_interval = 0
31
32 template_name = "vhsstenant_playbook.yaml"
33
34 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
35
Woojoong Kime30f6062017-10-05 19:38:56 -050036 def __init__(self, *args, **kwargs):
37 super(SyncVHSSTenant, self).__init__(*args, **kwargs)
38
Andy Bavier03905bc2018-01-17 15:49:00 -070039 def get_extra_attributes(self, o):
40 fields = {}
Woojoong Kime30f6062017-10-05 19:38:56 -050041
Andy Bavier03905bc2018-01-17 15:49:00 -070042 fields['db_ip'] = self.get_ip_address_from_peer_service_instance('db_network', 'HSSDBServiceInstance', o, 'db_ip')
43
44 return fields
45
46 def get_my_ip_address(self, o, network_name, parameter=None):
47 return self.get_ip_address_from_peer_service_instance_instance(network_name, o, o, parameter)
48
49 def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
50 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
51 return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter)
52
53 def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None):
54 try:
55 net_id = self.get_network_id(network_name)
56 ins_id = peer_si.leaf_model.instance_id
57 ip_address = Port.objects.get(
58 network_id=net_id, instance_id=ins_id).ip
59 except Exception:
60 self.log.error("Failed to fetch parameter",
61 parameter=parameter,
62 network_name=network_name)
63 self.defer_sync(o, "Waiting for parameters to become available")
64
65 return ip_address
66
67 def get_peer_serviceinstance_of_type(self, sitype, o):
68 prov_link_set = ServiceInstanceLink.objects.filter(
69 subscriber_service_instance_id=o.id)
70
71 try:
72 peer_service = next(
73 p.provider_service_instance for p in prov_link_set if p.provider_service_instance.leaf_model_name == sitype)
74 except StopIteration:
75 sub_link_set = ServiceInstanceLink.objects.filter(
76 provider_service_instance_id=o.id)
77 try:
78 peer_service = next(
79 s.subscriber_service_instance for s in sub_link_set if s.subscriber_service_instance.leaf_model_name == sitype)
80 except StopIteration:
81 self.log.error(
82 'Could not find service type in service graph', service_type=sitype, object=o)
83 raise ServiceGraphException(
84 "Synchronization failed due to incomplete service graph")
85
86 return peer_service
87
88 # To get each network id
89 def get_network_id(self, network_name):
90 return Network.objects.get(name=network_name).id