blob: 077d559b01f200128d90275c86bf1388ae21f5a6 [file] [log] [blame]
Matteo Scandolo0a207b52018-01-29 13:39:43 -08001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from xosconfig import Config
16from multistructlog import create_logger
Matteo Scandoloc1102a52018-02-01 17:26:04 -080017import time
18import datetime
Matteo Scandolo0a207b52018-01-29 13:39:43 -080019
20log = create_logger(Config().get('logging'))
21
22class ProgranHelpers():
23
24 @staticmethod
25 def get_onos_info_from_si(service_instance):
26 progran_service = service_instance.owner.leaf_model
27 return ProgranHelpers.get_onos_info_from_service(progran_service)
28
29 @staticmethod
Scott Bakerb06daad2019-02-04 15:46:54 -080030 def get_progran_onos_info(model_accessor):
Matteo Scandolo6b607c82018-01-30 09:12:26 -080031 try:
Scott Bakerb06daad2019-02-04 15:46:54 -080032 progran_service = model_accessor.ProgranService.objects.all()[0]
Matteo Scandolo6b607c82018-01-30 09:12:26 -080033 except IndexError:
Matteo Scandoloc1102a52018-02-01 17:26:04 -080034 raise Exception("Cannot find Progran Service, does it exists?")
Matteo Scandolo0a207b52018-01-29 13:39:43 -080035 return ProgranHelpers.get_onos_info_from_service(progran_service)
36
37 @staticmethod
38 def get_onos_info_from_service(progran_service):
39 return {
40 'url': progran_service.onos_address,
41 'port': progran_service.onos_port,
42 'username': progran_service.onos_username,
43 'password': progran_service.onos_password,
44 }
Matteo Scandoloc1102a52018-02-01 17:26:04 -080045
46 @staticmethod
47 def get_progran_rest_errors(res):
48 res = res.json()
Matteo Scandolo830403a2018-02-05 10:51:59 -080049 if res['Result'] == -2 or res['Result'] == -1:
50 log.error('Error from ONOS Progran', error=res)
Matteo Scandoloc1102a52018-02-01 17:26:04 -080051 raise Exception(res['ErrCode'])
52
53 @staticmethod
54 def date_to_time(d):
55 if len(d) == 0:
56 return 0
57 return time.mktime(datetime.datetime.strptime(d, "%d.%m.%Y %H:%S").timetuple())
58
59 @staticmethod
60 def update_fields(model, dict, mapping={}, transformations={}):
61 dict = ProgranHelpers.convert_keys(dict, mapping, transformations)
62 for k, v in dict.iteritems():
63 if hasattr(model, k):
64 setattr(model, k, v)
Matteo Scandolo830403a2018-02-05 10:51:59 -080065 # else:
66 # log.debug("%s does not have a '%s' property, not updating it" % (model.model_name, k))
Matteo Scandoloc1102a52018-02-01 17:26:04 -080067 return model
68
69 @staticmethod
70 def convert_keys(dict, mapping={}, transformations={}):
71 for k, v in dict.iteritems():
72 if k in mapping:
73 # apply custom transformations to the data
74 if k in transformations:
75 dict[k] = transformations[k](v)
76
77 # NOTE we may have different names that the field in the dict
78 dict[mapping[k]] = dict[k]
79 del dict[k]
80 return dict
81
82 @staticmethod
83 def list_diff(first, second):
84 second = set(second)
85 return [item for item in first if item not in second]