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