blob: 4daf1041661c6ee1d60917985b49feb607a3da49 [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
19from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
20from synchronizers.new_base.ansible_helper import run_template
Matteo Scandolo0a207b52018-01-29 13:39:43 -080021from synchronizers.new_base.modelaccessor import ProgranServiceInstance
Matteo Scandolo1c049b02018-01-18 11:32:46 -080022
23from xosconfig import Config
24from multistructlog import create_logger
25import json
26
Matteo Scandolo0a207b52018-01-29 13:39:43 -080027
Matteo Scandolo1c049b02018-01-18 11:32:46 -080028log = create_logger(Config().get('logging'))
29
30parentdir = os.path.join(os.path.dirname(__file__), "..")
31sys.path.insert(0, parentdir)
Matteo Scandoload7f3b42018-01-30 16:41:19 -080032sys.path.insert(0, os.path.dirname(__file__))
33from helpers import ProgranHelpers
Matteo Scandolo1c049b02018-01-18 11:32:46 -080034
35class SyncProgranServiceInstance(SyncInstanceUsingAnsible):
36 provides = [ProgranServiceInstance]
37
38 observes = ProgranServiceInstance
39
Matteo Scandolo1c049b02018-01-18 11:32:46 -080040 def skip_ansible_fields(self, o):
41 # FIXME This model does not have an instance, this is a workaroung to make it work,
42 # but it need to be cleaned up creating a general SyncUsingAnsible base class
43 return True
44
45 def get_handover_for_profile(self, o):
46 return {
47 "A3Hysteresis": o.handover.HysteresisA3,
48 "A3TriggerQuantity": o.handover.A3TriggerQuantity,
49 "A3offset": o.handover.A3offset,
50 "A5Hysteresis": o.handover.HysteresisA5,
51 "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp,
52 "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq,
53 "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp,
54 "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq,
55 "A5TriggerQuantity": o.handover.A5TriggerQuantity,
56 }
57
58 def get_progran_profile_field(self, o):
59
60 # basic information that we have in the service instance itself
61 profile = {
62 'AdmControl': o.AdmControl,
63 "DlSchedType": o.DlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -080064 "Start": o.start, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -080065 "UlSchedType": o.UlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -080066 "End": o.end, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -080067 "CellIndividualOffset": o.CellIndividualOffset,
68 "DlAllocRBRate": o.DlAllocRBRate,
69 "Name": o.name,
70 "UlAllocRBRate": o.UlAllocRBRate,
71 "Handover": self.get_handover_for_profile(o),
Matteo Scandolo6b607c82018-01-30 09:12:26 -080072 'mmeip': o.mmeip,
73 'mmeport': o.mmeport,
74 'DlWifiRate': o.DlWifiRate,
75 'DlUeAllocRbRate': o.DlUeAllocRbRate,
Matteo Scandolo1c049b02018-01-18 11:32:46 -080076 }
77 profile = json.dumps(profile)
78 return profile
79
Matteo Scandolo0a207b52018-01-29 13:39:43 -080080 def sync_record(self, o):
81 # NOTE overriding the default sync_record as we need to execute the playbook 2 times (profile and enodeb)
82
83 log.info("sync'ing profile", object=str(o), **o.tologdict())
84 onos = ProgranHelpers.get_onos_info_from_si(o)
85
86 # common field for both operations
87 base_field = {
Matteo Scandolo1c049b02018-01-18 11:32:46 -080088 'onos_url': onos['url'],
89 'onos_username': onos['username'],
90 'onos_password': onos['password'],
91 'onos_port': onos['port'],
Matteo Scandolo0a207b52018-01-29 13:39:43 -080092 }
93
94 # progran profile specific fields
95 profile_fields = {
Matteo Scandolo1c049b02018-01-18 11:32:46 -080096 'endpoint': 'profile',
Matteo Scandoload7f3b42018-01-30 16:41:19 -080097 'body': self.get_progran_profile_field(o),
Matteo Scandolo1c049b02018-01-18 11:32:46 -080098 'method': 'POST'
99 }
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800100 profile_fields["ansible_tag"] = getattr(o, "ansible_tag", o.__class__.__name__ + "_" + str(o.id))
101 profile_fields.update(base_field)
102 self.run_playbook(o, profile_fields)
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800103
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800104 # progran enodeb specific fields
105 if o.enodeb:
106 log.info("adding profile to enodeb", object=str(o), **o.tologdict())
107 enodeb_fields = {
Matteo Scandoload7f3b42018-01-30 16:41:19 -0800108 'body': json.dumps({
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800109 "ProfileArray": [
110 o.name
111 ]
112 }),
113 'method': 'POST',
114 'endpoint': 'enodeb/%s/profile' % o.enodeb.enbId
115 }
116 enodeb_fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_enodeb_to_profile"
117 enodeb_fields.update(base_field)
118 self.run_playbook(o, enodeb_fields)
119 else:
120 log.warn("IMPLEMENT THE CALL TO REMOVE A PROFILE FROM ENODEB")
121
122
123 o.save()
124
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800125
126 # FIXME we need to override this as the default expect to ssh into a VM
127 def run_playbook(self, o, fields):
128 run_template("progran_curl.yaml", fields, object=o)
129
130 def delete_record(self, o):
131 log.info("deleting object", object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800132 onos = ProgranHelpers.get_onos_info_from_si(o)
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800133 fields = {
134 'onos_url': onos['url'],
135 'onos_username': onos['username'],
136 'onos_password': onos['password'],
137 'onos_port': onos['port'],
138 'endpoint': 'profile/%s' % o.name,
Matteo Scandoload7f3b42018-01-30 16:41:19 -0800139 'body': '',
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800140 'method': 'DELETE'
141 }
142 res = self.run_playbook(o, fields)