blob: 8b38d74a4c648010f63147768eb651e956229395 [file] [log] [blame]
Matteo Scandoloc1102a52018-02-01 17:26:04 -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
Matteo Scandolo830403a2018-02-05 10:51:59 -080023from synchronizers.new_base.syncstep import SyncStep
Matteo Scandoloc1102a52018-02-01 17:26:04 -080024from synchronizers.new_base.modelaccessor import ProgranServiceInstance, ENodeB, Handover
25
26from xosconfig import Config
27from multistructlog import create_logger
Matteo Scandoloc1102a52018-02-01 17:26:04 -080028import requests
29from requests.auth import HTTPBasicAuth
30
31
32
33log = create_logger(Config().get('logging'))
34
35parentdir = os.path.join(os.path.dirname(__file__), "..")
36sys.path.insert(0, parentdir)
37sys.path.insert(0, os.path.dirname(__file__))
38from helpers import ProgranHelpers
39
40class 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 Scandolo830403a2018-02-05 10:51:59 -080055 log.debug("Reading profiles from progran")
Matteo Scandoloc1102a52018-02-01 17:26:04 -080056 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 Scandolo830403a2018-02-05 10:51:59 -080061 log.debug("Received Profiles: ", profiles=res)
62
Matteo Scandoloc1102a52018-02-01 17:26:04 -080063 # 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 Scandolo830403a2018-02-05 10:51:59 -080086
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 Scandolof6b6ed22018-02-13 15:27:21 -080091
92 # if the model has not been synchronizer yet, skip it
93 if si.no_sync is True:
94 log.info("Skipping profile %s as not synchronized" % p['Name'])
95 # NOTE add it to the removed profiles to avoid deletion (this is ugly, I know)
96 updated_profiles.append(si.name)
97 continue
Matteo Scandolo830403a2018-02-05 10:51:59 -080098 except IndexError:
99 si = ProgranServiceInstance()
100
Matteo Scandolo830403a2018-02-05 10:51:59 -0800101 si.created_by = "Progran"
102
103 log.debug("Profile %s is new, creating it" % p['Name'])
104
105 si = ProgranHelpers.update_fields(si, p, field_mapping, field_transformations)
106
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800107 # checking for handovers
108 handover_dict = p['Handover']
109 handover_dict = ProgranHelpers.convert_keys(handover_dict, handover_mapping)
110 del p['Handover']
111
Matteo Scandolo830403a2018-02-05 10:51:59 -0800112 if si.handover_id:
113 handover = si.handover
114 log.debug("handover already exists, updating it", handover=handover_dict)
115 else:
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800116 handover = Handover()
117 handover = ProgranHelpers.update_fields(handover, handover_dict)
Matteo Scandolo830403a2018-02-05 10:51:59 -0800118 log.debug("handover is new, creating it", handover=handover_dict)
119 handover.created_by = "Progran"
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800120
Matteo Scandolo830403a2018-02-05 10:51:59 -0800121 handover = ProgranHelpers.update_fields(handover, handover_dict)
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800122 handover.save()
123
Matteo Scandolo830403a2018-02-05 10:51:59 -0800124 # Assigning handover to profile
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800125 si.handover = handover
126
Matteo Scandolo830403a2018-02-05 10:51:59 -0800127 si.backend_status = "OK"
128 si.backend_code = 1
129
Matteo Scandolo16f61762018-02-13 12:00:51 -0800130 si.no_sync = True
131 si.previously_sync = True
132
Matteo Scandolof6b6ed22018-02-13 15:27:21 -0800133 si.enacted = time.mktime(datetime.datetime.now().timetuple())
134
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800135 si.save()
136
137 updated_profiles.append(si.name)
138
139 existing_profiles = [p.name for p in ProgranServiceInstance.objects.all() if not p.is_new]
140 deleted_profiles = ProgranHelpers.list_diff(existing_profiles, updated_profiles)
141
142 if len(deleted_profiles) > 0:
Matteo Scandolo830403a2018-02-05 10:51:59 -0800143 log.debug("Profiles %s have been removed in progran, removing them from XOS" % str(deleted_profiles))
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800144 for p in deleted_profiles:
145 si = ProgranServiceInstance.objects.get(name=p)
Matteo Scandolo830403a2018-02-05 10:51:59 -0800146 if si.created_by == 'XOS' and si.previously_sync == False:
147 # don't delete if the profile has been created by XOS and it hasn't been sync'ed yet
148 continue
Matteo Scandolof6b6ed22018-02-13 15:27:21 -0800149 # TODO delete also the associated Handover
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800150 si.delete()