blob: 9195f2b8bebefd5908948b2f266c7b5706e400c4 [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
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 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 Scandolo830403a2018-02-05 10:51:59 -080056 log.debug("Reading profiles from progran")
Matteo Scandoloc1102a52018-02-01 17:26:04 -080057 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 Scandolo830403a2018-02-05 10:51:59 -080062 log.debug("Received Profiles: ", profiles=res)
63
Matteo Scandoloc1102a52018-02-01 17:26:04 -080064 # 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 Scandolo830403a2018-02-05 10:51:59 -080087
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
95 si.no_sync = True
96 si.created_by = "Progran"
97
98 log.debug("Profile %s is new, creating it" % p['Name'])
99
100 si = ProgranHelpers.update_fields(si, p, field_mapping, field_transformations)
101
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800102 # checking for handovers
103 handover_dict = p['Handover']
104 handover_dict = ProgranHelpers.convert_keys(handover_dict, handover_mapping)
105 del p['Handover']
106
Matteo Scandolo830403a2018-02-05 10:51:59 -0800107 if si.handover_id:
108 handover = si.handover
109 log.debug("handover already exists, updating it", handover=handover_dict)
110 else:
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800111 handover = Handover()
112 handover = ProgranHelpers.update_fields(handover, handover_dict)
Matteo Scandolo830403a2018-02-05 10:51:59 -0800113 log.debug("handover is new, creating it", handover=handover_dict)
114 handover.created_by = "Progran"
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800115
Matteo Scandolo830403a2018-02-05 10:51:59 -0800116 handover = ProgranHelpers.update_fields(handover, handover_dict)
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800117 handover.save()
118
Matteo Scandolo830403a2018-02-05 10:51:59 -0800119 # Assigning handover to profile
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800120 si.handover = handover
121
Matteo Scandolo830403a2018-02-05 10:51:59 -0800122 si.backend_status = "OK"
123 si.backend_code = 1
124
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800125 si.save()
126
127 updated_profiles.append(si.name)
128
129 existing_profiles = [p.name for p in ProgranServiceInstance.objects.all() if not p.is_new]
130 deleted_profiles = ProgranHelpers.list_diff(existing_profiles, updated_profiles)
131
132 if len(deleted_profiles) > 0:
Matteo Scandolo830403a2018-02-05 10:51:59 -0800133 log.debug("Profiles %s have been removed in progran, removing them from XOS" % str(deleted_profiles))
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800134 for p in deleted_profiles:
135 si = ProgranServiceInstance.objects.get(name=p)
Matteo Scandolo830403a2018-02-05 10:51:59 -0800136 if si.created_by == 'XOS' and si.previously_sync == False:
137 # don't delete if the profile has been created by XOS and it hasn't been sync'ed yet
138 continue
Matteo Scandoloc1102a52018-02-01 17:26:04 -0800139 si.delete()