blob: e81232c60566a988d64e6f814829a3deebec2384 [file] [log] [blame]
Matteo Scandolo62919d62018-02-02 16:33:21 -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
19
20import datetime
21import time
22
Scott Bakerb06daad2019-02-04 15:46:54 -080023from xossynchronizer.steps.SyncInstanceUsingAnsible import SyncStep
24from xossynchronizer.modelaccessor import MCordSubscriberInstance
Matteo Scandolo62919d62018-02-02 16:33:21 -080025
26from xosconfig import Config
27from multistructlog import create_logger
28import json
29import requests
30from requests.auth import HTTPBasicAuth
31
32
33
34log = create_logger(Config().get('logging'))
35
36parentdir = os.path.join(os.path.dirname(__file__), "..")
37sys.path.insert(0, parentdir)
38sys.path.insert(0, os.path.dirname(__file__))
39from helpers import ProgranHelpers
40
41class 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 Scandolo830403a2018-02-05 10:51:59 -080056 log.debug("Reading IMSI from progran")
Scott Bakerb06daad2019-02-04 15:46:54 -080057 onos = ProgranHelpers.get_progran_onos_info(self.model_accessor)
Matteo Scandolo62919d62018-02-02 16:33:21 -080058 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 Scandolo830403a2018-02-05 10:51:59 -080062 log.debug("Received IMSIs: ", imsis=res)
Matteo Scandolo62919d62018-02-02 16:33:21 -080063
64 field_mapping = {
65 'IMSI': 'imsi_number',
66 'UeStatus': 'ue_status'
67 }
68
Matteo Scandolo138070a2019-02-16 11:06:17 -080069 field_transformations = {
70 'UeStatus': ProgranHelpers.int_to_string,
71 }
72
Matteo Scandolo62919d62018-02-02 16:33:21 -080073 updated_imsi = []
74
75 for i in res:
Matteo Scandolo62919d62018-02-02 16:33:21 -080076 try:
77 si = MCordSubscriberInstance.objects.get(imsi_number=i['IMSI'])
Matteo Scandolo830403a2018-02-05 10:51:59 -080078 log.debug("IMSI %s already exists, updating it" % i['IMSI'])
Matteo Scandolo62919d62018-02-02 16:33:21 -080079 except IndexError:
80 si = MCordSubscriberInstance()
81
82 si.no_sync = True
83 si.backend_code = 1
84 si.backend_status = "OK"
Matteo Scandolo830403a2018-02-05 10:51:59 -080085 si.created_by = "Progran"
Matteo Scandolo62919d62018-02-02 16:33:21 -080086
Matteo Scandolo830403a2018-02-05 10:51:59 -080087 log.debug("IMSI %s is new, creating it" % i['IMSI'])
Matteo Scandolo62919d62018-02-02 16:33:21 -080088
Matteo Scandolo138070a2019-02-16 11:06:17 -080089 si = ProgranHelpers.update_fields(si, i, field_mapping, field_transformations)
Matteo Scandolo62919d62018-02-02 16:33:21 -080090
91 si.save()
92
93 updated_imsi.append(si.imsi_number)
94
95 existing_imsi = [p.imsi_number for p in MCordSubscriberInstance.objects.all() if not p.is_new]
96 deleted_imsi = ProgranHelpers.list_diff(existing_imsi, updated_imsi)
97
98 if len(deleted_imsi) > 0:
Matteo Scandolo830403a2018-02-05 10:51:59 -080099 log.debug("Profiles %s have been removed in progran, removing them from XOS" % str(deleted_imsi))
Matteo Scandolo62919d62018-02-02 16:33:21 -0800100 for p in deleted_imsi:
101 si = MCordSubscriberInstance.objects.get(imsi_number=p)
Matteo Scandolo830403a2018-02-05 10:51:59 -0800102 # if si.created_by == 'XOS' and si.previously_sync == False:
103 # don't delete if the imsi has been created by XOS and it hasn't been sync'ed yet
104 # continue
Matteo Scandolo62919d62018-02-02 16:33:21 -0800105 si.delete()