blob: 14925bc017dda19669d48267734563b99b6fd3b9 [file] [log] [blame]
Matteo Scandolo1c049b02018-01-18 11:32:46 -08001
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
17import os
18import sys
Matteo Scandolob5352ed2018-02-01 16:14:05 -080019from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
Matteo Scandolo1c049b02018-01-18 11:32:46 -080020from synchronizers.new_base.ansible_helper import run_template
Matteo Scandoloe463b062018-02-01 10:14:03 -080021from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB
Matteo Scandolo1c049b02018-01-18 11:32:46 -080022
23from xosconfig import Config
24from multistructlog import create_logger
25import json
Matteo Scandolob5352ed2018-02-01 16:14:05 -080026import requests
27from requests.auth import HTTPBasicAuth
Matteo Scandolo1c049b02018-01-18 11:32:46 -080028
Matteo Scandolo0a207b52018-01-29 13:39:43 -080029
Matteo Scandolo1c049b02018-01-18 11:32:46 -080030log = create_logger(Config().get('logging'))
31
32parentdir = os.path.join(os.path.dirname(__file__), "..")
33sys.path.insert(0, parentdir)
Matteo Scandoload7f3b42018-01-30 16:41:19 -080034sys.path.insert(0, os.path.dirname(__file__))
35from helpers import ProgranHelpers
Matteo Scandolo1c049b02018-01-18 11:32:46 -080036
Matteo Scandolob5352ed2018-02-01 16:14:05 -080037class SyncProgranServiceInstance(SyncStep):
Matteo Scandolo1c049b02018-01-18 11:32:46 -080038 provides = [ProgranServiceInstance]
39
40 observes = ProgranServiceInstance
41
Matteo Scandolob5352ed2018-02-01 16:14:05 -080042 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 Scandolo1c049b02018-01-18 11:32:46 -080075
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 Scandolo6b607c82018-01-30 09:12:26 -080095 "Start": o.start, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -080096 "UlSchedType": o.UlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -080097 "End": o.end, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -080098 "CellIndividualOffset": o.CellIndividualOffset,
99 "DlAllocRBRate": o.DlAllocRBRate,
100 "Name": o.name,
101 "UlAllocRBRate": o.UlAllocRBRate,
102 "Handover": self.get_handover_for_profile(o),
Matteo Scandolo6b607c82018-01-30 09:12:26 -0800103 'mmeip': o.mmeip,
104 'mmeport': o.mmeport,
105 'DlWifiRate': o.DlWifiRate,
106 'DlUeAllocRbRate': o.DlUeAllocRbRate,
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800107 }
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800108
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800109 return profile
110
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800111 def delete_record(self, o):
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800112 log.info("deleting profile", object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800113 onos = ProgranHelpers.get_onos_info_from_si(o)
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800114 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())