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 | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 24 | from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB, Handover |
| 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 SyncProgranServiceInstanceBack(SyncStep): |
| 42 | provides = [ProgranServiceInstance] |
| 43 | |
| 44 | observes = ProgranServiceInstance |
| 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 profiles from progran") |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 57 | onos = ProgranHelpers.get_progran_onos_info() |
| 58 | profile_url = "http://%s:%s/onos/progran/profile/" % (onos['url'], onos['port']) |
| 59 | r = requests.get(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 60 | res = r.json()['ProfileArray'] |
| 61 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 62 | log.debug("Received Profiles: ", profiles=res) |
| 63 | |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 64 | # remove default profiles |
| 65 | res = [p for p in res if "Default" not in p['Name']] |
| 66 | |
| 67 | field_mapping = { |
| 68 | 'Name': 'name', |
| 69 | 'Start': 'start', |
| 70 | 'End': 'end' |
| 71 | } |
| 72 | |
| 73 | field_transformations = { |
| 74 | 'Start': ProgranHelpers.date_to_time, |
| 75 | 'End': ProgranHelpers.date_to_time |
| 76 | } |
| 77 | |
| 78 | handover_mapping = { |
| 79 | 'A5Hysteresis': 'HysteresisA5', |
| 80 | 'A3Hysteresis': 'HysteresisA3' |
| 81 | } |
| 82 | |
| 83 | updated_profiles = [] |
| 84 | |
| 85 | for p in res: |
| 86 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 87 | |
| 88 | # checking for profiles |
| 89 | try: |
| 90 | si = ProgranServiceInstance.objects.get(name=p['Name']) |
| 91 | log.debug("Profile %s already exists, updating it" % p['Name']) |
| 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 | |
| 99 | si = ProgranHelpers.update_fields(si, p, field_mapping, field_transformations) |
| 100 | |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 101 | # checking for handovers |
| 102 | handover_dict = p['Handover'] |
| 103 | handover_dict = ProgranHelpers.convert_keys(handover_dict, handover_mapping) |
| 104 | del p['Handover'] |
| 105 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 106 | if si.handover_id: |
| 107 | handover = si.handover |
| 108 | log.debug("handover already exists, updating it", handover=handover_dict) |
| 109 | else: |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 110 | handover = Handover() |
| 111 | handover = ProgranHelpers.update_fields(handover, handover_dict) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 112 | log.debug("handover is new, creating it", handover=handover_dict) |
| 113 | handover.created_by = "Progran" |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 114 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 115 | handover = ProgranHelpers.update_fields(handover, handover_dict) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 116 | handover.save() |
| 117 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 118 | # Assigning handover to profile |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 119 | si.handover = handover |
| 120 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 121 | si.backend_status = "OK" |
| 122 | si.backend_code = 1 |
| 123 | |
Matteo Scandolo | 16f6176 | 2018-02-13 12:00:51 -0800 | [diff] [blame^] | 124 | si.no_sync = True |
| 125 | si.previously_sync = True |
| 126 | |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 127 | si.save() |
| 128 | |
| 129 | updated_profiles.append(si.name) |
| 130 | |
| 131 | existing_profiles = [p.name for p in ProgranServiceInstance.objects.all() if not p.is_new] |
| 132 | deleted_profiles = ProgranHelpers.list_diff(existing_profiles, updated_profiles) |
| 133 | |
| 134 | if len(deleted_profiles) > 0: |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 135 | 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] | 136 | for p in deleted_profiles: |
| 137 | si = ProgranServiceInstance.objects.get(name=p) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 138 | if si.created_by == 'XOS' and si.previously_sync == False: |
| 139 | # don't delete if the profile has been created by XOS and it hasn't been sync'ed yet |
| 140 | continue |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 141 | si.delete() |