Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -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 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 23 | from synchronizers.new_base.syncstep import SyncStep |
Matteo Scandolo | 72a3890 | 2018-02-15 09:13:31 -0800 | [diff] [blame^] | 24 | from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB, Handover, ServiceInstanceLink, MCordSubscriberInstance |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 25 | |
| 26 | from xosconfig import Config |
| 27 | from multistructlog import create_logger |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 28 | import requests |
| 29 | from requests.auth import HTTPBasicAuth |
| 30 | |
| 31 | |
| 32 | |
| 33 | log = create_logger(Config().get('logging')) |
| 34 | |
| 35 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 36 | sys.path.insert(0, parentdir) |
| 37 | sys.path.insert(0, os.path.dirname(__file__)) |
| 38 | from helpers import ProgranHelpers |
| 39 | |
| 40 | class SyncProgranServiceInstanceBack(SyncStep): |
| 41 | provides = [ProgranServiceInstance] |
| 42 | |
| 43 | observes = ProgranServiceInstance |
| 44 | |
| 45 | |
| 46 | def call(self, failed=[], deletion=False): |
| 47 | """ |
| 48 | Read profile from progran and save them in xos |
| 49 | """ |
| 50 | |
| 51 | if deletion == False: |
| 52 | # NOTE we won't it to run only after the delete has completed |
| 53 | return |
| 54 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 55 | log.debug("Reading profiles from progran") |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 56 | onos = ProgranHelpers.get_progran_onos_info() |
| 57 | profile_url = "http://%s:%s/onos/progran/profile/" % (onos['url'], onos['port']) |
| 58 | r = requests.get(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 59 | res = r.json()['ProfileArray'] |
| 60 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 61 | log.debug("Received Profiles: ", profiles=res) |
| 62 | |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 63 | # remove default profiles |
| 64 | res = [p for p in res if "Default" not in p['Name']] |
| 65 | |
| 66 | field_mapping = { |
| 67 | 'Name': 'name', |
| 68 | 'Start': 'start', |
| 69 | 'End': 'end' |
| 70 | } |
| 71 | |
| 72 | field_transformations = { |
| 73 | 'Start': ProgranHelpers.date_to_time, |
| 74 | 'End': ProgranHelpers.date_to_time |
| 75 | } |
| 76 | |
| 77 | handover_mapping = { |
| 78 | 'A5Hysteresis': 'HysteresisA5', |
| 79 | 'A3Hysteresis': 'HysteresisA3' |
| 80 | } |
| 81 | |
| 82 | updated_profiles = [] |
| 83 | |
| 84 | for p in res: |
| 85 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 86 | |
| 87 | # checking for profiles |
| 88 | try: |
| 89 | si = ProgranServiceInstance.objects.get(name=p['Name']) |
| 90 | log.debug("Profile %s already exists, updating it" % p['Name']) |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame] | 91 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 92 | except IndexError: |
| 93 | si = ProgranServiceInstance() |
| 94 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 95 | si.created_by = "Progran" |
| 96 | |
| 97 | log.debug("Profile %s is new, creating it" % p['Name']) |
| 98 | |
Matteo Scandolo | 72a3890 | 2018-02-15 09:13:31 -0800 | [diff] [blame^] | 99 | if not si.is_new: |
| 100 | # update IMSI association |
| 101 | xos_imsis_for_profile = [i.subscriber_service_instance.leaf_model for i in si.provided_links.all()] |
| 102 | progran_imsis_for_profile = p['IMSIRuleArray'] |
| 103 | |
| 104 | log.debug("List of imsis for profile %s in XOS" % p["Name"], imsis=xos_imsis_for_profile) |
| 105 | log.debug("List of imsis for profile %s in ONOS" % p["Name"], imsis=progran_imsis_for_profile) |
| 106 | |
| 107 | for i in xos_imsis_for_profile: |
| 108 | if not i.imsi_number in progran_imsis_for_profile: |
| 109 | log.debug("Removing Imsi %s from profile %s" % (i.imsi_number, p['Name'])) |
| 110 | |
| 111 | imsi_link = ServiceInstanceLink.objects.get(subscriber_service_instance_id=i.id) |
| 112 | |
| 113 | # NOTE: this model has already been removed from the backend, no need to synchronize |
| 114 | imsi_link.backend_need_delete = False |
| 115 | imsi_link.no_sync = True |
| 116 | imsi_link.save() # we need to save it to avoid a synchronization loop |
| 117 | |
| 118 | imsi_link.delete() |
| 119 | else: |
| 120 | # remove from imsi list coming from progran everything we already know about |
| 121 | progran_imsis_for_profile.remove(i.imsi_number) |
| 122 | |
| 123 | for i in progran_imsis_for_profile: |
| 124 | log.debug("Adding Imsi %s to profile %s" % (i, p['Name'])) |
| 125 | imsi = MCordSubscriberInstance.objects.get(imsi_number=i) |
| 126 | imsi_to_profile = ServiceInstanceLink(provider_service_instance=si, |
| 127 | subscriber_service_instance=imsi) |
| 128 | imsi_to_profile.save() |
| 129 | |
| 130 | # if the model has not been synchronized yet, skip it |
| 131 | if not si.is_new and si.no_sync is False: |
| 132 | log.info("Skipping profile %s as not synchronized" % p['Name']) |
| 133 | # NOTE add it to the removed profiles to avoid deletion (this is ugly, I know) |
| 134 | updated_profiles.append(si.name) |
| 135 | continue |
| 136 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 137 | si = ProgranHelpers.update_fields(si, p, field_mapping, field_transformations) |
| 138 | |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 139 | # checking for handovers |
| 140 | handover_dict = p['Handover'] |
| 141 | handover_dict = ProgranHelpers.convert_keys(handover_dict, handover_mapping) |
| 142 | del p['Handover'] |
| 143 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 144 | if si.handover_id: |
| 145 | handover = si.handover |
| 146 | log.debug("handover already exists, updating it", handover=handover_dict) |
| 147 | else: |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 148 | handover = Handover() |
| 149 | handover = ProgranHelpers.update_fields(handover, handover_dict) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 150 | log.debug("handover is new, creating it", handover=handover_dict) |
| 151 | handover.created_by = "Progran" |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 152 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 153 | handover = ProgranHelpers.update_fields(handover, handover_dict) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 154 | handover.save() |
| 155 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 156 | # Assigning handover to profile |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 157 | si.handover = handover |
| 158 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 159 | si.backend_status = "OK" |
| 160 | si.backend_code = 1 |
| 161 | |
Matteo Scandolo | 16f6176 | 2018-02-13 12:00:51 -0800 | [diff] [blame] | 162 | si.no_sync = True |
| 163 | si.previously_sync = True |
| 164 | |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame] | 165 | si.enacted = time.mktime(datetime.datetime.now().timetuple()) |
| 166 | |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 167 | si.save() |
| 168 | |
| 169 | updated_profiles.append(si.name) |
| 170 | |
| 171 | existing_profiles = [p.name for p in ProgranServiceInstance.objects.all() if not p.is_new] |
| 172 | deleted_profiles = ProgranHelpers.list_diff(existing_profiles, updated_profiles) |
| 173 | |
| 174 | if len(deleted_profiles) > 0: |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 175 | log.debug("Profiles %s have been removed in progran, removing them from XOS" % str(deleted_profiles)) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 176 | for p in deleted_profiles: |
| 177 | si = ProgranServiceInstance.objects.get(name=p) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 178 | if si.created_by == 'XOS' and si.previously_sync == False: |
| 179 | # don't delete if the profile has been created by XOS and it hasn't been sync'ed yet |
| 180 | continue |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame] | 181 | # TODO delete also the associated Handover |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 182 | si.delete() |