blob: b8cbd3b5349a82b597769f7f8e74e564e2cccb65 [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 -080027from helpers import ProgranHelpers
28
Matteo Scandolo1c049b02018-01-18 11:32:46 -080029log = create_logger(Config().get('logging'))
30
31parentdir = os.path.join(os.path.dirname(__file__), "..")
32sys.path.insert(0, parentdir)
33
34class SyncProgranServiceInstance(SyncInstanceUsingAnsible):
35 provides = [ProgranServiceInstance]
36
37 observes = ProgranServiceInstance
38
Matteo Scandolo1c049b02018-01-18 11:32:46 -080039 def skip_ansible_fields(self, o):
40 # FIXME This model does not have an instance, this is a workaroung to make it work,
41 # but it need to be cleaned up creating a general SyncUsingAnsible base class
42 return True
43
44 def get_handover_for_profile(self, o):
45 return {
46 "A3Hysteresis": o.handover.HysteresisA3,
47 "A3TriggerQuantity": o.handover.A3TriggerQuantity,
48 "A3offset": o.handover.A3offset,
49 "A5Hysteresis": o.handover.HysteresisA5,
50 "A5Thresh1Rsrp": o.handover.A5Thresh1Rsrp,
51 "A5Thresh1Rsrq": o.handover.A5Thresh1Rsrq,
52 "A5Thresh2Rsrp": o.handover.A5Thresh2Rsrp,
53 "A5Thresh2Rsrq": o.handover.A5Thresh2Rsrq,
54 "A5TriggerQuantity": o.handover.A5TriggerQuantity,
55 }
56
57 def get_progran_profile_field(self, o):
58
59 # basic information that we have in the service instance itself
60 profile = {
61 'AdmControl': o.AdmControl,
62 "DlSchedType": o.DlSchedType,
63 "Start": o.start,
64 "UlSchedType": o.UlSchedType,
65 "End": o.end,
66 "CellIndividualOffset": o.CellIndividualOffset,
67 "DlAllocRBRate": o.DlAllocRBRate,
68 "Name": o.name,
69 "UlAllocRBRate": o.UlAllocRBRate,
70 "Handover": self.get_handover_for_profile(o),
71 }
72 profile = json.dumps(profile)
73 return profile
74
Matteo Scandolo0a207b52018-01-29 13:39:43 -080075 def sync_record(self, o):
76 # NOTE overriding the default sync_record as we need to execute the playbook 2 times (profile and enodeb)
77
78 log.info("sync'ing profile", object=str(o), **o.tologdict())
79 onos = ProgranHelpers.get_onos_info_from_si(o)
80
81 # common field for both operations
82 base_field = {
Matteo Scandolo1c049b02018-01-18 11:32:46 -080083 'onos_url': onos['url'],
84 'onos_username': onos['username'],
85 'onos_password': onos['password'],
86 'onos_port': onos['port'],
Matteo Scandolo0a207b52018-01-29 13:39:43 -080087 }
88
89 # progran profile specific fields
90 profile_fields = {
Matteo Scandolo1c049b02018-01-18 11:32:46 -080091 'endpoint': 'profile',
92 'profile': self.get_progran_profile_field(o),
93 'method': 'POST'
94 }
Matteo Scandolo0a207b52018-01-29 13:39:43 -080095 profile_fields["ansible_tag"] = getattr(o, "ansible_tag", o.__class__.__name__ + "_" + str(o.id))
96 profile_fields.update(base_field)
97 self.run_playbook(o, profile_fields)
Matteo Scandolo1c049b02018-01-18 11:32:46 -080098
Matteo Scandolo0a207b52018-01-29 13:39:43 -080099 # progran enodeb specific fields
100 if o.enodeb:
101 log.info("adding profile to enodeb", object=str(o), **o.tologdict())
102 enodeb_fields = {
103 'profile': json.dumps({
104 "ProfileArray": [
105 o.name
106 ]
107 }),
108 'method': 'POST',
109 'endpoint': 'enodeb/%s/profile' % o.enodeb.enbId
110 }
111 enodeb_fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_enodeb_to_profile"
112 enodeb_fields.update(base_field)
113 self.run_playbook(o, enodeb_fields)
114 else:
115 log.warn("IMPLEMENT THE CALL TO REMOVE A PROFILE FROM ENODEB")
116
117
118 o.save()
119
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800120
121 # FIXME we need to override this as the default expect to ssh into a VM
122 def run_playbook(self, o, fields):
123 run_template("progran_curl.yaml", fields, object=o)
124
125 def delete_record(self, o):
126 log.info("deleting object", object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -0800127 onos = ProgranHelpers.get_onos_info_from_si(o)
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800128 fields = {
129 'onos_url': onos['url'],
130 'onos_username': onos['username'],
131 'onos_password': onos['password'],
132 'onos_port': onos['port'],
133 'endpoint': 'profile/%s' % o.name,
134 'profile': '',
135 'method': 'DELETE'
136 }
137 res = self.run_playbook(o, fields)