blob: 730b23d5af649c9390368e5687c735c76dbd7f45 [file] [log] [blame]
A R Karthick36cfcef2016-08-18 15:20:07 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# 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 Karthick36cfcef2016-08-18 15:20:07 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick36cfcef2016-08-18 15:20:07 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# 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 Gaonker7997bb42016-03-28 09:46:15 -070016import os
17import json
18##load the olt config
19
20class 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 Gaonker4ca5cca2016-04-11 13:59:35 -070029 self.olt_conf['olt'] = True
Chetan Gaonker7997bb42016-03-28 09:46:15 -070030 except:
31 self.olt_handle = None
32 self.olt_conf = {}
33 self.olt_conf['olt'] = False
A R Karthick7c864552016-07-27 10:47:04 -070034
Chetan Gaonker7997bb42016-03-28 09:46:15 -070035 def on_olt(self):
36 return self.olt_conf['olt'] is True
37
38 def olt_port_map(self):
Chetan Gaonker5209fe82016-04-19 10:09:53 -070039 if self.on_olt() and self.olt_conf.has_key('port_map'):
Chetan Gaonker7997bb42016-03-28 09:46:15 -070040 port_map = {}
A R Karthickb7e80902016-05-17 09:38:31 -070041 if self.olt_conf['port_map'].has_key('ports'):
42 port_map['ports'] = self.olt_conf['port_map']['ports']
A R Karthick36cfcef2016-08-18 15:20:07 -070043 num_ports = len(port_map['ports'])
A R Karthickb7e80902016-05-17 09:38:31 -070044 else:
45 port_map['ports'] = []
46 num_ports = int(self.olt_conf['port_map']['num_ports'])
A R Karthickb03cecd2016-07-27 10:27:55 -070047 for port in xrange(0, num_ports*2, 2):
A R Karthickb7e80902016-05-17 09:38:31 -070048 port_map['ports'].append('veth{}'.format(port))
A R Karthick36cfcef2016-08-18 15:20:07 -070049 ##also add dhcprelay ports. We add as many relay ports as subscriber ports
50 relay_ports = num_ports
51 port_map['relay_ports'] = []
52 for port in xrange(relay_ports*2, relay_ports*4, 2):
53 port_map['relay_ports'].append('veth{}'.format(port))
A R Karthickb7e80902016-05-17 09:38:31 -070054 port_num = 1
55 port_map['uplink'] = int(self.olt_conf['uplink'])
A R Karthick07769362016-07-28 17:36:15 -070056 port_map['wan'] = None
57 if self.olt_conf.has_key('wan'):
58 port_map['wan'] = self.olt_conf['wan']
A R Karthickb7e80902016-05-17 09:38:31 -070059 port_list = []
60 ##build the port map and inverse port map
61 for port in port_map['ports']:
62 port_map[port_num] = port
63 port_map[port] = port_num
64 if port_num != port_map['uplink']:
65 ##create tx,rx map
66 port_list.append( (port_map['uplink'], port_num ) )
67 port_num += 1
A R Karthick36cfcef2016-08-18 15:20:07 -070068 ##build the port and inverse map for relay ports
69 for port in port_map['relay_ports']:
70 port_map[port_num] = port
71 port_map[port] = port_num
72 port_num += 1
A R Karthickb7e80902016-05-17 09:38:31 -070073 port_map['start_vlan'] = 0
74 if self.olt_conf['port_map'].has_key('host'):
75 port_map['host'] = self.olt_conf['port_map']['host']
76 else:
77 port_map['host'] = 'ovsbr0'
78 if self.olt_conf['port_map'].has_key('start_vlan'):
79 port_map['start_vlan'] = int(self.olt_conf['port_map']['start_vlan'])
80
81 return port_map, port_list
82 else:
83 return None, None
84
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070085 def olt_device_data(self):
86 if self.on_olt():
87 accessDeviceDict = {}
88 accessDeviceDict['uplink'] = str(self.olt_conf['uplink'])
89 accessDeviceDict['vlan'] = str(self.olt_conf['vlan'])
90 return accessDeviceDict
91 return None