Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -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 | |
| 20 | import datetime |
| 21 | import time |
| 22 | |
| 23 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep |
| 24 | from synchronizers.new_base.modelaccessor import MCordSubscriberInstance |
| 25 | |
| 26 | from xosconfig import Config |
| 27 | from multistructlog import create_logger |
| 28 | import json |
| 29 | import requests |
| 30 | from requests.auth import HTTPBasicAuth |
| 31 | |
| 32 | |
| 33 | |
| 34 | log = create_logger(Config().get('logging')) |
| 35 | |
| 36 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 37 | sys.path.insert(0, parentdir) |
| 38 | sys.path.insert(0, os.path.dirname(__file__)) |
| 39 | from helpers import ProgranHelpers |
| 40 | |
| 41 | class SyncImsiBack(SyncStep): |
| 42 | provides = [MCordSubscriberInstance] |
| 43 | |
| 44 | observes = MCordSubscriberInstance |
| 45 | |
| 46 | |
| 47 | def call(self, failed=[], deletion=False): |
| 48 | """ |
| 49 | Read profile from progran and save them in xos |
| 50 | """ |
| 51 | |
| 52 | if deletion == False: |
| 53 | # NOTE we won't it to run only after the delete has completed |
| 54 | return |
| 55 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame^] | 56 | log.debug("Reading IMSI from progran") |
Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -0800 | [diff] [blame] | 57 | onos = ProgranHelpers.get_progran_onos_info() |
| 58 | imsi_url = "http://%s:%s/onos/progran/imsi/" % (onos['url'], onos['port']) |
| 59 | r = requests.get(imsi_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 60 | res = r.json()['ImsiArray'] |
| 61 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame^] | 62 | log.debug("Received IMSIs: ", imsis=res) |
Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -0800 | [diff] [blame] | 63 | |
| 64 | field_mapping = { |
| 65 | 'IMSI': 'imsi_number', |
| 66 | 'UeStatus': 'ue_status' |
| 67 | } |
| 68 | |
| 69 | updated_imsi = [] |
| 70 | |
| 71 | for i in res: |
Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -0800 | [diff] [blame] | 72 | try: |
| 73 | si = MCordSubscriberInstance.objects.get(imsi_number=i['IMSI']) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame^] | 74 | log.debug("IMSI %s already exists, updating it" % i['IMSI']) |
Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -0800 | [diff] [blame] | 75 | except IndexError: |
| 76 | si = MCordSubscriberInstance() |
| 77 | |
| 78 | si.no_sync = True |
| 79 | si.backend_code = 1 |
| 80 | si.backend_status = "OK" |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame^] | 81 | si.created_by = "Progran" |
Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -0800 | [diff] [blame] | 82 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame^] | 83 | log.debug("IMSI %s is new, creating it" % i['IMSI']) |
Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -0800 | [diff] [blame] | 84 | |
| 85 | si = ProgranHelpers.update_fields(si, i, field_mapping,) |
| 86 | |
| 87 | si.save() |
| 88 | |
| 89 | updated_imsi.append(si.imsi_number) |
| 90 | |
| 91 | existing_imsi = [p.imsi_number for p in MCordSubscriberInstance.objects.all() if not p.is_new] |
| 92 | deleted_imsi = ProgranHelpers.list_diff(existing_imsi, updated_imsi) |
| 93 | |
| 94 | if len(deleted_imsi) > 0: |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame^] | 95 | log.debug("Profiles %s have been removed in progran, removing them from XOS" % str(deleted_imsi)) |
Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -0800 | [diff] [blame] | 96 | for p in deleted_imsi: |
| 97 | si = MCordSubscriberInstance.objects.get(imsi_number=p) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame^] | 98 | # if si.created_by == 'XOS' and si.previously_sync == False: |
| 99 | # don't delete if the imsi has been created by XOS and it hasn't been sync'ed yet |
| 100 | # continue |
Matteo Scandolo | 62919d6 | 2018-02-02 16:33:21 -0800 | [diff] [blame] | 101 | si.delete() |