Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -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 MCordSubscriberInstance |
| 22 | |
| 23 | from xosconfig import Config |
| 24 | from multistructlog import create_logger |
| 25 | import json |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 26 | import requests |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | log = create_logger(Config().get('logging')) |
| 30 | |
| 31 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 32 | sys.path.insert(0, parentdir) |
| 33 | sys.path.insert(0, os.path.dirname(__file__)) |
| 34 | from helpers import ProgranHelpers |
| 35 | |
| 36 | class SyncProgranIMSI(SyncInstanceUsingAnsible): |
| 37 | provides = [MCordSubscriberInstance] |
| 38 | |
| 39 | observes = MCordSubscriberInstance |
| 40 | |
| 41 | def skip_ansible_fields(self, o): |
| 42 | # FIXME This model does not have an instance, this is a workaroung to make it work, |
| 43 | # but it need to be cleaned up creating a general SyncUsingAnsible base class |
| 44 | return True |
| 45 | |
| 46 | def get_progran_imsi_field(self, o): |
| 47 | |
| 48 | imsi = { |
| 49 | "IMSI": o.imsi_number, |
| 50 | } |
| 51 | imsi = json.dumps(imsi) |
| 52 | return imsi |
| 53 | |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 54 | def get_fields(self, o): |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 55 | onos = ProgranHelpers.get_progran_onos_info() |
| 56 | fields = { |
| 57 | 'onos_url': onos['url'], |
| 58 | 'onos_username': onos['username'], |
| 59 | 'onos_password': onos['password'], |
| 60 | 'onos_port': onos['port'], |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return fields |
| 64 | |
| 65 | def sync_record(self, o): |
| 66 | # NOTE overriding the default method as we need to read from progran |
| 67 | base_fields = self.get_fields(o) |
| 68 | |
| 69 | create_fields = { |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 70 | 'endpoint': 'imsi', |
| 71 | 'body': self.get_progran_imsi_field(o), |
| 72 | 'method': 'POST' |
| 73 | } |
| 74 | |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 75 | create_fields["ansible_tag"] = getattr(o, "ansible_tag", o.__class__.__name__ + "_" + str(o.id)) |
| 76 | create_fields.update(base_fields) |
| 77 | |
| 78 | self.run_playbook(o, create_fields) |
| 79 | |
| 80 | # fetch the IMSI we just created |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame^] | 81 | # NOTE we won't need this method once we'll have polling in place |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 82 | imsi_url = "http://%s:%s/onos/progran/imsi/%s" % (base_fields['onos_url'], base_fields['onos_port'], o.imsi_number) |
| 83 | r = requests.get(imsi_url) |
| 84 | o.ue_status = r.json()['ImsiArray'][0]['UeStatus'] |
| 85 | |
| 86 | o.save() |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 87 | |
| 88 | # FIXME we need to override this as the default expect to ssh into a VM |
| 89 | def run_playbook(self, o, fields): |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 90 | return run_template("progran_curl.yaml", fields, object=o) |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 91 | |
| 92 | def delete_record(self, o): |
| 93 | log.info("deleting object", object=str(o), **o.tologdict()) |
| 94 | onos = ProgranHelpers.get_progran_onos_info() |
| 95 | fields = { |
| 96 | 'onos_url': onos['url'], |
| 97 | 'onos_username': onos['username'], |
| 98 | 'onos_password': onos['password'], |
| 99 | 'onos_port': onos['port'], |
| 100 | 'endpoint': 'imsi/%s' % o.imsi_number, |
| 101 | 'body': '', |
| 102 | 'method': 'DELETE' |
| 103 | } |
| 104 | res = self.run_playbook(o, fields) |