Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 1 | |
| 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 | |
| 17 | import os |
| 18 | import sys |
| 19 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 20 | from synchronizers.new_base.ansible_helper import run_template |
| 21 | from synchronizers.new_base.modelaccessor import ENodeB |
| 22 | |
| 23 | from xosconfig import Config |
| 24 | from multistructlog import create_logger |
| 25 | import json |
| 26 | |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 27 | |
| 28 | log = create_logger(Config().get('logging')) |
| 29 | |
| 30 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame^] | 31 | |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 32 | sys.path.insert(0, parentdir) |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame^] | 33 | sys.path.insert(0, os.path.dirname(__file__)) |
| 34 | |
| 35 | from helpers import ProgranHelpers |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 36 | |
| 37 | class SyncProgranEnodeB(SyncInstanceUsingAnsible): |
| 38 | provides = [ENodeB] |
| 39 | |
| 40 | observes = ENodeB |
| 41 | |
| 42 | def skip_ansible_fields(self, o): |
| 43 | # FIXME This model does not have an instance, this is a workaroung to make it work, |
| 44 | # but it need to be cleaned up creating a general SyncUsingAnsible base class |
| 45 | return True |
| 46 | |
| 47 | def get_progran_enodeb_field(self, o): |
| 48 | |
| 49 | enodeb = { |
| 50 | "eNBId": o.enbId, |
| 51 | "Description": o.description, |
| 52 | "IpAddr": o.ipAddr |
| 53 | } |
| 54 | enodeb = json.dumps(enodeb) |
| 55 | return enodeb |
| 56 | |
| 57 | def get_extra_attributes(self, o): |
| 58 | onos = ProgranHelpers.get_progran_onos_info() |
| 59 | fields = { |
| 60 | 'onos_url': onos['url'], |
| 61 | 'onos_username': onos['username'], |
| 62 | 'onos_password': onos['password'], |
| 63 | 'onos_port': onos['port'], |
| 64 | 'endpoint': 'enodeb', |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame^] | 65 | 'body': self.get_progran_enodeb_field(o), |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 66 | 'method': 'POST' |
| 67 | } |
| 68 | |
| 69 | return fields |
| 70 | |
| 71 | # FIXME we need to override this as the default expect to ssh into a VM |
| 72 | def run_playbook(self, o, fields): |
| 73 | run_template("progran_curl.yaml", fields, object=o) |
| 74 | |
| 75 | def delete_record(self, o): |
| 76 | log.info("deleting object", object=str(o), **o.tologdict()) |
| 77 | onos = ProgranHelpers.get_progran_onos_info() |
| 78 | fields = { |
| 79 | 'onos_url': onos['url'], |
| 80 | 'onos_username': onos['username'], |
| 81 | 'onos_password': onos['password'], |
| 82 | 'onos_port': onos['port'], |
| 83 | 'endpoint': 'enodeb/%s' % o.enbId, |
| 84 | 'profile': '', |
| 85 | 'method': 'DELETE' |
| 86 | } |
| 87 | res = self.run_playbook(o, fields) |