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 | d52da97 | 2018-01-31 14:37:43 -0800 | [diff] [blame] | 40 | # NOTE I need to keep track of the relations between profile and enodebs to remove them |
| 41 | # it contains: <profile-id>:<enodeb_id> |
| 42 | profile_enodebs = {} |
| 43 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 44 | def skip_ansible_fields(self, o): |
| 45 | # FIXME This model does not have an instance, this is a workaroung to make it work, |
| 46 | # but it need to be cleaned up creating a general SyncUsingAnsible base class |
| 47 | return True |
| 48 | |
| 49 | def get_handover_for_profile(self, o): |
| 50 | return { |
| 51 | "A3Hysteresis": o.handover.HysteresisA3, |
| 52 | "A3TriggerQuantity": o.handover.A3TriggerQuantity, |
| 53 | "A3offset": o.handover.A3offset, |
| 54 | "A5Hysteresis": o.handover.HysteresisA5, |
| 55 | "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp, |
| 56 | "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq, |
| 57 | "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp, |
| 58 | "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq, |
| 59 | "A5TriggerQuantity": o.handover.A5TriggerQuantity, |
| 60 | } |
| 61 | |
| 62 | def get_progran_profile_field(self, o): |
| 63 | |
| 64 | # basic information that we have in the service instance itself |
| 65 | profile = { |
| 66 | 'AdmControl': o.AdmControl, |
| 67 | "DlSchedType": o.DlSchedType, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 68 | "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] | 69 | "UlSchedType": o.UlSchedType, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 70 | "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] | 71 | "CellIndividualOffset": o.CellIndividualOffset, |
| 72 | "DlAllocRBRate": o.DlAllocRBRate, |
| 73 | "Name": o.name, |
| 74 | "UlAllocRBRate": o.UlAllocRBRate, |
| 75 | "Handover": self.get_handover_for_profile(o), |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 76 | 'mmeip': o.mmeip, |
| 77 | 'mmeport': o.mmeport, |
| 78 | 'DlWifiRate': o.DlWifiRate, |
| 79 | 'DlUeAllocRbRate': o.DlUeAllocRbRate, |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 80 | } |
| 81 | profile = json.dumps(profile) |
| 82 | return profile |
| 83 | |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 84 | def sync_record(self, o): |
| 85 | # NOTE overriding the default sync_record as we need to execute the playbook 2 times (profile and enodeb) |
| 86 | |
| 87 | log.info("sync'ing profile", object=str(o), **o.tologdict()) |
| 88 | onos = ProgranHelpers.get_onos_info_from_si(o) |
| 89 | |
| 90 | # common field for both operations |
| 91 | base_field = { |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 92 | 'onos_url': onos['url'], |
| 93 | 'onos_username': onos['username'], |
| 94 | 'onos_password': onos['password'], |
| 95 | 'onos_port': onos['port'], |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | # progran profile specific fields |
| 99 | profile_fields = { |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 100 | 'endpoint': 'profile', |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 101 | 'body': self.get_progran_profile_field(o), |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 102 | 'method': 'POST' |
| 103 | } |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 104 | profile_fields["ansible_tag"] = getattr(o, "ansible_tag", o.__class__.__name__ + "_" + str(o.id)) |
| 105 | profile_fields.update(base_field) |
| 106 | self.run_playbook(o, profile_fields) |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 107 | |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 108 | # progran enodeb specific fields |
| 109 | if o.enodeb: |
Matteo Scandolo | d52da97 | 2018-01-31 14:37:43 -0800 | [diff] [blame] | 110 | log.info("adding profile %s to enodeb %s" % (o.id, o.enodeb.enbId), object=str(o), **o.tologdict()) |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 111 | enodeb_fields = { |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 112 | 'body': json.dumps({ |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 113 | "ProfileArray": [ |
| 114 | o.name |
| 115 | ] |
| 116 | }), |
| 117 | 'method': 'POST', |
| 118 | 'endpoint': 'enodeb/%s/profile' % o.enodeb.enbId |
| 119 | } |
| 120 | enodeb_fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_enodeb_to_profile" |
| 121 | enodeb_fields.update(base_field) |
| 122 | self.run_playbook(o, enodeb_fields) |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 123 | |
Matteo Scandolo | d52da97 | 2018-01-31 14:37:43 -0800 | [diff] [blame] | 124 | # update local state |
| 125 | self.profile_enodebs[o.id] = o.enodeb.enbId |
| 126 | else: |
| 127 | try: |
| 128 | enbid = self.profile_enodebs[o.id] |
| 129 | except KeyError: |
| 130 | enbid = None |
| 131 | if enbid: |
| 132 | print enbid |
| 133 | log.info("removing profile %s from enodeb %s" % (o.id, self.profile_enodebs[o.id]), object=str(o), **o.tologdict()) |
| 134 | enodeb_fields = { |
| 135 | 'body': '', |
| 136 | 'method': 'DELETE', |
| 137 | 'endpoint': 'enodeb/%s/profile/%s' % (enbid, o.name) |
| 138 | } |
| 139 | enodeb_fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_rm_enodeb_from_profile" |
| 140 | enodeb_fields.update(base_field) |
| 141 | self.run_playbook(o, enodeb_fields) |
| 142 | del self.profile_enodebs[o.id] |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 143 | |
| 144 | o.save() |
| 145 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 146 | |
| 147 | # FIXME we need to override this as the default expect to ssh into a VM |
| 148 | def run_playbook(self, o, fields): |
| 149 | run_template("progran_curl.yaml", fields, object=o) |
| 150 | |
| 151 | def delete_record(self, o): |
| 152 | log.info("deleting object", object=str(o), **o.tologdict()) |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 153 | onos = ProgranHelpers.get_onos_info_from_si(o) |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 154 | fields = { |
| 155 | 'onos_url': onos['url'], |
| 156 | 'onos_username': onos['username'], |
| 157 | 'onos_password': onos['password'], |
| 158 | 'onos_port': onos['port'], |
| 159 | 'endpoint': 'profile/%s' % o.name, |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 160 | 'body': '', |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 161 | 'method': 'DELETE' |
| 162 | } |
| 163 | res = self.run_playbook(o, fields) |