Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016-present Ciena Corporation |
| 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 | # |
Chetan Gaonker | 7997bb4 | 2016-03-28 09:46:15 -0700 | [diff] [blame] | 16 | import os |
| 17 | import json |
| 18 | ##load the olt config |
| 19 | |
| 20 | class OltConfig: |
| 21 | def __init__(self, olt_conf_file = ''): |
| 22 | if not olt_conf_file: |
| 23 | self.olt_conf_file = os.getenv('OLT_CONFIG') |
| 24 | else: |
| 25 | self.olt_conf_file = olt_conf_file |
| 26 | try: |
| 27 | self.olt_handle = open(self.olt_conf_file, 'r') |
| 28 | self.olt_conf = json.load(self.olt_handle) |
Chetan Gaonker | 4ca5cca | 2016-04-11 13:59:35 -0700 | [diff] [blame] | 29 | self.olt_conf['olt'] = True |
Chetan Gaonker | 7997bb4 | 2016-03-28 09:46:15 -0700 | [diff] [blame] | 30 | except: |
| 31 | self.olt_handle = None |
| 32 | self.olt_conf = {} |
| 33 | self.olt_conf['olt'] = False |
| 34 | |
| 35 | def on_olt(self): |
| 36 | return self.olt_conf['olt'] is True |
| 37 | |
| 38 | def olt_port_map(self): |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 39 | if self.on_olt() and self.olt_conf.has_key('port_map'): |
Chetan Gaonker | 7997bb4 | 2016-03-28 09:46:15 -0700 | [diff] [blame] | 40 | port_map = {} |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 41 | port_map['ports'] = self.olt_conf['port_map']['ports'] |
| 42 | port_map['start_vlan'] = 0 |
| 43 | if self.olt_conf['port_map'].has_key('host'): |
| 44 | port_map['host'] = self.olt_conf['port_map']['host'] |
| 45 | else: |
| 46 | port_map['host'] = 'ovsbr0' |
| 47 | if self.olt_conf['port_map'].has_key('start_vlan'): |
| 48 | port_map['start_vlan'] = int(self.olt_conf['port_map']['start_vlan']) |
| 49 | |
Chetan Gaonker | 7997bb4 | 2016-03-28 09:46:15 -0700 | [diff] [blame] | 50 | ##Build a rx/tx port number to interface map |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 51 | port_map[1] = self.olt_conf['port_map']['rx'] |
| 52 | port_map[2] = self.olt_conf['port_map']['tx'] |
Chetan Gaonker | 7997bb4 | 2016-03-28 09:46:15 -0700 | [diff] [blame] | 53 | port_map[port_map[1]] = 1 |
| 54 | port_map[port_map[2]] = 2 |
| 55 | return port_map |
| 56 | else: |
| 57 | return None |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 58 | |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 59 | def olt_port_map_multi(self): |
| 60 | if self.on_olt() and self.olt_conf.has_key('port_map'): |
| 61 | port_map = {} |
| 62 | if self.olt_conf['port_map'].has_key('ports'): |
| 63 | port_map['ports'] = self.olt_conf['port_map']['ports'] |
| 64 | else: |
| 65 | port_map['ports'] = [] |
| 66 | num_ports = int(self.olt_conf['port_map']['num_ports']) |
| 67 | port_map['port'] = self.olt_conf['port_map']['port'] |
| 68 | for port in xrange(0, num_ports, 2): |
| 69 | port_map['ports'].append('veth{}'.format(port)) |
| 70 | port_num = 1 |
| 71 | port_map['uplink'] = int(self.olt_conf['uplink']) |
| 72 | port_list = [] |
| 73 | ##build the port map and inverse port map |
| 74 | for port in port_map['ports']: |
| 75 | port_map[port_num] = port |
| 76 | port_map[port] = port_num |
| 77 | if port_num != port_map['uplink']: |
| 78 | ##create tx,rx map |
| 79 | port_list.append( (port_map['uplink'], port_num ) ) |
| 80 | port_num += 1 |
| 81 | port_map['start_vlan'] = 0 |
| 82 | if self.olt_conf['port_map'].has_key('host'): |
| 83 | port_map['host'] = self.olt_conf['port_map']['host'] |
| 84 | else: |
| 85 | port_map['host'] = 'ovsbr0' |
| 86 | if self.olt_conf['port_map'].has_key('start_vlan'): |
| 87 | port_map['start_vlan'] = int(self.olt_conf['port_map']['start_vlan']) |
| 88 | |
| 89 | return port_map, port_list |
| 90 | else: |
| 91 | return None, None |
| 92 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 93 | def olt_device_data(self): |
| 94 | if self.on_olt(): |
| 95 | accessDeviceDict = {} |
| 96 | accessDeviceDict['uplink'] = str(self.olt_conf['uplink']) |
| 97 | accessDeviceDict['vlan'] = str(self.olt_conf['vlan']) |
| 98 | return accessDeviceDict |
| 99 | return None |