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 |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 19 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 20 | from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB, Handover |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 21 | |
| 22 | from xosconfig import Config |
| 23 | from multistructlog import create_logger |
| 24 | import json |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 25 | import requests |
| 26 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 27 | import time |
| 28 | import datetime |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 29 | |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 30 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 31 | log = create_logger(Config().get('logging')) |
| 32 | |
| 33 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 34 | sys.path.insert(0, parentdir) |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 35 | sys.path.insert(0, os.path.dirname(__file__)) |
| 36 | from helpers import ProgranHelpers |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 37 | |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 38 | class SyncProgranServiceInstance(SyncStep): |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 39 | provides = [ProgranServiceInstance] |
| 40 | |
| 41 | observes = ProgranServiceInstance |
| 42 | |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 43 | # Poll every 5 loops of self.call |
| 44 | poll = 0 |
| 45 | |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 46 | def sync_record(self, o): |
| 47 | onos = ProgranHelpers.get_progran_onos_info() |
| 48 | |
| 49 | log.info("sync'ing profile", object=str(o), **o.tologdict()) |
| 50 | |
| 51 | profile_url = "http://%s:%s/onos/progran/profile/" % (onos['url'], onos['port']) |
| 52 | data = self.get_progran_profile_field(o) |
| 53 | |
| 54 | r = requests.post(profile_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password'])) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 55 | |
| 56 | ProgranHelpers.get_progran_rest_errors(r) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 57 | log.info("Profile synchronized", response=r.json()) |
| 58 | |
| 59 | log.info("sync'ing enodeb", object=str(o), **o.tologdict()) |
| 60 | if o.enodeb_id: |
| 61 | log.info("adding profile %s to enodeb %s" % (o.id, o.enodeb.enbId), object=str(o), **o.tologdict()) |
| 62 | enodeb_url = "http://%s:%s/onos/progran/enodeb/%s/profile" % (onos['url'], onos['port'], o.enodeb.enbId) |
| 63 | data = { |
| 64 | "ProfileArray": [ |
| 65 | o.name |
| 66 | ] |
| 67 | } |
| 68 | r = requests.post(enodeb_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password'])) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 69 | ProgranHelpers.get_progran_rest_errors(r) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 70 | o.active_enodeb_id = o.enodeb_id # storing the value to know when it will be deleted |
| 71 | log.info("EnodeB synchronized", response=r.json()) |
| 72 | elif o.active_enodeb_id: |
| 73 | enb_id = ENodeB.objects.get(id=o.active_enodeb_id).enbId |
| 74 | log.info("removing profile %s from enodeb %s" % (o.name, o.active_enodeb_id), object=str(o), **o.tologdict()) |
| 75 | enodeb_url = "http://%s:%s/onos/progran/enodeb/%s/profile/%s" % (onos['url'], onos['port'], enb_id, o.name) |
| 76 | r = requests.delete(enodeb_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 77 | ProgranHelpers.get_progran_rest_errors(r) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 78 | o.active_enodeb_id = 0 # removing the value because it has been deleted |
| 79 | log.info("EnodeB synchronized", response=r.json()) |
| 80 | |
| 81 | o.save() |
| 82 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 83 | def get_handover_for_profile(self, o): |
| 84 | return { |
| 85 | "A3Hysteresis": o.handover.HysteresisA3, |
| 86 | "A3TriggerQuantity": o.handover.A3TriggerQuantity, |
| 87 | "A3offset": o.handover.A3offset, |
| 88 | "A5Hysteresis": o.handover.HysteresisA5, |
| 89 | "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp, |
| 90 | "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq, |
| 91 | "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp, |
| 92 | "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq, |
| 93 | "A5TriggerQuantity": o.handover.A5TriggerQuantity, |
| 94 | } |
| 95 | |
| 96 | def get_progran_profile_field(self, o): |
| 97 | |
| 98 | # basic information that we have in the service instance itself |
| 99 | profile = { |
| 100 | 'AdmControl': o.AdmControl, |
| 101 | "DlSchedType": o.DlSchedType, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 102 | "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] | 103 | "UlSchedType": o.UlSchedType, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 104 | "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] | 105 | "CellIndividualOffset": o.CellIndividualOffset, |
| 106 | "DlAllocRBRate": o.DlAllocRBRate, |
| 107 | "Name": o.name, |
| 108 | "UlAllocRBRate": o.UlAllocRBRate, |
| 109 | "Handover": self.get_handover_for_profile(o), |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 110 | 'mmeip': o.mmeip, |
| 111 | 'mmeport': o.mmeport, |
| 112 | 'DlWifiRate': o.DlWifiRate, |
| 113 | 'DlUeAllocRbRate': o.DlUeAllocRbRate, |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 114 | } |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 115 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 116 | return profile |
| 117 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 118 | def delete_record(self, o): |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 119 | log.info("deleting profile", object=str(o), **o.tologdict()) |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 120 | onos = ProgranHelpers.get_onos_info_from_si(o) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 121 | profile_url = "http://%s:%s/onos/progran/profile/%s" % (onos['url'], onos['port'], o.name) |
| 122 | r = requests.delete(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 123 | o.active_enodeb_id = 0 # removing the value because it has been deleted |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 124 | log.info("Profile synchronized", response=r.json()) |
| 125 | |
| 126 | def fetch_pending(self, deleted): |
| 127 | # self.read_profiles_from_progran() |
| 128 | return super(SyncProgranServiceInstance, self).fetch_pending(deleted) |
| 129 | |
| 130 | @staticmethod |
| 131 | def date_to_time(d): |
| 132 | if len(d) == 0: |
| 133 | return 0 |
| 134 | return time.mktime(datetime.datetime.strptime(d, "%d.%m.%Y %H:%S").timetuple()) |
| 135 | |
| 136 | @staticmethod |
| 137 | def update_fields(model, dict, mapping={}, transformations={}): |
| 138 | dict = SyncProgranServiceInstance.convert_keys(dict, mapping, transformations) |
| 139 | for k, v in dict.iteritems(): |
| 140 | if hasattr(model, k): |
| 141 | setattr(model, k, v) |
| 142 | else: |
| 143 | log.warn("%s does not have a '%s' property, not updating it" % (model.model_name, k)) |
| 144 | return model |
| 145 | |
| 146 | @staticmethod |
| 147 | def convert_keys(dict, mapping={}, transformations={}): |
| 148 | for k, v in dict.iteritems(): |
| 149 | if k in mapping: |
| 150 | # apply custom transformations to the data |
| 151 | if k in transformations: |
| 152 | dict[k] = transformations[k](v) |
| 153 | |
| 154 | # NOTE we may have different names that the field in the dict |
| 155 | dict[mapping[k]] = dict[k] |
| 156 | del dict[k] |
| 157 | return dict |
| 158 | |
| 159 | |
| 160 | def my_call(self, failed=[], deletion=False): |
| 161 | """ |
| 162 | Read profile from progran and save them in xos |
| 163 | """ |
| 164 | if self.poll < 5: |
| 165 | self.poll = self.poll + 1 |
| 166 | else: |
| 167 | self.poll = 0 |
| 168 | onos = ProgranHelpers.get_progran_onos_info() |
| 169 | profile_url = "http://%s:%s/onos/progran/profile/" % (onos['url'], onos['port']) |
| 170 | r = requests.get(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 171 | res = r.json()['ProfileArray'] |
| 172 | |
| 173 | # remove default profiles |
| 174 | res = [p for p in res if "Default" not in p['Name']] |
| 175 | |
| 176 | field_mapping = { |
| 177 | 'Name': 'name', |
| 178 | 'Start': 'start', |
| 179 | 'End': 'end' |
| 180 | } |
| 181 | |
| 182 | field_transformations = { |
| 183 | 'Start': SyncProgranServiceInstance.date_to_time, |
| 184 | 'End': SyncProgranServiceInstance.date_to_time |
| 185 | } |
| 186 | |
| 187 | handover_mapping = { |
| 188 | 'A5Hysteresis': 'HysteresisA5', |
| 189 | 'A3Hysteresis': 'HysteresisA3' |
| 190 | } |
| 191 | |
| 192 | for p in res: |
| 193 | |
| 194 | # checking for handovers |
| 195 | handover_dict = p['Handover'] |
| 196 | handover_dict = SyncProgranServiceInstance.convert_keys(handover_dict, handover_mapping) |
| 197 | del p['Handover'] |
| 198 | |
| 199 | try: |
| 200 | handover = Handover.objects.get(**handover_dict) |
| 201 | log.info("handover already exists, updating it", handover=handover_dict) |
| 202 | except IndexError: |
| 203 | handover = Handover() |
| 204 | handover = SyncProgranServiceInstance.update_fields(handover, handover_dict) |
| 205 | log.info("handover is new, creating it", handover=handover_dict) |
| 206 | |
| 207 | handover.save() |
| 208 | |
| 209 | # checking for profiles |
| 210 | try: |
| 211 | si = ProgranServiceInstance.objects.get(name=p['Name']) |
| 212 | log.info("Profile %s already exists, updating it" % p['Name']) |
| 213 | except IndexError: |
| 214 | si = ProgranServiceInstance() |
| 215 | si.name = p['Name'] |
| 216 | log.info("Profile %s is new, creating it" % p['Name']) |
| 217 | |
| 218 | si = SyncProgranServiceInstance.update_fields(si, p, field_mapping, field_transformations) |
| 219 | si.handover = handover |
| 220 | |
| 221 | |
| 222 | |
| 223 | # TODO keep track of the deleted profiles |
| 224 | # existing profiles - updated profiles = deleted profiles |
| 225 | |
| 226 | si.save() |