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 |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 19 | from xossynchronizer.steps.SyncInstanceUsingAnsible import SyncStep |
| 20 | from xossynchronizer.modelaccessor import MCordSubscriberInstance |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 21 | |
| 22 | from xosconfig import Config |
| 23 | from multistructlog import create_logger |
| 24 | import json |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 25 | import requests |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 26 | from requests.auth import HTTPBasicAuth |
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 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 36 | class SyncProgranIMSI(SyncStep): |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 37 | provides = [MCordSubscriberInstance] |
| 38 | |
| 39 | observes = MCordSubscriberInstance |
| 40 | |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 41 | def get_progran_imsi_field(self, o): |
| 42 | |
| 43 | imsi = { |
| 44 | "IMSI": o.imsi_number, |
| 45 | } |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 46 | return imsi |
| 47 | |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 48 | def sync_record(self, o): |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 49 | log.info("sync'ing imsi", object=str(o), **o.tologdict()) |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 50 | onos = ProgranHelpers.get_progran_onos_info(self.model_accessor) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 51 | imsi_url = "http://%s:%s/onos/progran/imsi/" % (onos['url'], onos['port']) |
| 52 | data = self.get_progran_imsi_field(o) |
| 53 | r = requests.post(imsi_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password'])) |
Matteo Scandolo | e975431 | 2018-01-31 12:38:30 -0800 | [diff] [blame] | 54 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 55 | ProgranHelpers.get_progran_rest_errors(r) |
| 56 | log.info("Profile synchronized", response=r.json()) |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 57 | |
| 58 | def delete_record(self, o): |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 59 | log.info("deleting imsi", object=str(o), **o.tologdict()) |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 60 | onos = ProgranHelpers.get_progran_onos_info(self.model_accessor) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 61 | profile_url = "http://%s:%s/onos/progran/imsi/%s" % (onos['url'], onos['port'], o.imsi_number) |
| 62 | r = requests.delete(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 63 | log.info("IMSI synchronized", response=r.json()) |