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 | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 20 | from synchronizers.new_base.ansible_helper import run_template |
Matteo Scandolo | e463b06 | 2018-02-01 10:14:03 -0800 | [diff] [blame] | 21 | from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB |
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 |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame^] | 26 | import requests |
| 27 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 28 | |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 29 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 30 | log = create_logger(Config().get('logging')) |
| 31 | |
| 32 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 33 | sys.path.insert(0, parentdir) |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 34 | sys.path.insert(0, os.path.dirname(__file__)) |
| 35 | from helpers import ProgranHelpers |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 36 | |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame^] | 37 | class SyncProgranServiceInstance(SyncStep): |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 38 | provides = [ProgranServiceInstance] |
| 39 | |
| 40 | observes = ProgranServiceInstance |
| 41 | |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame^] | 42 | def sync_record(self, o): |
| 43 | onos = ProgranHelpers.get_progran_onos_info() |
| 44 | |
| 45 | log.info("sync'ing profile", object=str(o), **o.tologdict()) |
| 46 | |
| 47 | profile_url = "http://%s:%s/onos/progran/profile/" % (onos['url'], onos['port']) |
| 48 | data = self.get_progran_profile_field(o) |
| 49 | |
| 50 | r = requests.post(profile_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 51 | log.info("Profile synchronized", response=r.json()) |
| 52 | |
| 53 | log.info("sync'ing enodeb", object=str(o), **o.tologdict()) |
| 54 | if o.enodeb_id: |
| 55 | log.info("adding profile %s to enodeb %s" % (o.id, o.enodeb.enbId), object=str(o), **o.tologdict()) |
| 56 | enodeb_url = "http://%s:%s/onos/progran/enodeb/%s/profile" % (onos['url'], onos['port'], o.enodeb.enbId) |
| 57 | data = { |
| 58 | "ProfileArray": [ |
| 59 | o.name |
| 60 | ] |
| 61 | } |
| 62 | r = requests.post(enodeb_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 63 | o.active_enodeb_id = o.enodeb_id # storing the value to know when it will be deleted |
| 64 | log.info("EnodeB synchronized", response=r.json()) |
| 65 | elif o.active_enodeb_id: |
| 66 | enb_id = ENodeB.objects.get(id=o.active_enodeb_id).enbId |
| 67 | log.info("removing profile %s from enodeb %s" % (o.name, o.active_enodeb_id), object=str(o), **o.tologdict()) |
| 68 | enodeb_url = "http://%s:%s/onos/progran/enodeb/%s/profile/%s" % (onos['url'], onos['port'], enb_id, o.name) |
| 69 | r = requests.delete(enodeb_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 70 | o.active_enodeb_id = 0 # removing the value because it has been deleted |
| 71 | log.info("EnodeB synchronized", response=r.json()) |
| 72 | |
| 73 | o.save() |
| 74 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 75 | |
| 76 | def get_handover_for_profile(self, o): |
| 77 | return { |
| 78 | "A3Hysteresis": o.handover.HysteresisA3, |
| 79 | "A3TriggerQuantity": o.handover.A3TriggerQuantity, |
| 80 | "A3offset": o.handover.A3offset, |
| 81 | "A5Hysteresis": o.handover.HysteresisA5, |
| 82 | "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp, |
| 83 | "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq, |
| 84 | "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp, |
| 85 | "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq, |
| 86 | "A5TriggerQuantity": o.handover.A5TriggerQuantity, |
| 87 | } |
| 88 | |
| 89 | def get_progran_profile_field(self, o): |
| 90 | |
| 91 | # basic information that we have in the service instance itself |
| 92 | profile = { |
| 93 | 'AdmControl': o.AdmControl, |
| 94 | "DlSchedType": o.DlSchedType, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 95 | "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] | 96 | "UlSchedType": o.UlSchedType, |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 97 | "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] | 98 | "CellIndividualOffset": o.CellIndividualOffset, |
| 99 | "DlAllocRBRate": o.DlAllocRBRate, |
| 100 | "Name": o.name, |
| 101 | "UlAllocRBRate": o.UlAllocRBRate, |
| 102 | "Handover": self.get_handover_for_profile(o), |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 103 | 'mmeip': o.mmeip, |
| 104 | 'mmeport': o.mmeport, |
| 105 | 'DlWifiRate': o.DlWifiRate, |
| 106 | 'DlUeAllocRbRate': o.DlUeAllocRbRate, |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 107 | } |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame^] | 108 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 109 | return profile |
| 110 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 111 | def delete_record(self, o): |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame^] | 112 | log.info("deleting profile", object=str(o), **o.tologdict()) |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 113 | onos = ProgranHelpers.get_onos_info_from_si(o) |
Matteo Scandolo | b5352ed | 2018-02-01 16:14:05 -0800 | [diff] [blame^] | 114 | profile_url = "http://%s:%s/onos/progran/profile/%s" % (onos['url'], onos['port'], o.name) |
| 115 | r = requests.delete(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 116 | o.active_enodeb_id = 0 # removing the value because it has been deleted |
| 117 | log.info("Profile synchronized", response=r.json()) |