Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 1 | # 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 | |
| 15 | from xosconfig import Config |
| 16 | from multistructlog import create_logger |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 17 | import time |
| 18 | import datetime |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 19 | |
| 20 | log = create_logger(Config().get('logging')) |
| 21 | |
| 22 | class 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 Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 30 | def get_progran_onos_info(model_accessor): |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 31 | try: |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 32 | progran_service = model_accessor.ProgranService.objects.all()[0] |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 33 | except IndexError: |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 34 | raise Exception("Cannot find Progran Service, does it exists?") |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 35 | 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 Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 45 | |
| 46 | @staticmethod |
| 47 | def get_progran_rest_errors(res): |
| 48 | res = res.json() |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 49 | if res['Result'] == -2 or res['Result'] == -1: |
| 50 | log.error('Error from ONOS Progran', error=res) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 51 | 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 |
Matteo Scandolo | 138070a | 2019-02-16 11:06:17 -0800 | [diff] [blame] | 60 | def int_to_string(i): |
| 61 | return str(i) |
| 62 | |
| 63 | @staticmethod |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 64 | def update_fields(model, dict, mapping={}, transformations={}): |
| 65 | dict = ProgranHelpers.convert_keys(dict, mapping, transformations) |
| 66 | for k, v in dict.iteritems(): |
| 67 | if hasattr(model, k): |
Matteo Scandolo | 138070a | 2019-02-16 11:06:17 -0800 | [diff] [blame] | 68 | log.debug("Setting %s=%s on %s" % (k, v, model.model_name)) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 69 | setattr(model, k, v) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 70 | # else: |
| 71 | # log.debug("%s does not have a '%s' property, not updating it" % (model.model_name, k)) |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 72 | return model |
| 73 | |
| 74 | @staticmethod |
| 75 | def convert_keys(dict, mapping={}, transformations={}): |
| 76 | for k, v in dict.iteritems(): |
| 77 | if k in mapping: |
| 78 | # apply custom transformations to the data |
| 79 | if k in transformations: |
| 80 | dict[k] = transformations[k](v) |
| 81 | |
Matteo Scandolo | 138070a | 2019-02-16 11:06:17 -0800 | [diff] [blame] | 82 | # NOTE we may have different names than the field in the dict |
Matteo Scandolo | c1102a5 | 2018-02-01 17:26:04 -0800 | [diff] [blame] | 83 | dict[mapping[k]] = dict[k] |
| 84 | del dict[k] |
| 85 | return dict |
| 86 | |
| 87 | @staticmethod |
| 88 | def list_diff(first, second): |
| 89 | second = set(second) |
| 90 | return [item for item in first if item not in second] |