blob: ae97c95741fa6ac7c07f879621c5cae35eb24fab [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
23from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
24from 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
56 log.info("Reading profiles from progran")
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
62 # remove default profiles
63 res = [p for p in res if "Default" not in p['Name']]
64
65 field_mapping = {
66 'Name': 'name',
67 'Start': 'start',
68 'End': 'end'
69 }
70
71 field_transformations = {
72 'Start': ProgranHelpers.date_to_time,
73 'End': ProgranHelpers.date_to_time
74 }
75
76 handover_mapping = {
77 'A5Hysteresis': 'HysteresisA5',
78 'A3Hysteresis': 'HysteresisA3'
79 }
80
81 updated_profiles = []
82
83 for p in res:
84
85 # checking for handovers
86 handover_dict = p['Handover']
87 handover_dict = ProgranHelpers.convert_keys(handover_dict, handover_mapping)
88 del p['Handover']
89
90 try:
91 handover = Handover.objects.get(**handover_dict)
92 log.info("handover already exists, updating it", handover=handover_dict)
93 except IndexError:
94 handover = Handover()
95 handover = ProgranHelpers.update_fields(handover, handover_dict)
96 log.info("handover is new, creating it", handover=handover_dict)
97
98 handover.save()
99
100 # checking for profiles
101 try:
102 si = ProgranServiceInstance.objects.get(name=p['Name'])
103 log.info("Profile %s already exists, updating it" % p['Name'])
104 except IndexError:
105 si = ProgranServiceInstance()
106 si.name = p['Name']
107
108 si.no_sync = True
109
110 log.info("Profile %s is new, creating it" % p['Name'])
111
112 si = ProgranHelpers.update_fields(si, p, field_mapping, field_transformations)
113 si.handover = handover
114
115 si.save()
116
117 updated_profiles.append(si.name)
118
119 existing_profiles = [p.name for p in ProgranServiceInstance.objects.all() if not p.is_new]
120 deleted_profiles = ProgranHelpers.list_diff(existing_profiles, updated_profiles)
121
122 if len(deleted_profiles) > 0:
123 log.info("Profiles %s have been removed in progran, removing them from XOS" % str(deleted_profiles))
124 for p in deleted_profiles:
125 si = ProgranServiceInstance.objects.get(name=p)
126 si.delete()