blob: 11d679d44fc4a94362d69bc6f95f248150b7e8a5 [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 Scandoloc1102a52018-02-01 17:26:04 -080020from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB, Handover
Matteo Scandolo1c049b02018-01-18 11:32:46 -080021
22from xosconfig import Config
23from multistructlog import create_logger
24import json
Matteo Scandolob5352ed2018-02-01 16:14:05 -080025import requests
26from requests.auth import HTTPBasicAuth
Matteo Scandoloc1102a52018-02-01 17:26:04 -080027import time
28import datetime
Matteo Scandolo1c049b02018-01-18 11:32:46 -080029
Matteo Scandolo0a207b52018-01-29 13:39:43 -080030
Matteo Scandolo1c049b02018-01-18 11:32:46 -080031log = create_logger(Config().get('logging'))
32
33parentdir = os.path.join(os.path.dirname(__file__), "..")
34sys.path.insert(0, parentdir)
Matteo Scandoload7f3b42018-01-30 16:41:19 -080035sys.path.insert(0, os.path.dirname(__file__))
36from helpers import ProgranHelpers
Matteo Scandolo1c049b02018-01-18 11:32:46 -080037
Matteo Scandolob5352ed2018-02-01 16:14:05 -080038class SyncProgranServiceInstance(SyncStep):
Matteo Scandolo1c049b02018-01-18 11:32:46 -080039 provides = [ProgranServiceInstance]
40
41 observes = ProgranServiceInstance
42
Matteo Scandoloc1102a52018-02-01 17:26:04 -080043 # Poll every 5 loops of self.call
44 poll = 0
45
Matteo Scandolob5352ed2018-02-01 16:14:05 -080046 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 Scandoloc1102a52018-02-01 17:26:04 -080055
56 ProgranHelpers.get_progran_rest_errors(r)
Matteo Scandolob5352ed2018-02-01 16:14:05 -080057 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 Scandoloc1102a52018-02-01 17:26:04 -080069 ProgranHelpers.get_progran_rest_errors(r)
Matteo Scandolob5352ed2018-02-01 16:14:05 -080070 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 Scandoloc1102a52018-02-01 17:26:04 -080077 ProgranHelpers.get_progran_rest_errors(r)
Matteo Scandolob5352ed2018-02-01 16:14:05 -080078 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 Scandolo1c049b02018-01-18 11:32:46 -080083 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 Scandolo6b607c82018-01-30 09:12:26 -0800102 "Start": o.start, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800103 "UlSchedType": o.UlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -0800104 "End": o.end, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800105 "CellIndividualOffset": o.CellIndividualOffset,
106 "DlAllocRBRate": o.DlAllocRBRate,
107 "Name": o.name,
108 "UlAllocRBRate": o.UlAllocRBRate,
109 "Handover": self.get_handover_for_profile(o),
Matteo Scandolo6b607c82018-01-30 09:12:26 -0800110 'mmeip': o.mmeip,
111 'mmeport': o.mmeport,
112 'DlWifiRate': o.DlWifiRate,
113 'DlUeAllocRbRate': o.DlUeAllocRbRate,
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800114 }
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800115
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800116 return profile
117
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800118 def delete_record(self, o):
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800119 log.info("deleting profile", object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800120 onos = ProgranHelpers.get_onos_info_from_si(o)
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800121 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 Scandoloc1102a52018-02-01 17:26:04 -0800124 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()