blob: b5a9a3e673bcfccdb7ca6e0f3586d8a838f8a630 [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
23from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
24from synchronizers.new_base.modelaccessor import MCordSubscriberInstance
25
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")
Matteo Scandolo62919d62018-02-02 16:33:21 -080057 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 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
69 updated_imsi = []
70
71 for i in res:
Matteo Scandolo62919d62018-02-02 16:33:21 -080072 try:
73 si = MCordSubscriberInstance.objects.get(imsi_number=i['IMSI'])
Matteo Scandolo830403a2018-02-05 10:51:59 -080074 log.debug("IMSI %s already exists, updating it" % i['IMSI'])
Matteo Scandolo62919d62018-02-02 16:33:21 -080075 except IndexError:
76 si = MCordSubscriberInstance()
77
78 si.no_sync = True
79 si.backend_code = 1
80 si.backend_status = "OK"
Matteo Scandolo830403a2018-02-05 10:51:59 -080081 si.created_by = "Progran"
Matteo Scandolo62919d62018-02-02 16:33:21 -080082
Matteo Scandolo830403a2018-02-05 10:51:59 -080083 log.debug("IMSI %s is new, creating it" % i['IMSI'])
Matteo Scandolo62919d62018-02-02 16:33:21 -080084
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 Scandolo830403a2018-02-05 10:51:59 -080095 log.debug("Profiles %s have been removed in progran, removing them from XOS" % str(deleted_imsi))
Matteo Scandolo62919d62018-02-02 16:33:21 -080096 for p in deleted_imsi:
97 si = MCordSubscriberInstance.objects.get(imsi_number=p)
Matteo Scandolo830403a2018-02-05 10:51:59 -080098 # 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 Scandolo62919d62018-02-02 16:33:21 -0800101 si.delete()