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 |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 19 | from xossynchronizer.steps.syncstep import SyncStep |
| 20 | from xossynchronizer.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 | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 43 | def sync_record(self, o): |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 44 | onos = ProgranHelpers.get_progran_onos_info(self.model_accessor) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 45 | |
| 46 | log.info("sync'ing profile", object=str(o), **o.tologdict()) |
| 47 | |
| 48 | profile_url = "http://%s:%s/onos/progran/profile/" % (onos['url'], onos['port']) |
| 49 | data = self.get_progran_profile_field(o) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 50 | log.debug("Sync'ing profile with data", request_data=data) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 51 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 52 | if o.previously_sync == False: |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame] | 53 | log.debug("Sending POST", url=profile_url, data=json.dumps(data)) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 54 | r = requests.post(profile_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 55 | else: |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame] | 56 | log.debug("Sending PUT", url=profile_url, data=json.dumps(data)) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 57 | r = requests.put(profile_url, data=json.dumps(data), |
| 58 | auth=HTTPBasicAuth(onos['username'], onos['password'])) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 59 | |
| 60 | ProgranHelpers.get_progran_rest_errors(r) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 61 | log.info("Profile synchronized", response=r.json()) |
| 62 | |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 63 | if o.enodeb_id: |
| 64 | log.info("adding profile %s to enodeb %s" % (o.id, o.enodeb.enbId), object=str(o), **o.tologdict()) |
| 65 | enodeb_url = "http://%s:%s/onos/progran/enodeb/%s/profile" % (onos['url'], onos['port'], o.enodeb.enbId) |
| 66 | data = { |
| 67 | "ProfileArray": [ |
| 68 | o.name |
| 69 | ] |
| 70 | } |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 71 | log.debug("Adding enodeb to profile with data", request_data=data) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 72 | 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] | 73 | ProgranHelpers.get_progran_rest_errors(r) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 74 | o.active_enodeb_id = o.enodeb_id # storing the value to know when it will be deleted |
| 75 | log.info("EnodeB synchronized", response=r.json()) |
| 76 | elif o.active_enodeb_id: |
| 77 | enb_id = ENodeB.objects.get(id=o.active_enodeb_id).enbId |
| 78 | log.info("removing profile %s from enodeb %s" % (o.name, o.active_enodeb_id), object=str(o), **o.tologdict()) |
| 79 | enodeb_url = "http://%s:%s/onos/progran/enodeb/%s/profile/%s" % (onos['url'], onos['port'], enb_id, o.name) |
| 80 | r = requests.delete(enodeb_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 81 | ProgranHelpers.get_progran_rest_errors(r) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 82 | o.active_enodeb_id = 0 # removing the value because it has been deleted |
| 83 | log.info("EnodeB synchronized", response=r.json()) |
| 84 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 85 | o.previously_sync = True |
Matteo Scandolo | 7874216 | 2018-02-23 10:24:34 -0800 | [diff] [blame] | 86 | o.no_sync = True |
Matteo Scandolo | 8c096ef | 2018-02-16 22:42:04 -0800 | [diff] [blame] | 87 | o.save() |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 88 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 89 | def get_handover_for_profile(self, o): |
| 90 | return { |
| 91 | "A3Hysteresis": o.handover.HysteresisA3, |
| 92 | "A3TriggerQuantity": o.handover.A3TriggerQuantity, |
| 93 | "A3offset": o.handover.A3offset, |
| 94 | "A5Hysteresis": o.handover.HysteresisA5, |
| 95 | "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp, |
| 96 | "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq, |
| 97 | "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp, |
| 98 | "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq, |
| 99 | "A5TriggerQuantity": o.handover.A5TriggerQuantity, |
| 100 | } |
| 101 | |
| 102 | def get_progran_profile_field(self, o): |
| 103 | |
| 104 | # basic information that we have in the service instance itself |
| 105 | profile = { |
| 106 | 'AdmControl': o.AdmControl, |
| 107 | "DlSchedType": o.DlSchedType, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 108 | "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] | 109 | "UlSchedType": o.UlSchedType, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 110 | "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] | 111 | "CellIndividualOffset": o.CellIndividualOffset, |
| 112 | "DlAllocRBRate": o.DlAllocRBRate, |
| 113 | "Name": o.name, |
| 114 | "UlAllocRBRate": o.UlAllocRBRate, |
| 115 | "Handover": self.get_handover_for_profile(o), |
Matteo Scandolo | 8c096ef | 2018-02-16 22:42:04 -0800 | [diff] [blame] | 116 | "MMECfg": { |
| 117 | "Port": o.mmeport, |
| 118 | "IPAddr": o.mmeip, |
| 119 | }, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 120 | 'DlWifiRate': o.DlWifiRate, |
| 121 | 'DlUeAllocRbRate': o.DlUeAllocRbRate, |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 122 | } |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 123 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 124 | return profile |
| 125 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 126 | def delete_record(self, o): |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 127 | log.info("deleting profile", object=str(o), **o.tologdict()) |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 128 | onos = ProgranHelpers.get_onos_info_from_si(o) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame] | 129 | profile_url = "http://%s:%s/onos/progran/profile/%s" % (onos['url'], onos['port'], o.name) |
| 130 | r = requests.delete(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 131 | 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] | 132 | log.info("Profile synchronized", response=r.json()) |
| 133 | |
| 134 | def fetch_pending(self, deleted): |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 135 | return super(SyncProgranServiceInstance, self).fetch_pending(deleted) |