A R Karthick | 36cfcef | 2016-08-18 15:20:07 -0700 | [diff] [blame] | 1 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 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 |
A R Karthick | 36cfcef | 2016-08-18 15:20:07 -0700 | [diff] [blame] | 7 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 36cfcef | 2016-08-18 15:20:07 -0700 | [diff] [blame] | 9 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 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 |
A R Karthick | 7c86455 | 2016-07-27 10:47:04 -0700 | [diff] [blame] | 34 | |
Chetan Gaonker | 7997bb4 | 2016-03-28 09:46:15 -0700 | [diff] [blame] | 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 = {} |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 41 | port_map['ponsim'] = self.olt_conf['port_map'].has_key('ponsim') |
A.R Karthick | 88e80b9 | 2016-12-05 20:23:45 -0800 | [diff] [blame] | 42 | if self.olt_conf['port_map'].has_key('switches'): |
| 43 | port_map['switches'] = self.olt_conf['port_map']['switches'] |
| 44 | else: |
| 45 | port_map['switches'] = [] |
| 46 | nr_switches = 1 |
| 47 | if self.olt_conf['port_map'].has_key('nr_switches'): |
| 48 | nr_switches = int(self.olt_conf['port_map']['nr_switches']) |
| 49 | for sw in xrange(nr_switches): |
| 50 | switch = 'br-int{}'.format(sw+1) if sw > 0 else 'br-int' |
| 51 | port_map['switches'].append(switch) |
| 52 | #if we have a host interface enabled, invalidate the switches config |
| 53 | if self.olt_conf['port_map'].has_key('host'): |
| 54 | #if host interface is specified, then use the host instead of ovs switch |
| 55 | port_map['host'] = self.olt_conf['port_map']['host'] |
A R Karthick | e8bd80e | 2017-08-01 12:36:24 -0700 | [diff] [blame] | 56 | port_map['switches'] = [ port_map['host'] ] + port_map['switches'] |
A.R Karthick | 88e80b9 | 2016-12-05 20:23:45 -0800 | [diff] [blame] | 57 | else: |
| 58 | port_map['host'] = port_map['switches'][0] |
| 59 | nr_switches = len(port_map['switches']) |
| 60 | port_map['switch_port_list'] = [] |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 61 | if self.olt_conf['port_map'].has_key('ports'): |
| 62 | port_map['ports'] = self.olt_conf['port_map']['ports'] |
A R Karthick | 36cfcef | 2016-08-18 15:20:07 -0700 | [diff] [blame] | 63 | num_ports = len(port_map['ports']) |
A.R Karthick | 88e80b9 | 2016-12-05 20:23:45 -0800 | [diff] [blame] | 64 | port_map['switch_port_list'].append( (port_map['switches'][0], port_map['ports']) ) |
A R Karthick | e8bd80e | 2017-08-01 12:36:24 -0700 | [diff] [blame] | 65 | index = 1 |
| 66 | for switch in port_map['switches'][1:]: |
| 67 | port_start = index * num_ports * 2 |
| 68 | port_end = port_start + num_ports * 2 |
| 69 | index += 1 |
| 70 | port_list = [] |
| 71 | for port in xrange(port_start, port_end, 2): |
| 72 | port_name = 'veth{}'.format(port) |
| 73 | port_map['ports'].append(port_name) |
| 74 | port_list.append(port_name) |
| 75 | port_map['switch_port_list'].append( (switch, port_list) ) |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 76 | else: |
| 77 | port_map['ports'] = [] |
| 78 | num_ports = int(self.olt_conf['port_map']['num_ports']) |
A.R Karthick | 88e80b9 | 2016-12-05 20:23:45 -0800 | [diff] [blame] | 79 | for sw in xrange(nr_switches): |
| 80 | port_list = [] |
| 81 | switch = port_map['switches'][sw] |
| 82 | port_start = sw * num_ports * 2 |
| 83 | port_end = port_start + num_ports * 2 |
| 84 | for port in xrange(port_start, port_end, 2): |
| 85 | port_name = 'veth{}'.format(port) |
| 86 | port_map['ports'].append(port_name) |
| 87 | port_list.append(port_name) |
| 88 | port_map['switch_port_list'].append( (switch, port_list) ) |
A R Karthick | 36cfcef | 2016-08-18 15:20:07 -0700 | [diff] [blame] | 89 | ##also add dhcprelay ports. We add as many relay ports as subscriber ports |
A.R Karthick | 88e80b9 | 2016-12-05 20:23:45 -0800 | [diff] [blame] | 90 | port_map['num_ports'] = num_ports |
A R Karthick | 36cfcef | 2016-08-18 15:20:07 -0700 | [diff] [blame] | 91 | relay_ports = num_ports |
| 92 | port_map['relay_ports'] = [] |
A.R Karthick | 88e80b9 | 2016-12-05 20:23:45 -0800 | [diff] [blame] | 93 | port_map['switch_relay_port_list'] = [] |
| 94 | for sw in xrange(nr_switches): |
| 95 | port_list = [] |
| 96 | switch = port_map['switches'][sw] |
| 97 | port_start = (nr_switches + sw) * relay_ports * 2 |
| 98 | port_end = port_start + relay_ports * 2 |
| 99 | for port in xrange(port_start, port_end, 2): |
| 100 | port_name = 'veth{}'.format(port) |
| 101 | port_map['relay_ports'].append(port_name) |
| 102 | port_list.append(port_name) |
| 103 | port_map['switch_relay_port_list'].append( (switch, port_list) ) |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 104 | port_num = 1 |
| 105 | port_map['uplink'] = int(self.olt_conf['uplink']) |
A R Karthick | 0776936 | 2016-07-28 17:36:15 -0700 | [diff] [blame] | 106 | port_map['wan'] = None |
| 107 | if self.olt_conf.has_key('wan'): |
| 108 | port_map['wan'] = self.olt_conf['wan'] |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 109 | port_list = [] |
| 110 | ##build the port map and inverse port map |
A.R Karthick | 88e80b9 | 2016-12-05 20:23:45 -0800 | [diff] [blame] | 111 | for sw in xrange(nr_switches): |
| 112 | sw_portnum = 1 |
| 113 | switch, ports = port_map['switch_port_list'][sw] |
| 114 | uplink = sw * num_ports + port_map['uplink'] |
| 115 | port_map[switch] = {} |
| 116 | port_map[switch]['uplink'] = uplink |
| 117 | for p in ports: |
| 118 | port_map[port_num] = p |
| 119 | port_map[p] = port_num |
| 120 | if sw_portnum != port_map['uplink']: |
| 121 | #create tx, rx map |
| 122 | port_list.append( (uplink, port_num) ) |
| 123 | port_num += 1 |
| 124 | sw_portnum += 1 |
A R Karthick | 36cfcef | 2016-08-18 15:20:07 -0700 | [diff] [blame] | 125 | ##build the port and inverse map for relay ports |
| 126 | for port in port_map['relay_ports']: |
| 127 | port_map[port_num] = port |
| 128 | port_map[port] = port_num |
| 129 | port_num += 1 |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 130 | port_map['start_vlan'] = 0 |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 131 | if self.olt_conf['port_map'].has_key('start_vlan'): |
| 132 | port_map['start_vlan'] = int(self.olt_conf['port_map']['start_vlan']) |
| 133 | |
| 134 | return port_map, port_list |
| 135 | else: |
| 136 | return None, None |
| 137 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 138 | def olt_device_data(self): |
| 139 | if self.on_olt(): |
| 140 | accessDeviceDict = {} |
| 141 | accessDeviceDict['uplink'] = str(self.olt_conf['uplink']) |
| 142 | accessDeviceDict['vlan'] = str(self.olt_conf['vlan']) |
| 143 | return accessDeviceDict |
| 144 | return None |
A.R Karthick | 369f89e | 2017-03-02 15:22:45 -0800 | [diff] [blame] | 145 | |
| 146 | def get_vcpes(self): |
| 147 | if self.on_olt(): |
| 148 | if self.olt_conf.has_key('vcpe'): |
| 149 | return self.olt_conf['vcpe'] |
| 150 | return [] |
A R Karthick | 03f40aa | 2017-03-20 19:33:55 -0700 | [diff] [blame] | 151 | |
| 152 | def get_vcpes_by_type(self, service): |
| 153 | return filter(lambda vcpe: vcpe['type'].lower() == service.lower(), self.get_vcpes()) |