blob: 3f48e05c52a6d7bdada419db53399ce1a70ca97d [file] [log] [blame]
JianHao90e65dc2017-10-31 18:01:07 +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 SyncVENBServiceInstance(SyncInstanceUsingAnsible):
25 provides = [VENBServiceInstance]
26
27 observes = VENBServiceInstance
28
29 requested_interval = 0
30
31 template_name = "venbserviceinstance_playbook.yaml"
32
33 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
34
35 def __init__(self, *args, **kwargs):
36 super(SyncVENBServiceInstance, self).__init__(*args, **kwargs)
37
Andy Bavierd1bfbd82017-11-14 17:26:18 -070038 def get_service(self, o):
39 if not o.owner:
40 return None
41
42 service = VENBService.objects.filter(id=o.owner.id)
43
44 if not service:
45 return None
46
47 return service[0]
48
JianHao90e65dc2017-10-31 18:01:07 +080049 def get_extra_attributes(self, o):
50
51 fields = {}
52 fields['flat_ip'] = self.get_ip_address('flat_network', VENBServiceInstance, 'get_venb_flat_ip')
Andy Bavierd1bfbd82017-11-14 17:26:18 -070053 service = self.get_service(o)
54 fields['login_user'] = service.login_user
55 fields['login_password'] = service.login_password
Andy Baviera42532a2017-11-15 14:45:13 -070056 fields['tas_ip'] = service.tas_ip
57 fields['ntp_ip'] = service.ntp_ip
JianHao90e65dc2017-10-31 18:01:07 +080058 return fields
59
60 def get_ip_address(self, network_name, service_instance, parameter):
61
62 try:
63 net_id = self.get_network_id(network_name)
64 ins_id = self.get_instance_id(service_instance)
65 ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip
66
67 except Exception:
68 ip_address = "error"
Sapan Bhatia10d0d3e2017-11-22 09:38:15 -050069 self.log.error("Could not fetch parameter", parameter = parameter, network_name = network_name)
JianHao90e65dc2017-10-31 18:01:07 +080070
71 return ip_address
72
73 # To get each network id
74 def get_network_id(self, network_name):
75 return Network.objects.get(name=network_name).id
76
77 # To get service_instance (assumption: there is a single instance for each service)
78 def get_instance_id(self, serviceinstance):
79 instances = serviceinstance.objects.all()
80 instance_id = instances[0].instance_id
81 return instance_id