blob: aec739b474b178099e208a34b74e10f65dbdcc2f [file] [log] [blame]
Woojoong Kimeb8a7182018-01-09 12:54:38 -08001# 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
18from synchronizers.new_base.modelaccessor import *
19from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
20
21parentdir = os.path.join(os.path.dirname(__file__), "..")
22sys.path.insert(0, parentdir)
23
24class SyncHSSDBServiceInstance(SyncInstanceUsingAnsible):
25
26 observes = HSSDBServiceInstance
27 template_name = "hssdbserviceinstance_playbook.yaml"
28 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
29
30 def __init__(self, *args, **kwargs):
31 super(SyncHSSDBServiceInstance, self).__init__(*args, **kwargs)
32
Woojoong Kim06e455a2018-01-18 13:51:46 -080033 def get_extra_attributes(self, o):
34 fields = {}
35
36 fields['db_ip'] = self.get_ip_address_from_peer_service_instance_instance('db_network', o, o, 'db_ip')
37
38 return fields
39
40 def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
41 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
42 return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter)
43
44 def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None):
45 try:
46 net_id = self.get_network_id(network_name)
47 ins_id = peer_si.leaf_model.instance_id
48 ip_address = Port.objects.get(
49 network_id=net_id, instance_id=ins_id).ip
50 except Exception:
51 self.log.error("Failed to fetch parameter",
52 parameter=parameter,
53 network_name=network_name)
54 self.defer_sync(o, "Waiting for parameters to become available")
55
56 return ip_address
57
58 def get_peer_serviceinstance_of_type(self, sitype, o):
59 prov_link_set = ServiceInstanceLink.objects.filter(
60 subscriber_service_instance_id=o.id)
61
62 try:
63 peer_service = next(
64 p.provider_service_instance for p in prov_link_set if p.provider_service_instance.leaf_model_name == sitype)
65 except StopIteration:
66 sub_link_set = ServiceInstanceLink.objects.filter(
67 provider_service_instance_id=o.id)
68 try:
69 peer_service = next(
70 s.subscriber_service_instance for s in sub_link_set if s.subscriber_service_instance.leaf_model_name == sitype)
71 except StopIteration:
72 self.log.error(
73 'Could not find service type in service graph', service_type=sitype, object=o)
74 raise ServiceGraphException(
75 "Synchronization failed due to incomplete service graph")
76
77 return peer_service
78
79 # To get each network id
80 def get_network_id(self, network_name):
81 return Network.objects.get(name=network_name).id
82
83 # To get service_instance (assumption: there is a single instance for each service)
84 def get_instance_id(self, serviceinstance):
85 instances = serviceinstance.objects.all()
86 instance_id = instances[0].instance_id
87 return instance_id
88
89 def has_instance(self, sitype, o):
90 try:
91 i = self.get_peer_serviceinstance_of_type(sitype, o)
92 except ServiceGraphException:
93 self.log.info("Missing in ServiceInstance graph",
94 serviceinstance=sitype)
95 return False
96
97 return i.leaf_model.instance_id
98
99
Woojoong Kimeb8a7182018-01-09 12:54:38 -0800100