blob: bcfadfd26a3eb3bfa4d6917c5342493ae69b6212 [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 Scandolod52da972018-01-31 14:37:43 -080040 # NOTE I need to keep track of the relations between profile and enodebs to remove them
41 # it contains: <profile-id>:<enodeb_id>
42 profile_enodebs = {}
43
Matteo Scandolo1c049b02018-01-18 11:32:46 -080044 def skip_ansible_fields(self, o):
45 # FIXME This model does not have an instance, this is a workaroung to make it work,
46 # but it need to be cleaned up creating a general SyncUsingAnsible base class
47 return True
48
49 def get_handover_for_profile(self, o):
50 return {
51 "A3Hysteresis": o.handover.HysteresisA3,
52 "A3TriggerQuantity": o.handover.A3TriggerQuantity,
53 "A3offset": o.handover.A3offset,
54 "A5Hysteresis": o.handover.HysteresisA5,
55 "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp,
56 "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq,
57 "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp,
58 "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq,
59 "A5TriggerQuantity": o.handover.A5TriggerQuantity,
60 }
61
62 def get_progran_profile_field(self, o):
63
64 # basic information that we have in the service instance itself
65 profile = {
66 'AdmControl': o.AdmControl,
67 "DlSchedType": o.DlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -080068 "Start": o.start, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -080069 "UlSchedType": o.UlSchedType,
Matteo Scandolo6b607c82018-01-30 09:12:26 -080070 "End": o.end, # TODO date has to be in the format dd.MM.yyyy HH:mm
Matteo Scandolo1c049b02018-01-18 11:32:46 -080071 "CellIndividualOffset": o.CellIndividualOffset,
72 "DlAllocRBRate": o.DlAllocRBRate,
73 "Name": o.name,
74 "UlAllocRBRate": o.UlAllocRBRate,
75 "Handover": self.get_handover_for_profile(o),
Matteo Scandolo6b607c82018-01-30 09:12:26 -080076 'mmeip': o.mmeip,
77 'mmeport': o.mmeport,
78 'DlWifiRate': o.DlWifiRate,
79 'DlUeAllocRbRate': o.DlUeAllocRbRate,
Matteo Scandolo1c049b02018-01-18 11:32:46 -080080 }
81 profile = json.dumps(profile)
82 return profile
83
Matteo Scandolo0a207b52018-01-29 13:39:43 -080084 def sync_record(self, o):
85 # NOTE overriding the default sync_record as we need to execute the playbook 2 times (profile and enodeb)
86
87 log.info("sync'ing profile", object=str(o), **o.tologdict())
88 onos = ProgranHelpers.get_onos_info_from_si(o)
89
90 # common field for both operations
91 base_field = {
Matteo Scandolo1c049b02018-01-18 11:32:46 -080092 'onos_url': onos['url'],
93 'onos_username': onos['username'],
94 'onos_password': onos['password'],
95 'onos_port': onos['port'],
Matteo Scandolo0a207b52018-01-29 13:39:43 -080096 }
97
98 # progran profile specific fields
99 profile_fields = {
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800100 'endpoint': 'profile',
Matteo Scandoload7f3b42018-01-30 16:41:19 -0800101 'body': self.get_progran_profile_field(o),
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800102 'method': 'POST'
103 }
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800104 profile_fields["ansible_tag"] = getattr(o, "ansible_tag", o.__class__.__name__ + "_" + str(o.id))
105 profile_fields.update(base_field)
106 self.run_playbook(o, profile_fields)
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800107
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800108 # progran enodeb specific fields
109 if o.enodeb:
Matteo Scandolod52da972018-01-31 14:37:43 -0800110 log.info("adding profile %s to enodeb %s" % (o.id, o.enodeb.enbId), object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800111 enodeb_fields = {
Matteo Scandoload7f3b42018-01-30 16:41:19 -0800112 'body': json.dumps({
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800113 "ProfileArray": [
114 o.name
115 ]
116 }),
117 'method': 'POST',
118 'endpoint': 'enodeb/%s/profile' % o.enodeb.enbId
119 }
120 enodeb_fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_enodeb_to_profile"
121 enodeb_fields.update(base_field)
122 self.run_playbook(o, enodeb_fields)
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800123
Matteo Scandolod52da972018-01-31 14:37:43 -0800124 # update local state
125 self.profile_enodebs[o.id] = o.enodeb.enbId
126 else:
127 try:
128 enbid = self.profile_enodebs[o.id]
129 except KeyError:
130 enbid = None
131 if enbid:
132 print enbid
133 log.info("removing profile %s from enodeb %s" % (o.id, self.profile_enodebs[o.id]), object=str(o), **o.tologdict())
134 enodeb_fields = {
135 'body': '',
136 'method': 'DELETE',
137 'endpoint': 'enodeb/%s/profile/%s' % (enbid, o.name)
138 }
139 enodeb_fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_rm_enodeb_from_profile"
140 enodeb_fields.update(base_field)
141 self.run_playbook(o, enodeb_fields)
142 del self.profile_enodebs[o.id]
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800143
144 o.save()
145
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800146
147 # FIXME we need to override this as the default expect to ssh into a VM
148 def run_playbook(self, o, fields):
149 run_template("progran_curl.yaml", fields, object=o)
150
151 def delete_record(self, o):
152 log.info("deleting object", object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800153 onos = ProgranHelpers.get_onos_info_from_si(o)
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800154 fields = {
155 'onos_url': onos['url'],
156 'onos_username': onos['username'],
157 'onos_password': onos['password'],
158 'onos_port': onos['port'],
159 'endpoint': 'profile/%s' % o.name,
Matteo Scandoload7f3b42018-01-30 16:41:19 -0800160 'body': '',
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800161 'method': 'DELETE'
162 }
163 res = self.run_playbook(o, fields)