blob: d4e3d70e03b2dcfd532bf0d6e27dc48f7bb47c1e [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 Scandolobc37ad12018-02-05 10:51:59 -080019from synchronizers.new_base.syncstep import SyncStep
Matteo Scandolode4597f2018-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 Scandolode4597f2018-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 Scandolob5352ed2018-02-01 16:14:05 -080043 def sync_record(self, o):
44 onos = ProgranHelpers.get_progran_onos_info()
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 Scandolobc37ad12018-02-05 10:51:59 -080050 log.debug("Sync'ing profile with data", request_data=data)
Matteo Scandolob5352ed2018-02-01 16:14:05 -080051
Matteo Scandolobc37ad12018-02-05 10:51:59 -080052 if o.previously_sync == False:
Matteo Scandolo43a25b52018-02-13 15:27:21 -080053 log.debug("Sending POST", url=profile_url, data=json.dumps(data))
Matteo Scandolobc37ad12018-02-05 10:51:59 -080054 r = requests.post(profile_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password']))
55 else:
Matteo Scandolo43a25b52018-02-13 15:27:21 -080056 log.debug("Sending PUT", url=profile_url, data=json.dumps(data))
Matteo Scandolobc37ad12018-02-05 10:51:59 -080057 r = requests.put(profile_url, data=json.dumps(data),
58 auth=HTTPBasicAuth(onos['username'], onos['password']))
Matteo Scandolode4597f2018-02-01 17:26:04 -080059
60 ProgranHelpers.get_progran_rest_errors(r)
Matteo Scandolob5352ed2018-02-01 16:14:05 -080061 log.info("Profile synchronized", response=r.json())
62
Matteo Scandolob5352ed2018-02-01 16:14:05 -080063 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 Scandolobc37ad12018-02-05 10:51:59 -080071 log.debug("Adding enodeb to profile with data", request_data=data)
Matteo Scandolob5352ed2018-02-01 16:14:05 -080072 r = requests.post(enodeb_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password']))
Matteo Scandolode4597f2018-02-01 17:26:04 -080073 ProgranHelpers.get_progran_rest_errors(r)
Matteo Scandolob5352ed2018-02-01 16:14:05 -080074 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 Scandolode4597f2018-02-01 17:26:04 -080081 ProgranHelpers.get_progran_rest_errors(r)
Matteo Scandolob5352ed2018-02-01 16:14:05 -080082 o.active_enodeb_id = 0 # removing the value because it has been deleted
83 log.info("EnodeB synchronized", response=r.json())
84
Matteo Scandolobc37ad12018-02-05 10:51:59 -080085 o.previously_sync = True
Matteo Scandolo74bb9752018-02-23 10:24:34 -080086 o.no_sync = True
Matteo Scandolo51297192018-02-16 22:42:04 -080087 o.save()
Matteo Scandolob5352ed2018-02-01 16:14:05 -080088
Matteo Scandolo1c049b02018-01-18 11:32:46 -080089 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 Scandolo6b607c82018-01-30 09:12:26 -0800108 "Start": o.start, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800109 "UlSchedType": o.UlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -0800110 "End": o.end, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800111 "CellIndividualOffset": o.CellIndividualOffset,
112 "DlAllocRBRate": o.DlAllocRBRate,
113 "Name": o.name,
114 "UlAllocRBRate": o.UlAllocRBRate,
115 "Handover": self.get_handover_for_profile(o),
Matteo Scandolo51297192018-02-16 22:42:04 -0800116 "MMECfg": {
117 "Port": o.mmeport,
118 "IPAddr": o.mmeip,
119 },
Matteo Scandolo6b607c82018-01-30 09:12:26 -0800120 'DlWifiRate': o.DlWifiRate,
121 'DlUeAllocRbRate': o.DlUeAllocRbRate,
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800122 }
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800123
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800124 return profile
125
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800126 def delete_record(self, o):
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800127 log.info("deleting profile", object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800128 onos = ProgranHelpers.get_onos_info_from_si(o)
Matteo Scandolob5352ed2018-02-01 16:14:05 -0800129 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 Scandolode4597f2018-02-01 17:26:04 -0800132 log.info("Profile synchronized", response=r.json())
133
134 def fetch_pending(self, deleted):
Matteo Scandolode4597f2018-02-01 17:26:04 -0800135 return super(SyncProgranServiceInstance, self).fetch_pending(deleted)