blob: 3b8e0d36475a2f5b5675bb6c8754494473af873c [file] [log] [blame]
Matteo Scandolo1c049b02018-01-18 11:32:46 -08001
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
17import os
18import sys
19from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
20from synchronizers.new_base.ansible_helper import run_template
Matteo Scandolo0a207b52018-01-29 13:39:43 -080021from synchronizers.new_base.modelaccessor import ProgranServiceInstance
Matteo Scandolo1c049b02018-01-18 11:32:46 -080022
23from xosconfig import Config
24from multistructlog import create_logger
25import json
26
Matteo Scandolo0a207b52018-01-29 13:39:43 -080027from helpers import ProgranHelpers
28
Matteo Scandolo1c049b02018-01-18 11:32:46 -080029log = create_logger(Config().get('logging'))
30
31parentdir = os.path.join(os.path.dirname(__file__), "..")
32sys.path.insert(0, parentdir)
33
34class SyncProgranServiceInstance(SyncInstanceUsingAnsible):
35 provides = [ProgranServiceInstance]
36
37 observes = ProgranServiceInstance
38
Matteo Scandolo1c049b02018-01-18 11:32:46 -080039 def skip_ansible_fields(self, o):
40 # FIXME This model does not have an instance, this is a workaroung to make it work,
41 # but it need to be cleaned up creating a general SyncUsingAnsible base class
42 return True
43
44 def get_handover_for_profile(self, o):
45 return {
46 "A3Hysteresis": o.handover.HysteresisA3,
47 "A3TriggerQuantity": o.handover.A3TriggerQuantity,
48 "A3offset": o.handover.A3offset,
49 "A5Hysteresis": o.handover.HysteresisA5,
50 "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp,
51 "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq,
52 "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp,
53 "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq,
54 "A5TriggerQuantity": o.handover.A5TriggerQuantity,
55 }
56
57 def get_progran_profile_field(self, o):
58
59 # basic information that we have in the service instance itself
60 profile = {
61 'AdmControl': o.AdmControl,
62 "DlSchedType": o.DlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -080063 "Start": o.start, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -080064 "UlSchedType": o.UlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -080065 "End": o.end, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -080066 "CellIndividualOffset": o.CellIndividualOffset,
67 "DlAllocRBRate": o.DlAllocRBRate,
68 "Name": o.name,
69 "UlAllocRBRate": o.UlAllocRBRate,
70 "Handover": self.get_handover_for_profile(o),
Matteo Scandolo6b607c82018-01-30 09:12:26 -080071 'mmeip': o.mmeip,
72 'mmeport': o.mmeport,
73 'DlWifiRate': o.DlWifiRate,
74 'DlUeAllocRbRate': o.DlUeAllocRbRate,
Matteo Scandolo1c049b02018-01-18 11:32:46 -080075 }
76 profile = json.dumps(profile)
77 return profile
78
Matteo Scandolo0a207b52018-01-29 13:39:43 -080079 def sync_record(self, o):
80 # NOTE overriding the default sync_record as we need to execute the playbook 2 times (profile and enodeb)
81
82 log.info("sync'ing profile", object=str(o), **o.tologdict())
83 onos = ProgranHelpers.get_onos_info_from_si(o)
84
85 # common field for both operations
86 base_field = {
Matteo Scandolo1c049b02018-01-18 11:32:46 -080087 'onos_url': onos['url'],
88 'onos_username': onos['username'],
89 'onos_password': onos['password'],
90 'onos_port': onos['port'],
Matteo Scandolo0a207b52018-01-29 13:39:43 -080091 }
92
93 # progran profile specific fields
94 profile_fields = {
Matteo Scandolo1c049b02018-01-18 11:32:46 -080095 'endpoint': 'profile',
96 'profile': self.get_progran_profile_field(o),
97 'method': 'POST'
98 }
Matteo Scandolo0a207b52018-01-29 13:39:43 -080099 profile_fields["ansible_tag"] = getattr(o, "ansible_tag", o.__class__.__name__ + "_" + str(o.id))
100 profile_fields.update(base_field)
101 self.run_playbook(o, profile_fields)
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800102
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800103 # progran enodeb specific fields
104 if o.enodeb:
105 log.info("adding profile to enodeb", object=str(o), **o.tologdict())
106 enodeb_fields = {
107 'profile': json.dumps({
108 "ProfileArray": [
109 o.name
110 ]
111 }),
112 'method': 'POST',
113 'endpoint': 'enodeb/%s/profile' % o.enodeb.enbId
114 }
115 enodeb_fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_enodeb_to_profile"
116 enodeb_fields.update(base_field)
117 self.run_playbook(o, enodeb_fields)
118 else:
119 log.warn("IMPLEMENT THE CALL TO REMOVE A PROFILE FROM ENODEB")
120
121
122 o.save()
123
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800124
125 # FIXME we need to override this as the default expect to ssh into a VM
126 def run_playbook(self, o, fields):
127 run_template("progran_curl.yaml", fields, object=o)
128
129 def delete_record(self, o):
130 log.info("deleting object", object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800131 onos = ProgranHelpers.get_onos_info_from_si(o)
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800132 fields = {
133 'onos_url': onos['url'],
134 'onos_username': onos['username'],
135 'onos_password': onos['password'],
136 'onos_port': onos['port'],
137 'endpoint': 'profile/%s' % o.name,
138 'profile': '',
139 'method': 'DELETE'
140 }
141 res = self.run_playbook(o, fields)