Matteo Scandolo | 8f6aa12 | 2018-05-10 09:50:48 -0700 | [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 | |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame^] | 16 | from xosconfig import Config |
| 17 | from multistructlog import create_logger |
| 18 | |
| 19 | log = create_logger(Config().get('logging')) |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 20 | |
Matteo Scandolo | 8f6aa12 | 2018-05-10 09:50:48 -0700 | [diff] [blame] | 21 | class Helpers(): |
| 22 | @staticmethod |
| 23 | def format_url(url): |
| 24 | if 'http' in url: |
| 25 | return url |
| 26 | else: |
| 27 | return 'http://%s' % url |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 28 | |
Matteo Scandolo | 8f6aa12 | 2018-05-10 09:50:48 -0700 | [diff] [blame] | 29 | @staticmethod |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame^] | 30 | def get_onos_fabric_service(model_accessor, switch=None): |
| 31 | fabric = None |
| 32 | if switch: |
| 33 | fabric = model_accessor.Service.objects.get(id=switch.fabric_id) |
| 34 | else: |
| 35 | fabric = model_accessor.Service.objects.get(name="fabric") |
| 36 | onos_fabric_service = fabric.provider_services[0].leaf_model |
Scott Baker | afdf11d | 2018-08-16 15:47:55 -0700 | [diff] [blame] | 37 | return onos_fabric_service |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame^] | 38 | |
| 39 | @staticmethod |
| 40 | def get_onos(model, model_accessor, onos=None): |
| 41 | if(model.fabric != None): |
| 42 | onos = Helpers.get_onos_fabric_service(model_accessor, model) |
| 43 | else: |
| 44 | fabric_service = model_accessor.FabricService.objects.all() |
| 45 | if(len(fabric_service) == 1): |
| 46 | onos = Helpers.get_onos_fabric_service(model_accessor) |
| 47 | |
| 48 | if not onos: |
| 49 | log.error("Configuration error. Fabric Service without ONOS is not possible.") |
| 50 | raise Exception("Configuration error. Fabric Service without ONOS is not possible.") |
| 51 | return onos |