blob: e3308f6fe69ac912f45da5e90aefb7c6823233f7 [file] [log] [blame]
Matteo Scandolo8f6aa122018-05-10 09:50:48 -07001# 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 Bakere9855012019-04-01 15:01:34 -070015from __future__ import absolute_import
Himanshu Bhandari3d2d3412020-04-29 12:08:09 +053016from xosconfig import Config
17from multistructlog import create_logger
18
19log = create_logger(Config().get('logging'))
Scott Bakere9855012019-04-01 15:01:34 -070020
Matteo Scandolo8f6aa122018-05-10 09:50:48 -070021class Helpers():
22 @staticmethod
23 def format_url(url):
24 if 'http' in url:
25 return url
26 else:
27 return 'http://%s' % url
Scott Bakere9855012019-04-01 15:01:34 -070028
Matteo Scandolo8f6aa122018-05-10 09:50:48 -070029 @staticmethod
Himanshu Bhandari3d2d3412020-04-29 12:08:09 +053030 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 Bakerafdf11d2018-08-16 15:47:55 -070037 return onos_fabric_service
Himanshu Bhandari3d2d3412020-04-29 12:08:09 +053038
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